blob: 4867f58f178fc0040fc316d9de4cf6e35bee1746 [file] [log] [blame]
Avi Kivity7d36db32010-08-03 14:07:34 +03001#include "ioram.h"
2#include "vm.h"
3#include "libcflat.h"
Gleb Natapove7c37962010-12-22 17:06:18 +02004#include "desc.h"
Avi Kivityd7143f32012-03-25 15:49:05 +02005#include "types.h"
Avi Kivity7d36db32010-08-03 14:07:34 +03006
7#define memset __builtin_memset
8#define TESTDEV_IO_PORT 0xe0
9
10int fails, tests;
11
Avi Kivityd7143f32012-03-25 15:49:05 +020012static int exceptions;
13
Avi Kivity7d36db32010-08-03 14:07:34 +030014void report(const char *name, int result)
15{
16 ++tests;
17 if (result)
18 printf("PASS: %s\n", name);
19 else {
20 printf("FAIL: %s\n", name);
21 ++fails;
22 }
23}
24
25static char st1[] = "abcdefghijklmnop";
26
27void test_stringio()
28{
29 unsigned char r = 0;
30 asm volatile("cld \n\t"
31 "movw %0, %%dx \n\t"
32 "rep outsb \n\t"
33 : : "i"((short)TESTDEV_IO_PORT),
34 "S"(st1), "c"(sizeof(st1) - 1));
35 asm volatile("inb %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
36 report("outsb up", r == st1[sizeof(st1) - 2]); /* last char */
37
38 asm volatile("std \n\t"
39 "movw %0, %%dx \n\t"
40 "rep outsb \n\t"
41 : : "i"((short)TESTDEV_IO_PORT),
42 "S"(st1 + sizeof(st1) - 2), "c"(sizeof(st1) - 1));
43 asm volatile("cld \n\t" : : );
44 asm volatile("in %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
45 report("outsb down", r == st1[0]);
46}
47
48void test_cmps_one(unsigned char *m1, unsigned char *m3)
49{
50 void *rsi, *rdi;
51 long rcx, tmp;
52
53 rsi = m1; rdi = m3; rcx = 30;
54 asm volatile("xor %[tmp], %[tmp] \n\t"
55 "repe/cmpsb"
56 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
57 : : "cc");
58 report("repe/cmpsb (1)", rcx == 0 && rsi == m1 + 30 && rdi == m3 + 30);
59
Avi Kivity51ba4182010-08-17 19:34:39 +030060 rsi = m1; rdi = m3; rcx = 30;
61 asm volatile("or $1, %[tmp]\n\t" // clear ZF
62 "repe/cmpsb"
63 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
64 : : "cc");
65 report("repe/cmpsb (1.zf)", rcx == 0 && rsi == m1 + 30 && rdi == m3 + 30);
66
Avi Kivity7d36db32010-08-03 14:07:34 +030067 rsi = m1; rdi = m3; rcx = 15;
68 asm volatile("xor %[tmp], %[tmp] \n\t"
69 "repe/cmpsw"
70 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
71 : : "cc");
72 report("repe/cmpsw (1)", rcx == 0 && rsi == m1 + 30 && rdi == m3 + 30);
73
74 rsi = m1; rdi = m3; rcx = 7;
75 asm volatile("xor %[tmp], %[tmp] \n\t"
76 "repe/cmpsl"
77 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
78 : : "cc");
79 report("repe/cmpll (1)", rcx == 0 && rsi == m1 + 28 && rdi == m3 + 28);
80
81 rsi = m1; rdi = m3; rcx = 4;
82 asm volatile("xor %[tmp], %[tmp] \n\t"
83 "repe/cmpsq"
84 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
85 : : "cc");
86 report("repe/cmpsq (1)", rcx == 0 && rsi == m1 + 32 && rdi == m3 + 32);
87
88 rsi = m1; rdi = m3; rcx = 130;
89 asm volatile("xor %[tmp], %[tmp] \n\t"
90 "repe/cmpsb"
91 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
92 : : "cc");
93 report("repe/cmpsb (2)",
94 rcx == 29 && rsi == m1 + 101 && rdi == m3 + 101);
95
96 rsi = m1; rdi = m3; rcx = 65;
97 asm volatile("xor %[tmp], %[tmp] \n\t"
98 "repe/cmpsw"
99 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
100 : : "cc");
101 report("repe/cmpsw (2)",
102 rcx == 14 && rsi == m1 + 102 && rdi == m3 + 102);
103
104 rsi = m1; rdi = m3; rcx = 32;
105 asm volatile("xor %[tmp], %[tmp] \n\t"
106 "repe/cmpsl"
107 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
108 : : "cc");
109 report("repe/cmpll (2)",
110 rcx == 6 && rsi == m1 + 104 && rdi == m3 + 104);
111
112 rsi = m1; rdi = m3; rcx = 16;
113 asm volatile("xor %[tmp], %[tmp] \n\t"
114 "repe/cmpsq"
115 : "+S"(rsi), "+D"(rdi), "+c"(rcx), [tmp]"=&r"(tmp)
116 : : "cc");
117 report("repe/cmpsq (2)",
118 rcx == 3 && rsi == m1 + 104 && rdi == m3 + 104);
119
120}
121
122void test_cmps(void *mem)
123{
124 unsigned char *m1 = mem, *m2 = mem + 1024;
125 unsigned char m3[1024];
126
127 for (int i = 0; i < 100; ++i)
128 m1[i] = m2[i] = m3[i] = i;
129 for (int i = 100; i < 200; ++i)
130 m1[i] = (m3[i] = m2[i] = i) + 1;
131 test_cmps_one(m1, m3);
132 test_cmps_one(m1, m2);
133}
134
Avi Kivity80a4ea72010-08-17 17:44:14 +0300135void test_scas(void *mem)
136{
137 bool z;
138 void *di;
139
140 *(ulong *)mem = 0x77665544332211;
141
142 di = mem;
143 asm ("scasb; setz %0" : "=rm"(z), "+D"(di) : "a"(0xff11));
144 report("scasb match", di == mem + 1 && z);
145
146 di = mem;
147 asm ("scasb; setz %0" : "=rm"(z), "+D"(di) : "a"(0xff54));
148 report("scasb mismatch", di == mem + 1 && !z);
149
150 di = mem;
151 asm ("scasw; setz %0" : "=rm"(z), "+D"(di) : "a"(0xff2211));
152 report("scasw match", di == mem + 2 && z);
153
154 di = mem;
155 asm ("scasw; setz %0" : "=rm"(z), "+D"(di) : "a"(0xffdd11));
156 report("scasw mismatch", di == mem + 2 && !z);
157
158 di = mem;
159 asm ("scasl; setz %0" : "=rm"(z), "+D"(di) : "a"(0xff44332211ul));
160 report("scasd match", di == mem + 4 && z);
161
162 di = mem;
163 asm ("scasl; setz %0" : "=rm"(z), "+D"(di) : "a"(0x45332211));
164 report("scasd mismatch", di == mem + 4 && !z);
165
166 di = mem;
167 asm ("scasq; setz %0" : "=rm"(z), "+D"(di) : "a"(0x77665544332211ul));
168 report("scasq match", di == mem + 8 && z);
169
170 di = mem;
171 asm ("scasq; setz %0" : "=rm"(z), "+D"(di) : "a"(3));
172 report("scasq mismatch", di == mem + 8 && !z);
173}
174
Avi Kivity7d36db32010-08-03 14:07:34 +0300175void test_cr8(void)
176{
177 unsigned long src, dst;
178
179 dst = 777;
180 src = 3;
181 asm volatile("mov %[src], %%cr8; mov %%cr8, %[dst]"
182 : [dst]"+r"(dst), [src]"+r"(src));
183 report("mov %cr8", dst == 3 && src == 3);
184}
185
186void test_push(void *mem)
187{
188 unsigned long tmp;
189 unsigned long *stack_top = mem + 4096;
190 unsigned long *new_stack_top;
191 unsigned long memw = 0x123456789abcdeful;
192
193 memset(mem, 0x55, (void *)stack_top - mem);
194
195 asm volatile("mov %%rsp, %[tmp] \n\t"
196 "mov %[stack_top], %%rsp \n\t"
197 "pushq $-7 \n\t"
198 "pushq %[reg] \n\t"
199 "pushq (%[mem]) \n\t"
200 "pushq $-7070707 \n\t"
201 "mov %%rsp, %[new_stack_top] \n\t"
202 "mov %[tmp], %%rsp"
203 : [tmp]"=&r"(tmp), [new_stack_top]"=r"(new_stack_top)
204 : [stack_top]"r"(stack_top),
205 [reg]"r"(-17l), [mem]"r"(&memw)
206 : "memory");
207
208 report("push $imm8", stack_top[-1] == -7ul);
209 report("push %reg", stack_top[-2] == -17ul);
210 report("push mem", stack_top[-3] == 0x123456789abcdeful);
211 report("push $imm", stack_top[-4] == -7070707);
212}
213
214void test_pop(void *mem)
215{
Avi Kivity5269d6e2012-06-27 11:36:31 +0300216 unsigned long tmp, tmp3;
Avi Kivity7d36db32010-08-03 14:07:34 +0300217 unsigned long *stack_top = mem + 4096;
218 unsigned long memw = 0x123456789abcdeful;
219 static unsigned long tmp2;
220
221 memset(mem, 0x55, (void *)stack_top - mem);
222
223 asm volatile("pushq %[val] \n\t"
224 "popq (%[mem])"
225 : : [val]"m"(memw), [mem]"r"(mem) : "memory");
226 report("pop mem", *(unsigned long *)mem == memw);
227
228 memw = 7 - memw;
229 asm volatile("mov %%rsp, %[tmp] \n\t"
230 "mov %[stack_top], %%rsp \n\t"
231 "pushq %[val] \n\t"
232 "popq %[tmp2] \n\t"
233 "mov %[tmp], %%rsp"
234 : [tmp]"=&r"(tmp), [tmp2]"=m"(tmp2)
235 : [val]"r"(memw), [stack_top]"r"(stack_top)
236 : "memory");
237 report("pop mem (2)", tmp2 == memw);
238
239 memw = 129443 - memw;
240 asm volatile("mov %%rsp, %[tmp] \n\t"
241 "mov %[stack_top], %%rsp \n\t"
242 "pushq %[val] \n\t"
243 "popq %[tmp2] \n\t"
244 "mov %[tmp], %%rsp"
245 : [tmp]"=&r"(tmp), [tmp2]"=r"(tmp2)
246 : [val]"r"(memw), [stack_top]"r"(stack_top)
247 : "memory");
248 report("pop reg", tmp2 == memw);
249
250 asm volatile("mov %%rsp, %[tmp] \n\t"
251 "mov %[stack_top], %%rsp \n\t"
252 "push $1f \n\t"
253 "ret \n\t"
254 "2: jmp 2b \n\t"
255 "1: mov %[tmp], %%rsp"
256 : [tmp]"=&r"(tmp) : [stack_top]"r"(stack_top)
257 : "memory");
258 report("ret", 1);
Avi Kivity5269d6e2012-06-27 11:36:31 +0300259
260 stack_top[-1] = 0x778899;
261 asm volatile("mov %%rsp, %[tmp] \n\t"
262 "mov %%rbp, %[tmp3] \n\t"
263 "mov %[stack_top], %%rbp \n\t"
264 "leave \n\t"
265 "xchg %%rsp, %[tmp] \n\t"
266 "xchg %%rbp, %[tmp3]"
267 : [tmp]"=&r"(tmp), [tmp3]"=&r"(tmp3) : [stack_top]"r"(stack_top-1)
268 : "memory");
269 report("leave", tmp == (ulong)stack_top && tmp3 == 0x778899);
Avi Kivity7d36db32010-08-03 14:07:34 +0300270}
271
272void test_ljmp(void *mem)
273{
274 unsigned char *m = mem;
275 volatile int res = 1;
276
277 *(unsigned long**)m = &&jmpf;
278 asm volatile ("data16/mov %%cs, %0":"=m"(*(m + sizeof(unsigned long))));
279 asm volatile ("rex64/ljmp *%0"::"m"(*m));
280 res = 0;
281jmpf:
282 report("ljmp", res);
283}
284
285void test_incdecnotneg(void *mem)
286{
287 unsigned long *m = mem, v = 1234;
288 unsigned char *mb = mem, vb = 66;
289
290 *m = 0;
291
292 asm volatile ("incl %0":"+m"(*m));
293 report("incl", *m == 1);
294 asm volatile ("decl %0":"+m"(*m));
295 report("decl", *m == 0);
296 asm volatile ("incb %0":"+m"(*m));
297 report("incb", *m == 1);
298 asm volatile ("decb %0":"+m"(*m));
299 report("decb", *m == 0);
300
301 asm volatile ("lock incl %0":"+m"(*m));
302 report("lock incl", *m == 1);
303 asm volatile ("lock decl %0":"+m"(*m));
304 report("lock decl", *m == 0);
305 asm volatile ("lock incb %0":"+m"(*m));
306 report("lock incb", *m == 1);
307 asm volatile ("lock decb %0":"+m"(*m));
308 report("lock decb", *m == 0);
309
310 *m = v;
311
312 asm ("lock negq %0" : "+m"(*m)); v = -v;
313 report("lock negl", *m == v);
314 asm ("lock notq %0" : "+m"(*m)); v = ~v;
315 report("lock notl", *m == v);
316
317 *mb = vb;
318
319 asm ("lock negb %0" : "+m"(*mb)); vb = -vb;
320 report("lock negb", *mb == vb);
321 asm ("lock notb %0" : "+m"(*mb)); vb = ~vb;
322 report("lock notb", *mb == vb);
323}
324
325void test_smsw(void)
326{
327 char mem[16];
328 unsigned short msw, msw_orig, *pmsw;
329 int i, zero;
330
331 msw_orig = read_cr0();
332
333 asm("smsw %0" : "=r"(msw));
334 report("smsw (1)", msw == msw_orig);
335
336 memset(mem, 0, 16);
337 pmsw = (void *)mem;
338 asm("smsw %0" : "=m"(pmsw[4]));
339 zero = 1;
340 for (i = 0; i < 8; ++i)
341 if (i != 4 && pmsw[i])
342 zero = 0;
343 report("smsw (2)", msw == pmsw[4] && zero);
344}
345
346void test_lmsw(void)
347{
348 char mem[16];
349 unsigned short msw, *pmsw;
350 unsigned long cr0;
351
352 cr0 = read_cr0();
353
354 msw = cr0 ^ 8;
355 asm("lmsw %0" : : "r"(msw));
356 printf("before %lx after %lx\n", cr0, read_cr0());
357 report("lmsw (1)", (cr0 ^ read_cr0()) == 8);
358
359 pmsw = (void *)mem;
360 *pmsw = cr0;
361 asm("lmsw %0" : : "m"(*pmsw));
362 printf("before %lx after %lx\n", cr0, read_cr0());
363 report("lmsw (2)", cr0 == read_cr0());
364
365 /* lmsw can't clear cr0.pe */
366 msw = (cr0 & ~1ul) ^ 4; /* change EM to force trap */
367 asm("lmsw %0" : : "r"(msw));
368 report("lmsw (3)", (cr0 ^ read_cr0()) == 4 && (cr0 & 1));
369
370 /* back to normal */
371 msw = cr0;
372 asm("lmsw %0" : : "r"(msw));
373}
374
375void test_xchg(void *mem)
376{
377 unsigned long *memq = mem;
378 unsigned long rax;
379
380 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
381 "mov %%rax, (%[memq])\n\t"
382 "mov $0xfedcba9876543210, %%rax\n\t"
383 "xchg %%al, (%[memq])\n\t"
384 "mov %%rax, %[rax]\n\t"
385 : [rax]"=r"(rax)
386 : [memq]"r"(memq)
387 : "memory");
388 report("xchg reg, r/m (1)",
389 rax == 0xfedcba98765432ef && *memq == 0x123456789abcd10);
390
391 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
392 "mov %%rax, (%[memq])\n\t"
393 "mov $0xfedcba9876543210, %%rax\n\t"
394 "xchg %%ax, (%[memq])\n\t"
395 "mov %%rax, %[rax]\n\t"
396 : [rax]"=r"(rax)
397 : [memq]"r"(memq)
398 : "memory");
399 report("xchg reg, r/m (2)",
400 rax == 0xfedcba987654cdef && *memq == 0x123456789ab3210);
401
402 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
403 "mov %%rax, (%[memq])\n\t"
404 "mov $0xfedcba9876543210, %%rax\n\t"
405 "xchg %%eax, (%[memq])\n\t"
406 "mov %%rax, %[rax]\n\t"
407 : [rax]"=r"(rax)
408 : [memq]"r"(memq)
409 : "memory");
410 report("xchg reg, r/m (3)",
411 rax == 0x89abcdef && *memq == 0x123456776543210);
412
413 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
414 "mov %%rax, (%[memq])\n\t"
415 "mov $0xfedcba9876543210, %%rax\n\t"
416 "xchg %%rax, (%[memq])\n\t"
417 "mov %%rax, %[rax]\n\t"
418 : [rax]"=r"(rax)
419 : [memq]"r"(memq)
420 : "memory");
421 report("xchg reg, r/m (4)",
422 rax == 0x123456789abcdef && *memq == 0xfedcba9876543210);
423}
424
Wei Yongjun5647d552010-08-12 21:44:01 +0800425void test_xadd(void *mem)
426{
427 unsigned long *memq = mem;
428 unsigned long rax;
429
430 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
431 "mov %%rax, (%[memq])\n\t"
432 "mov $0xfedcba9876543210, %%rax\n\t"
433 "xadd %%al, (%[memq])\n\t"
434 "mov %%rax, %[rax]\n\t"
435 : [rax]"=r"(rax)
436 : [memq]"r"(memq)
437 : "memory");
438 report("xadd reg, r/m (1)",
439 rax == 0xfedcba98765432ef && *memq == 0x123456789abcdff);
440
441 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
442 "mov %%rax, (%[memq])\n\t"
443 "mov $0xfedcba9876543210, %%rax\n\t"
444 "xadd %%ax, (%[memq])\n\t"
445 "mov %%rax, %[rax]\n\t"
446 : [rax]"=r"(rax)
447 : [memq]"r"(memq)
448 : "memory");
449 report("xadd reg, r/m (2)",
450 rax == 0xfedcba987654cdef && *memq == 0x123456789abffff);
451
452 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
453 "mov %%rax, (%[memq])\n\t"
454 "mov $0xfedcba9876543210, %%rax\n\t"
455 "xadd %%eax, (%[memq])\n\t"
456 "mov %%rax, %[rax]\n\t"
457 : [rax]"=r"(rax)
458 : [memq]"r"(memq)
459 : "memory");
460 report("xadd reg, r/m (3)",
461 rax == 0x89abcdef && *memq == 0x1234567ffffffff);
462
463 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
464 "mov %%rax, (%[memq])\n\t"
465 "mov $0xfedcba9876543210, %%rax\n\t"
466 "xadd %%rax, (%[memq])\n\t"
467 "mov %%rax, %[rax]\n\t"
468 : [rax]"=r"(rax)
469 : [memq]"r"(memq)
470 : "memory");
471 report("xadd reg, r/m (4)",
472 rax == 0x123456789abcdef && *memq == 0xffffffffffffffff);
473}
474
Wei Yongjund4655ea2010-08-05 14:09:15 +0800475void test_btc(void *mem)
476{
477 unsigned int *a = mem;
478
479 memset(mem, 0, 3 * sizeof(unsigned int));
480
481 asm ("btcl $32, %0" :: "m"(a[0]) : "memory");
482 asm ("btcl $1, %0" :: "m"(a[1]) : "memory");
483 asm ("btcl %1, %0" :: "m"(a[0]), "r"(66) : "memory");
484 report("btcl imm8, r/m", a[0] == 1 && a[1] == 2 && a[2] == 4);
485
486 asm ("btcl %1, %0" :: "m"(a[3]), "r"(-1) : "memory");
487 report("btcl reg, r/m", a[0] == 1 && a[1] == 2 && a[2] == 0x80000004);
488}
489
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800490void test_bsfbsr(void *mem)
491{
Avi Kivity554de462011-11-28 15:09:34 +0200492 unsigned long rax, *memq = mem;
493 unsigned eax, *meml = mem;
494 unsigned short ax, *memw = mem;
495 unsigned char z;
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800496
Avi Kivity554de462011-11-28 15:09:34 +0200497 *memw = 0xc000;
498 asm("bsfw %[mem], %[a]" : [a]"=a"(ax) : [mem]"m"(*memw));
499 report("bsfw r/m, reg", ax == 14);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800500
Avi Kivity554de462011-11-28 15:09:34 +0200501 *meml = 0xc0000000;
502 asm("bsfl %[mem], %[a]" : [a]"=a"(eax) : [mem]"m"(*meml));
503 report("bsfl r/m, reg", eax == 30);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800504
Avi Kivity554de462011-11-28 15:09:34 +0200505 *memq = 0xc00000000000;
506 asm("bsfq %[mem], %[a]" : [a]"=a"(rax) : [mem]"m"(*memq));
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800507 report("bsfq r/m, reg", rax == 46);
508
Avi Kivity554de462011-11-28 15:09:34 +0200509 *memq = 0;
510 asm("bsfq %[mem], %[a]; setz %[z]"
511 : [a]"=a"(rax), [z]"=rm"(z) : [mem]"m"(*memq));
512 report("bsfq r/m, reg", z == 1);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800513
Avi Kivity554de462011-11-28 15:09:34 +0200514 *memw = 0xc000;
515 asm("bsrw %[mem], %[a]" : [a]"=a"(ax) : [mem]"m"(*memw));
516 report("bsrw r/m, reg", ax == 15);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800517
Avi Kivity554de462011-11-28 15:09:34 +0200518 *meml = 0xc0000000;
519 asm("bsrl %[mem], %[a]" : [a]"=a"(eax) : [mem]"m"(*meml));
520 report("bsrl r/m, reg", eax == 31);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800521
Avi Kivity554de462011-11-28 15:09:34 +0200522 *memq = 0xc00000000000;
523 asm("bsrq %[mem], %[a]" : [a]"=a"(rax) : [mem]"m"(*memq));
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800524 report("bsrq r/m, reg", rax == 47);
525
Avi Kivity554de462011-11-28 15:09:34 +0200526 *memq = 0;
527 asm("bsrq %[mem], %[a]; setz %[z]"
528 : [a]"=a"(rax), [z]"=rm"(z) : [mem]"m"(*memq));
529 report("bsrq r/m, reg", z == 1);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800530}
531
Avi Kivity51d65a32010-08-19 19:15:31 +0300532static void test_imul(ulong *mem)
533{
534 ulong a;
535
536 *mem = 51; a = 0x1234567812345678UL;
537 asm ("imulw %1, %%ax" : "+a"(a) : "m"(*mem));
538 report("imul ax, mem", a == 0x12345678123439e8);
539
540 *mem = 51; a = 0x1234567812345678UL;
541 asm ("imull %1, %%eax" : "+a"(a) : "m"(*mem));
542 report("imul eax, mem", a == 0xa06d39e8);
543
544 *mem = 51; a = 0x1234567812345678UL;
545 asm ("imulq %1, %%rax" : "+a"(a) : "m"(*mem));
546 report("imul rax, mem", a == 0xA06D39EBA06D39E8UL);
547
548 *mem = 0x1234567812345678UL; a = 0x8765432187654321L;
549 asm ("imulw $51, %1, %%ax" : "+a"(a) : "m"(*mem));
550 report("imul ax, mem, imm8", a == 0x87654321876539e8);
551
552 *mem = 0x1234567812345678UL;
553 asm ("imull $51, %1, %%eax" : "+a"(a) : "m"(*mem));
554 report("imul eax, mem, imm8", a == 0xa06d39e8);
555
556 *mem = 0x1234567812345678UL;
557 asm ("imulq $51, %1, %%rax" : "+a"(a) : "m"(*mem));
558 report("imul rax, mem, imm8", a == 0xA06D39EBA06D39E8UL);
559
560 *mem = 0x1234567812345678UL; a = 0x8765432187654321L;
561 asm ("imulw $311, %1, %%ax" : "+a"(a) : "m"(*mem));
562 report("imul ax, mem, imm", a == 0x8765432187650bc8);
563
564 *mem = 0x1234567812345678UL;
565 asm ("imull $311, %1, %%eax" : "+a"(a) : "m"(*mem));
566 report("imul eax, mem, imm", a == 0x1d950bc8);
567
568 *mem = 0x1234567812345678UL;
569 asm ("imulq $311, %1, %%rax" : "+a"(a) : "m"(*mem));
570 report("imul rax, mem, imm", a == 0x1D950BDE1D950BC8L);
571}
572
Avi Kivityf12d86b2010-08-24 14:01:11 +0300573static void test_div(long *mem)
574{
575 long a, d;
576 u8 ex = 1;
577
578 *mem = 0; a = 1; d = 2;
579 asm (ASM_TRY("1f") "divq %3; movb $0, %2; 1:"
580 : "+a"(a), "+d"(d), "+q"(ex) : "m"(*mem));
581 report("divq (fault)", a == 1 && d == 2 && ex);
582
583 *mem = 987654321098765UL; a = 123456789012345UL; d = 123456789012345UL;
584 asm (ASM_TRY("1f") "divq %3; movb $0, %2; 1:"
585 : "+a"(a), "+d"(d), "+q"(ex) : "m"(*mem));
586 report("divq (1)",
587 a == 0x1ffffffb1b963b33ul && d == 0x273ba4384ede2ul && !ex);
588}
589
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200590typedef unsigned __attribute__((vector_size(16))) sse128;
591
592typedef union {
593 sse128 sse;
594 unsigned u[4];
595} sse_union;
596
597static bool sseeq(sse_union *v1, sse_union *v2)
598{
599 bool ok = true;
600 int i;
601
602 for (i = 0; i < 4; ++i) {
603 ok &= v1->u[i] == v2->u[i];
604 }
605
606 return ok;
607}
608
609static void test_sse(sse_union *mem)
610{
611 sse_union v;
612
613 write_cr0(read_cr0() & ~6); /* EM, TS */
614 write_cr4(read_cr4() | 0x200); /* OSFXSR */
615 v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
616 asm("movdqu %1, %0" : "=m"(*mem) : "x"(v.sse));
617 report("movdqu (read)", sseeq(&v, mem));
618 mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
619 asm("movdqu %1, %0" : "=x"(v.sse) : "m"(*mem));
620 report("movdqu (write)", sseeq(mem, &v));
621}
622
Avi Kivity35870822012-03-22 12:58:06 +0200623static void test_mmx(uint64_t *mem)
624{
625 uint64_t v;
626
627 write_cr0(read_cr0() & ~6); /* EM, TS */
628 asm volatile("fninit");
629 v = 0x0102030405060708ULL;
630 asm("movq %1, %0" : "=m"(*mem) : "y"(v));
631 report("movq (mmx, read)", v == *mem);
632 *mem = 0x8070605040302010ull;
633 asm("movq %1, %0" : "=y"(v) : "m"(*mem));
634 report("movq (mmx, write)", v == *mem);
635}
636
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300637static void test_rip_relative(unsigned *mem, char *insn_ram)
638{
639 /* movb $1, mem+2(%rip) */
640 insn_ram[0] = 0xc6;
641 insn_ram[1] = 0x05;
642 *(unsigned *)&insn_ram[2] = 2 + (char *)mem - (insn_ram + 7);
643 insn_ram[6] = 0x01;
644 /* ret */
645 insn_ram[7] = 0xc3;
646
647 *mem = 0;
648 asm("callq *%1" : "+m"(*mem) : "r"(insn_ram));
649 report("movb $imm, 0(%rip)", *mem == 0x10000);
650}
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200651
Avi Kivityb212fcd2011-09-13 11:15:16 +0300652static void test_shld_shrd(u32 *mem)
653{
654 *mem = 0x12345678;
655 asm("shld %2, %1, %0" : "+m"(*mem) : "r"(0xaaaaaaaaU), "c"((u8)3));
656 report("shld (cl)", *mem == ((0x12345678 << 3) | 5));
657 *mem = 0x12345678;
658 asm("shrd %2, %1, %0" : "+m"(*mem) : "r"(0x55555555U), "c"((u8)3));
659 report("shrd (cl)", *mem == ((0x12345678 >> 3) | (5u << 29)));
660}
661
Avi Kivityd7143f32012-03-25 15:49:05 +0200662static void advance_rip_by_3_and_note_exception(struct ex_regs *regs)
663{
664 ++exceptions;
665 regs->rip += 3;
666}
667
668static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
669 uint8_t *alt_insn_page, void *insn_ram)
670{
671 uint16_t fcw = 0; // all exceptions unmasked
672 ulong *cr3 = (ulong *)read_cr3();
673
674 write_cr0(read_cr0() & ~6); // TS, EM
675 // Place a trapping instruction in the page to trigger a VMEXIT
676 insn_page[0] = 0x89; // mov %eax, (%rax)
677 insn_page[1] = 0x00;
678 insn_page[2] = 0x90; // nop
679 insn_page[3] = 0xc3; // ret
680 // Place the instruction we want the hypervisor to see in the alternate page
681 alt_insn_page[0] = 0x0f; // movq %mm0, (%rax)
682 alt_insn_page[1] = 0x7f;
683 alt_insn_page[2] = 0x00;
684 alt_insn_page[3] = 0xc3; // ret
685
686 exceptions = 0;
687 handle_exception(MF_VECTOR, advance_rip_by_3_and_note_exception);
688
689 // Load the code TLB with insn_page, but point the page tables at
690 // alt_insn_page (and keep the data TLB clear, for AMD decode assist).
691 // This will make the CPU trap on the insn_page instruction but the
692 // hypervisor will see alt_insn_page.
693 install_page(cr3, virt_to_phys(insn_page), insn_ram);
694 asm volatile("fninit; fldcw %0" : : "m"(fcw));
695 asm volatile("fldz; fldz; fdivp"); // generate exception
696 invlpg(insn_ram);
697 // Load code TLB
698 asm volatile("call *%0" : : "r"(insn_ram + 3));
699 install_page(cr3, virt_to_phys(alt_insn_page), insn_ram);
700 // Trap, let hypervisor emulate at alt_insn_page
701 asm volatile("call *%0" : : "r"(insn_ram), "a"(mem));
702 // exit MMX mode
703 asm volatile("fnclex; emms");
704 report("movq mmx generates #MF", exceptions == 1);
705 handle_exception(MF_VECTOR, 0);
706}
707
Avi Kivityec278ce2012-04-18 19:27:00 +0300708static void test_crosspage_mmio(volatile uint8_t *mem)
709{
710 volatile uint16_t w, *pw;
711
712 pw = (volatile uint16_t *)&mem[4095];
713 mem[4095] = 0x99;
714 mem[4096] = 0x77;
715 asm volatile("mov %1, %0" : "=r"(w) : "m"(*pw) : "memory");
716 report("cross-page mmio read", w == 0x7799);
717 asm volatile("mov %1, %0" : "=m"(*pw) : "r"((uint16_t)0x88aa));
718 report("cross-page mmio write", mem[4095] == 0xaa && mem[4096] == 0x88);
719}
720
Avi Kivity7d36db32010-08-03 14:07:34 +0300721int main()
722{
723 void *mem;
Avi Kivityd7143f32012-03-25 15:49:05 +0200724 void *insn_page, *alt_insn_page;
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300725 void *insn_ram;
Avi Kivity7d36db32010-08-03 14:07:34 +0300726 unsigned long t1, t2;
727
728 setup_vm();
Avi Kivitya526e202010-08-24 14:01:10 +0300729 setup_idt();
Avi Kivityec278ce2012-04-18 19:27:00 +0300730 mem = alloc_vpages(2);
731 install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem);
732 // install the page twice to test cross-page mmio
733 install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem + 4096);
Avi Kivityd7143f32012-03-25 15:49:05 +0200734 insn_page = alloc_page();
735 alt_insn_page = alloc_page();
736 insn_ram = vmap(virt_to_phys(insn_page), 4096);
Avi Kivity7d36db32010-08-03 14:07:34 +0300737
738 // test mov reg, r/m and mov r/m, reg
739 t1 = 0x123456789abcdef;
740 asm volatile("mov %[t1], (%[mem]) \n\t"
741 "mov (%[mem]), %[t2]"
742 : [t2]"=r"(t2)
743 : [t1]"r"(t1), [mem]"r"(mem)
744 : "memory");
745 report("mov reg, r/m (1)", t2 == 0x123456789abcdef);
746
747 test_cmps(mem);
Avi Kivity80a4ea72010-08-17 17:44:14 +0300748 test_scas(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300749
750 test_push(mem);
751 test_pop(mem);
752
753 test_xchg(mem);
Wei Yongjun5647d552010-08-12 21:44:01 +0800754 test_xadd(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300755
756 test_cr8();
757
758 test_smsw();
759 test_lmsw();
760 test_ljmp(mem);
761 test_stringio();
762 test_incdecnotneg(mem);
Wei Yongjund4655ea2010-08-05 14:09:15 +0800763 test_btc(mem);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800764 test_bsfbsr(mem);
Avi Kivity51d65a32010-08-19 19:15:31 +0300765 test_imul(mem);
Avi Kivityf12d86b2010-08-24 14:01:11 +0300766 test_div(mem);
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200767 test_sse(mem);
Avi Kivity35870822012-03-22 12:58:06 +0200768 test_mmx(mem);
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300769 test_rip_relative(mem, insn_ram);
Avi Kivityb212fcd2011-09-13 11:15:16 +0300770 test_shld_shrd(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300771
Avi Kivityd7143f32012-03-25 15:49:05 +0200772 test_mmx_movq_mf(mem, insn_page, alt_insn_page, insn_ram);
773
Avi Kivityec278ce2012-04-18 19:27:00 +0300774 test_crosspage_mmio(mem);
775
Avi Kivity7d36db32010-08-03 14:07:34 +0300776 printf("\nSUMMARY: %d tests, %d failures\n", tests, fails);
777 return fails ? 1 : 0;
778}