ia64/xen-unstable

view tools/tests/test_x86_emulator.c @ 11827:a7c6b1c5507c

[IA64] remove unused vmx/mm.c file

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author awilliam@xenbuild.aw
date Sun Oct 22 14:14:58 2006 -0600 (2006-10-22)
parents 51a98a6c2c05
children 88935ae47fa9
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 #define PFEC_write_access (1U<<1)
20 static int read_any(
21 unsigned long addr,
22 unsigned long *val,
23 unsigned int bytes,
24 struct x86_emulate_ctxt *ctxt)
25 {
26 switch ( bytes )
27 {
28 case 1: *val = *(u8 *)addr; break;
29 case 2: *val = *(u16 *)addr; break;
30 case 4: *val = *(u32 *)addr; break;
31 case 8: *val = *(unsigned long *)addr; break;
32 }
33 return X86EMUL_CONTINUE;
34 }
36 static int write_any(
37 unsigned long addr,
38 unsigned long val,
39 unsigned int bytes,
40 struct x86_emulate_ctxt *ctxt)
41 {
42 switch ( bytes )
43 {
44 case 1: *(u8 *)addr = (u8)val; break;
45 case 2: *(u16 *)addr = (u16)val; break;
46 case 4: *(u32 *)addr = (u32)val; break;
47 case 8: *(unsigned long *)addr = val; break;
48 }
49 return X86EMUL_CONTINUE;
50 }
52 static int cmpxchg_any(
53 unsigned long addr,
54 unsigned long old,
55 unsigned long new,
56 unsigned int bytes,
57 struct x86_emulate_ctxt *ctxt)
58 {
59 switch ( bytes )
60 {
61 case 1: *(u8 *)addr = (u8)new; break;
62 case 2: *(u16 *)addr = (u16)new; break;
63 case 4: *(u32 *)addr = (u32)new; break;
64 case 8: *(unsigned long *)addr = new; break;
65 }
66 return X86EMUL_CONTINUE;
67 }
69 static int cmpxchg8b_any(
70 unsigned long addr,
71 unsigned long old_lo,
72 unsigned long old_hi,
73 unsigned long new_lo,
74 unsigned long new_hi,
75 struct x86_emulate_ctxt *ctxt)
76 {
77 ((unsigned long *)addr)[0] = new_lo;
78 ((unsigned long *)addr)[1] = new_hi;
79 return X86EMUL_CONTINUE;
80 }
82 static struct x86_emulate_ops emulops = {
83 read_any, write_any, read_any, write_any, cmpxchg_any, cmpxchg8b_any
84 };
86 int main(int argc, char **argv)
87 {
88 struct x86_emulate_ctxt ctxt;
89 struct cpu_user_regs regs;
90 char instr[20] = { 0x01, 0x08 }; /* add %ecx,(%eax) */
91 unsigned int *res;
92 int rc;
94 ctxt.regs = &regs;
95 ctxt.mode = X86EMUL_MODE_PROT32;
97 res = mmap((void *)0x100000, 0x1000, PROT_READ|PROT_WRITE,
98 MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
99 if ( res == MAP_FAILED )
100 {
101 fprintf(stderr, "mmap to low address failed\n");
102 exit(1);
103 }
105 printf("%-40s", "Testing addl %%ecx,(%%eax)...");
106 instr[0] = 0x01; instr[1] = 0x08;
107 regs.eflags = 0x200;
108 regs.eip = (unsigned long)&instr[0];
109 regs.ecx = 0x12345678;
110 regs.error_code = PFEC_write_access;
111 ctxt.cr2 = (unsigned long)res;
112 *res = 0x7FFFFFFF;
113 rc = x86_emulate_memop(&ctxt, &emulops);
114 if ( (rc != 0) ||
115 (*res != 0x92345677) ||
116 (regs.eflags != 0xa94) ||
117 (regs.eip != (unsigned long)&instr[2]) )
118 goto fail;
119 printf("okay\n");
121 printf("%-40s", "Testing xorl (%%eax),%%ecx...");
122 instr[0] = 0x33; instr[1] = 0x08;
123 regs.eflags = 0x200;
124 regs.eip = (unsigned long)&instr[0];
125 #ifdef __x86_64__
126 regs.ecx = 0xFFFFFFFF12345678UL;
127 #else
128 regs.ecx = 0x12345678UL;
129 #endif
130 ctxt.cr2 = (unsigned long)res;
131 regs.error_code = 0;
132 rc = x86_emulate_memop(&ctxt, &emulops);
133 if ( (rc != 0) ||
134 (*res != 0x92345677) ||
135 (regs.ecx != 0x8000000FUL) ||
136 (regs.eip != (unsigned long)&instr[2]) )
137 goto fail;
138 printf("okay\n");
140 printf("%-40s", "Testing movl (%%eax),%%ecx...");
141 instr[0] = 0x8b; instr[1] = 0x08;
142 regs.eflags = 0x200;
143 regs.eip = (unsigned long)&instr[0];
144 regs.ecx = ~0UL;
145 ctxt.cr2 = (unsigned long)res;
146 regs.error_code = 0;
147 rc = x86_emulate_memop(&ctxt, &emulops);
148 if ( (rc != 0) ||
149 (*res != 0x92345677) ||
150 (regs.ecx != 0x92345677UL) ||
151 (regs.eip != (unsigned long)&instr[2]) )
152 goto fail;
153 printf("okay\n");
155 printf("%-40s", "Testing lock cmpxchgb %%cl,(%%eax)...");
156 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x08;
157 regs.eflags = 0x200;
158 regs.eip = (unsigned long)&instr[0];
159 regs.eax = 0x92345677UL;
160 regs.ecx = 0xAA;
161 ctxt.cr2 = (unsigned long)res;
162 regs.error_code = PFEC_write_access;
163 rc = x86_emulate_memop(&ctxt, &emulops);
164 if ( (rc != 0) ||
165 (*res != 0x923456AA) ||
166 (regs.eflags != 0x244) ||
167 (regs.eax != 0x92345677UL) ||
168 (regs.eip != (unsigned long)&instr[4]) )
169 goto fail;
170 printf("okay\n");
172 printf("%-40s", "Testing lock cmpxchgb %%cl,(%%eax)...");
173 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb0; instr[3] = 0x08;
174 regs.eflags = 0x200;
175 regs.eip = (unsigned long)&instr[0];
176 regs.eax = 0xAABBCC77UL;
177 regs.ecx = 0xFF;
178 ctxt.cr2 = (unsigned long)res;
179 regs.error_code = PFEC_write_access;
180 rc = x86_emulate_memop(&ctxt, &emulops);
181 if ( (rc != 0) ||
182 (*res != 0x923456AA) ||
183 ((regs.eflags&0x240) != 0x200) ||
184 (regs.eax != 0xAABBCCAA) ||
185 (regs.ecx != 0xFF) ||
186 (regs.eip != (unsigned long)&instr[4]) )
187 goto fail;
188 printf("okay\n");
190 printf("%-40s", "Testing xchgl %%ecx,(%%eax)...");
191 instr[0] = 0x87; instr[1] = 0x08;
192 regs.eflags = 0x200;
193 regs.eip = (unsigned long)&instr[0];
194 regs.ecx = 0x12345678;
195 ctxt.cr2 = (unsigned long)res;
196 regs.error_code = PFEC_write_access;
197 rc = x86_emulate_memop(&ctxt, &emulops);
198 if ( (rc != 0) ||
199 (*res != 0x12345678) ||
200 (regs.eflags != 0x200) ||
201 (regs.ecx != 0x923456AA) ||
202 (regs.eip != (unsigned long)&instr[2]) )
203 goto fail;
204 printf("okay\n");
206 printf("%-40s", "Testing lock cmpxchgl %%ecx,(%%eax)...");
207 instr[0] = 0xf0; instr[1] = 0x0f; instr[2] = 0xb1; instr[3] = 0x08;
208 regs.eflags = 0x200;
209 *res = 0x923456AA;
210 regs.eip = (unsigned long)&instr[0];
211 regs.eax = 0x923456AAUL;
212 regs.ecx = 0xDDEEFF00L;
213 ctxt.cr2 = (unsigned long)res;
214 regs.error_code = PFEC_write_access;
215 rc = x86_emulate_memop(&ctxt, &emulops);
216 if ( (rc != 0) ||
217 (*res != 0xDDEEFF00) ||
218 (regs.eflags != 0x244) ||
219 (regs.eax != 0x923456AAUL) ||
220 (regs.eip != (unsigned long)&instr[4]) )
221 goto fail;
222 printf("okay\n");
224 printf("%-40s", "Testing rep movsw...");
225 instr[0] = 0xf3; instr[1] = 0x66; instr[2] = 0xa5;
226 *res = 0x22334455;
227 regs.eflags = 0x200;
228 regs.ecx = 23;
229 regs.eip = (unsigned long)&instr[0];
230 regs.esi = (unsigned long)res + 0;
231 regs.edi = (unsigned long)res + 2;
232 regs.error_code = 0; /* read fault */
233 ctxt.cr2 = regs.esi;
234 rc = x86_emulate_memop(&ctxt, &emulops);
235 if ( (rc != 0) ||
236 (*res != 0x44554455) ||
237 (regs.eflags != 0x200) ||
238 (regs.ecx != 22) ||
239 (regs.esi != ((unsigned long)res + 2)) ||
240 (regs.edi != ((unsigned long)res + 4)) ||
241 (regs.eip != (unsigned long)&instr[0]) )
242 goto fail;
243 printf("okay\n");
245 printf("%-40s", "Testing btrl $0x1,(%edi)...");
246 instr[0] = 0x0f; instr[1] = 0xba; instr[2] = 0x37; instr[3] = 0x01;
247 *res = 0x2233445F;
248 regs.eflags = 0x200;
249 regs.eip = (unsigned long)&instr[0];
250 regs.edi = (unsigned long)res;
251 ctxt.cr2 = regs.edi;
252 regs.error_code = PFEC_write_access;
253 rc = x86_emulate_memop(&ctxt, &emulops);
254 if ( (rc != 0) ||
255 (*res != 0x2233445D) ||
256 ((regs.eflags&0x201) != 0x201) ||
257 (regs.eip != (unsigned long)&instr[4]) )
258 goto fail;
259 printf("okay\n");
261 res[0] = 0x12345678;
262 res[1] = 0x87654321;
264 printf("%-40s", "Testing cmpxchg8b (%edi) [succeeding]...");
265 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
266 regs.eflags = 0x200;
267 regs.eax = res[0];
268 regs.edx = res[1];
269 regs.ebx = 0x9999AAAA;
270 regs.ecx = 0xCCCCFFFF;
271 regs.eip = (unsigned long)&instr[0];
272 regs.edi = (unsigned long)res;
273 ctxt.cr2 = regs.edi;
274 regs.error_code = PFEC_write_access;
275 rc = x86_emulate_memop(&ctxt, &emulops);
276 if ( (rc != 0) ||
277 (res[0] != 0x9999AAAA) ||
278 (res[1] != 0xCCCCFFFF) ||
279 ((regs.eflags&0x240) != 0x240) ||
280 (regs.eip != (unsigned long)&instr[3]) )
281 goto fail;
282 printf("okay\n");
284 printf("%-40s", "Testing cmpxchg8b (%edi) [failing]...");
285 instr[0] = 0x0f; instr[1] = 0xc7; instr[2] = 0x0f;
286 regs.eip = (unsigned long)&instr[0];
287 regs.edi = (unsigned long)res;
288 ctxt.cr2 = regs.edi;
289 regs.error_code = PFEC_write_access;
290 rc = x86_emulate_memop(&ctxt, &emulops);
291 if ( (rc != 0) ||
292 (res[0] != 0x9999AAAA) ||
293 (res[1] != 0xCCCCFFFF) ||
294 (regs.eax != 0x9999AAAA) ||
295 (regs.edx != 0xCCCCFFFF) ||
296 ((regs.eflags&0x240) != 0x200) ||
297 (regs.eip != (unsigned long)&instr[3]) )
298 goto fail;
299 printf("okay\n");
301 printf("%-40s", "Testing movsxbd (%%eax),%%ecx...");
302 instr[0] = 0x0f; instr[1] = 0xbe; instr[2] = 0x08;
303 regs.eip = (unsigned long)&instr[0];
304 regs.ecx = 0x12345678;
305 ctxt.cr2 = (unsigned long)res;
306 *res = 0x82;
307 regs.error_code = 0;
308 rc = x86_emulate_memop(&ctxt, &emulops);
309 if ( (rc != 0) ||
310 (*res != 0x82) ||
311 (regs.ecx != 0xFFFFFF82) ||
312 ((regs.eflags&0x240) != 0x200) ||
313 (regs.eip != (unsigned long)&instr[3]) )
314 goto fail;
315 printf("okay\n");
317 printf("%-40s", "Testing movzxwd (%%eax),%%ecx...");
318 instr[0] = 0x0f; instr[1] = 0xb7; instr[2] = 0x08;
319 regs.eip = (unsigned long)&instr[0];
320 regs.ecx = 0x12345678;
321 ctxt.cr2 = (unsigned long)res;
322 *res = 0x1234aa82;
323 regs.error_code = 0;
324 rc = x86_emulate_memop(&ctxt, &emulops);
325 if ( (rc != 0) ||
326 (*res != 0x1234aa82) ||
327 (regs.ecx != 0xaa82) ||
328 ((regs.eflags&0x240) != 0x200) ||
329 (regs.eip != (unsigned long)&instr[3]) )
330 goto fail;
331 printf("okay\n");
333 return 0;
335 fail:
336 printf("failed!\n");
337 return 1;
338 }