blob: 0735405409fc2eb451573243c28bd271e79937ca [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 Kivity28f04f22012-06-27 11:36:38 +0300216 unsigned long tmp, tmp3, rsp, rbp;
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 Kivity28f04f22012-06-27 11:36:38 +0300270
271 rbp = 0xaa55aa55bb66bb66ULL;
272 rsp = (unsigned long)stack_top;
273 asm volatile("xchg %%rsp, %[rsp] \n\t"
274 "xchg %%rbp, %[rbp] \n\t"
275 "enter $0x1238, $0 \n\t"
276 "xchg %%rsp, %[rsp] \n\t"
277 "xchg %%rbp, %[rbp]"
278 : [rsp]"+a"(rsp), [rbp]"+b"(rbp) : : "memory");
279 report("enter",
280 rsp == (unsigned long)stack_top - 8 - 0x1238
281 && rbp == (unsigned long)stack_top - 8
282 && stack_top[-1] == 0xaa55aa55bb66bb66ULL);
Avi Kivity7d36db32010-08-03 14:07:34 +0300283}
284
285void test_ljmp(void *mem)
286{
287 unsigned char *m = mem;
288 volatile int res = 1;
289
290 *(unsigned long**)m = &&jmpf;
291 asm volatile ("data16/mov %%cs, %0":"=m"(*(m + sizeof(unsigned long))));
292 asm volatile ("rex64/ljmp *%0"::"m"(*m));
293 res = 0;
294jmpf:
295 report("ljmp", res);
296}
297
298void test_incdecnotneg(void *mem)
299{
300 unsigned long *m = mem, v = 1234;
301 unsigned char *mb = mem, vb = 66;
302
303 *m = 0;
304
305 asm volatile ("incl %0":"+m"(*m));
306 report("incl", *m == 1);
307 asm volatile ("decl %0":"+m"(*m));
308 report("decl", *m == 0);
309 asm volatile ("incb %0":"+m"(*m));
310 report("incb", *m == 1);
311 asm volatile ("decb %0":"+m"(*m));
312 report("decb", *m == 0);
313
314 asm volatile ("lock incl %0":"+m"(*m));
315 report("lock incl", *m == 1);
316 asm volatile ("lock decl %0":"+m"(*m));
317 report("lock decl", *m == 0);
318 asm volatile ("lock incb %0":"+m"(*m));
319 report("lock incb", *m == 1);
320 asm volatile ("lock decb %0":"+m"(*m));
321 report("lock decb", *m == 0);
322
323 *m = v;
324
325 asm ("lock negq %0" : "+m"(*m)); v = -v;
326 report("lock negl", *m == v);
327 asm ("lock notq %0" : "+m"(*m)); v = ~v;
328 report("lock notl", *m == v);
329
330 *mb = vb;
331
332 asm ("lock negb %0" : "+m"(*mb)); vb = -vb;
333 report("lock negb", *mb == vb);
334 asm ("lock notb %0" : "+m"(*mb)); vb = ~vb;
335 report("lock notb", *mb == vb);
336}
337
338void test_smsw(void)
339{
340 char mem[16];
341 unsigned short msw, msw_orig, *pmsw;
342 int i, zero;
343
344 msw_orig = read_cr0();
345
346 asm("smsw %0" : "=r"(msw));
347 report("smsw (1)", msw == msw_orig);
348
349 memset(mem, 0, 16);
350 pmsw = (void *)mem;
351 asm("smsw %0" : "=m"(pmsw[4]));
352 zero = 1;
353 for (i = 0; i < 8; ++i)
354 if (i != 4 && pmsw[i])
355 zero = 0;
356 report("smsw (2)", msw == pmsw[4] && zero);
357}
358
359void test_lmsw(void)
360{
361 char mem[16];
362 unsigned short msw, *pmsw;
363 unsigned long cr0;
364
365 cr0 = read_cr0();
366
367 msw = cr0 ^ 8;
368 asm("lmsw %0" : : "r"(msw));
369 printf("before %lx after %lx\n", cr0, read_cr0());
370 report("lmsw (1)", (cr0 ^ read_cr0()) == 8);
371
372 pmsw = (void *)mem;
373 *pmsw = cr0;
374 asm("lmsw %0" : : "m"(*pmsw));
375 printf("before %lx after %lx\n", cr0, read_cr0());
376 report("lmsw (2)", cr0 == read_cr0());
377
378 /* lmsw can't clear cr0.pe */
379 msw = (cr0 & ~1ul) ^ 4; /* change EM to force trap */
380 asm("lmsw %0" : : "r"(msw));
381 report("lmsw (3)", (cr0 ^ read_cr0()) == 4 && (cr0 & 1));
382
383 /* back to normal */
384 msw = cr0;
385 asm("lmsw %0" : : "r"(msw));
386}
387
388void test_xchg(void *mem)
389{
390 unsigned long *memq = mem;
391 unsigned long rax;
392
393 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
394 "mov %%rax, (%[memq])\n\t"
395 "mov $0xfedcba9876543210, %%rax\n\t"
396 "xchg %%al, (%[memq])\n\t"
397 "mov %%rax, %[rax]\n\t"
398 : [rax]"=r"(rax)
399 : [memq]"r"(memq)
400 : "memory");
401 report("xchg reg, r/m (1)",
402 rax == 0xfedcba98765432ef && *memq == 0x123456789abcd10);
403
404 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
405 "mov %%rax, (%[memq])\n\t"
406 "mov $0xfedcba9876543210, %%rax\n\t"
407 "xchg %%ax, (%[memq])\n\t"
408 "mov %%rax, %[rax]\n\t"
409 : [rax]"=r"(rax)
410 : [memq]"r"(memq)
411 : "memory");
412 report("xchg reg, r/m (2)",
413 rax == 0xfedcba987654cdef && *memq == 0x123456789ab3210);
414
415 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
416 "mov %%rax, (%[memq])\n\t"
417 "mov $0xfedcba9876543210, %%rax\n\t"
418 "xchg %%eax, (%[memq])\n\t"
419 "mov %%rax, %[rax]\n\t"
420 : [rax]"=r"(rax)
421 : [memq]"r"(memq)
422 : "memory");
423 report("xchg reg, r/m (3)",
424 rax == 0x89abcdef && *memq == 0x123456776543210);
425
426 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
427 "mov %%rax, (%[memq])\n\t"
428 "mov $0xfedcba9876543210, %%rax\n\t"
429 "xchg %%rax, (%[memq])\n\t"
430 "mov %%rax, %[rax]\n\t"
431 : [rax]"=r"(rax)
432 : [memq]"r"(memq)
433 : "memory");
434 report("xchg reg, r/m (4)",
435 rax == 0x123456789abcdef && *memq == 0xfedcba9876543210);
436}
437
Wei Yongjun5647d552010-08-12 21:44:01 +0800438void test_xadd(void *mem)
439{
440 unsigned long *memq = mem;
441 unsigned long rax;
442
443 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
444 "mov %%rax, (%[memq])\n\t"
445 "mov $0xfedcba9876543210, %%rax\n\t"
446 "xadd %%al, (%[memq])\n\t"
447 "mov %%rax, %[rax]\n\t"
448 : [rax]"=r"(rax)
449 : [memq]"r"(memq)
450 : "memory");
451 report("xadd reg, r/m (1)",
452 rax == 0xfedcba98765432ef && *memq == 0x123456789abcdff);
453
454 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
455 "mov %%rax, (%[memq])\n\t"
456 "mov $0xfedcba9876543210, %%rax\n\t"
457 "xadd %%ax, (%[memq])\n\t"
458 "mov %%rax, %[rax]\n\t"
459 : [rax]"=r"(rax)
460 : [memq]"r"(memq)
461 : "memory");
462 report("xadd reg, r/m (2)",
463 rax == 0xfedcba987654cdef && *memq == 0x123456789abffff);
464
465 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
466 "mov %%rax, (%[memq])\n\t"
467 "mov $0xfedcba9876543210, %%rax\n\t"
468 "xadd %%eax, (%[memq])\n\t"
469 "mov %%rax, %[rax]\n\t"
470 : [rax]"=r"(rax)
471 : [memq]"r"(memq)
472 : "memory");
473 report("xadd reg, r/m (3)",
474 rax == 0x89abcdef && *memq == 0x1234567ffffffff);
475
476 asm volatile("mov $0x123456789abcdef, %%rax\n\t"
477 "mov %%rax, (%[memq])\n\t"
478 "mov $0xfedcba9876543210, %%rax\n\t"
479 "xadd %%rax, (%[memq])\n\t"
480 "mov %%rax, %[rax]\n\t"
481 : [rax]"=r"(rax)
482 : [memq]"r"(memq)
483 : "memory");
484 report("xadd reg, r/m (4)",
485 rax == 0x123456789abcdef && *memq == 0xffffffffffffffff);
486}
487
Wei Yongjund4655ea2010-08-05 14:09:15 +0800488void test_btc(void *mem)
489{
490 unsigned int *a = mem;
491
492 memset(mem, 0, 3 * sizeof(unsigned int));
493
494 asm ("btcl $32, %0" :: "m"(a[0]) : "memory");
495 asm ("btcl $1, %0" :: "m"(a[1]) : "memory");
496 asm ("btcl %1, %0" :: "m"(a[0]), "r"(66) : "memory");
497 report("btcl imm8, r/m", a[0] == 1 && a[1] == 2 && a[2] == 4);
498
499 asm ("btcl %1, %0" :: "m"(a[3]), "r"(-1) : "memory");
500 report("btcl reg, r/m", a[0] == 1 && a[1] == 2 && a[2] == 0x80000004);
501}
502
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800503void test_bsfbsr(void *mem)
504{
Avi Kivity554de462011-11-28 15:09:34 +0200505 unsigned long rax, *memq = mem;
506 unsigned eax, *meml = mem;
507 unsigned short ax, *memw = mem;
508 unsigned char z;
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800509
Avi Kivity554de462011-11-28 15:09:34 +0200510 *memw = 0xc000;
511 asm("bsfw %[mem], %[a]" : [a]"=a"(ax) : [mem]"m"(*memw));
512 report("bsfw r/m, reg", ax == 14);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800513
Avi Kivity554de462011-11-28 15:09:34 +0200514 *meml = 0xc0000000;
515 asm("bsfl %[mem], %[a]" : [a]"=a"(eax) : [mem]"m"(*meml));
516 report("bsfl r/m, reg", eax == 30);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800517
Avi Kivity554de462011-11-28 15:09:34 +0200518 *memq = 0xc00000000000;
519 asm("bsfq %[mem], %[a]" : [a]"=a"(rax) : [mem]"m"(*memq));
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800520 report("bsfq r/m, reg", rax == 46);
521
Avi Kivity554de462011-11-28 15:09:34 +0200522 *memq = 0;
523 asm("bsfq %[mem], %[a]; setz %[z]"
524 : [a]"=a"(rax), [z]"=rm"(z) : [mem]"m"(*memq));
525 report("bsfq r/m, reg", z == 1);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800526
Avi Kivity554de462011-11-28 15:09:34 +0200527 *memw = 0xc000;
528 asm("bsrw %[mem], %[a]" : [a]"=a"(ax) : [mem]"m"(*memw));
529 report("bsrw r/m, reg", ax == 15);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800530
Avi Kivity554de462011-11-28 15:09:34 +0200531 *meml = 0xc0000000;
532 asm("bsrl %[mem], %[a]" : [a]"=a"(eax) : [mem]"m"(*meml));
533 report("bsrl r/m, reg", eax == 31);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800534
Avi Kivity554de462011-11-28 15:09:34 +0200535 *memq = 0xc00000000000;
536 asm("bsrq %[mem], %[a]" : [a]"=a"(rax) : [mem]"m"(*memq));
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800537 report("bsrq r/m, reg", rax == 47);
538
Avi Kivity554de462011-11-28 15:09:34 +0200539 *memq = 0;
540 asm("bsrq %[mem], %[a]; setz %[z]"
541 : [a]"=a"(rax), [z]"=rm"(z) : [mem]"m"(*memq));
542 report("bsrq r/m, reg", z == 1);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800543}
544
Avi Kivity51d65a32010-08-19 19:15:31 +0300545static void test_imul(ulong *mem)
546{
547 ulong a;
548
549 *mem = 51; a = 0x1234567812345678UL;
550 asm ("imulw %1, %%ax" : "+a"(a) : "m"(*mem));
551 report("imul ax, mem", a == 0x12345678123439e8);
552
553 *mem = 51; a = 0x1234567812345678UL;
554 asm ("imull %1, %%eax" : "+a"(a) : "m"(*mem));
555 report("imul eax, mem", a == 0xa06d39e8);
556
557 *mem = 51; a = 0x1234567812345678UL;
558 asm ("imulq %1, %%rax" : "+a"(a) : "m"(*mem));
559 report("imul rax, mem", a == 0xA06D39EBA06D39E8UL);
560
561 *mem = 0x1234567812345678UL; a = 0x8765432187654321L;
562 asm ("imulw $51, %1, %%ax" : "+a"(a) : "m"(*mem));
563 report("imul ax, mem, imm8", a == 0x87654321876539e8);
564
565 *mem = 0x1234567812345678UL;
566 asm ("imull $51, %1, %%eax" : "+a"(a) : "m"(*mem));
567 report("imul eax, mem, imm8", a == 0xa06d39e8);
568
569 *mem = 0x1234567812345678UL;
570 asm ("imulq $51, %1, %%rax" : "+a"(a) : "m"(*mem));
571 report("imul rax, mem, imm8", a == 0xA06D39EBA06D39E8UL);
572
573 *mem = 0x1234567812345678UL; a = 0x8765432187654321L;
574 asm ("imulw $311, %1, %%ax" : "+a"(a) : "m"(*mem));
575 report("imul ax, mem, imm", a == 0x8765432187650bc8);
576
577 *mem = 0x1234567812345678UL;
578 asm ("imull $311, %1, %%eax" : "+a"(a) : "m"(*mem));
579 report("imul eax, mem, imm", a == 0x1d950bc8);
580
581 *mem = 0x1234567812345678UL;
582 asm ("imulq $311, %1, %%rax" : "+a"(a) : "m"(*mem));
583 report("imul rax, mem, imm", a == 0x1D950BDE1D950BC8L);
584}
585
Avi Kivityf12d86b2010-08-24 14:01:11 +0300586static void test_div(long *mem)
587{
588 long a, d;
589 u8 ex = 1;
590
591 *mem = 0; a = 1; d = 2;
592 asm (ASM_TRY("1f") "divq %3; movb $0, %2; 1:"
593 : "+a"(a), "+d"(d), "+q"(ex) : "m"(*mem));
594 report("divq (fault)", a == 1 && d == 2 && ex);
595
596 *mem = 987654321098765UL; a = 123456789012345UL; d = 123456789012345UL;
597 asm (ASM_TRY("1f") "divq %3; movb $0, %2; 1:"
598 : "+a"(a), "+d"(d), "+q"(ex) : "m"(*mem));
599 report("divq (1)",
600 a == 0x1ffffffb1b963b33ul && d == 0x273ba4384ede2ul && !ex);
601}
602
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200603typedef unsigned __attribute__((vector_size(16))) sse128;
604
605typedef union {
606 sse128 sse;
607 unsigned u[4];
608} sse_union;
609
610static bool sseeq(sse_union *v1, sse_union *v2)
611{
612 bool ok = true;
613 int i;
614
615 for (i = 0; i < 4; ++i) {
616 ok &= v1->u[i] == v2->u[i];
617 }
618
619 return ok;
620}
621
622static void test_sse(sse_union *mem)
623{
624 sse_union v;
625
626 write_cr0(read_cr0() & ~6); /* EM, TS */
627 write_cr4(read_cr4() | 0x200); /* OSFXSR */
628 v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
629 asm("movdqu %1, %0" : "=m"(*mem) : "x"(v.sse));
630 report("movdqu (read)", sseeq(&v, mem));
631 mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
632 asm("movdqu %1, %0" : "=x"(v.sse) : "m"(*mem));
633 report("movdqu (write)", sseeq(mem, &v));
634}
635
Avi Kivity35870822012-03-22 12:58:06 +0200636static void test_mmx(uint64_t *mem)
637{
638 uint64_t v;
639
640 write_cr0(read_cr0() & ~6); /* EM, TS */
641 asm volatile("fninit");
642 v = 0x0102030405060708ULL;
643 asm("movq %1, %0" : "=m"(*mem) : "y"(v));
644 report("movq (mmx, read)", v == *mem);
645 *mem = 0x8070605040302010ull;
646 asm("movq %1, %0" : "=y"(v) : "m"(*mem));
647 report("movq (mmx, write)", v == *mem);
648}
649
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300650static void test_rip_relative(unsigned *mem, char *insn_ram)
651{
652 /* movb $1, mem+2(%rip) */
653 insn_ram[0] = 0xc6;
654 insn_ram[1] = 0x05;
655 *(unsigned *)&insn_ram[2] = 2 + (char *)mem - (insn_ram + 7);
656 insn_ram[6] = 0x01;
657 /* ret */
658 insn_ram[7] = 0xc3;
659
660 *mem = 0;
661 asm("callq *%1" : "+m"(*mem) : "r"(insn_ram));
662 report("movb $imm, 0(%rip)", *mem == 0x10000);
663}
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200664
Avi Kivityb212fcd2011-09-13 11:15:16 +0300665static void test_shld_shrd(u32 *mem)
666{
667 *mem = 0x12345678;
668 asm("shld %2, %1, %0" : "+m"(*mem) : "r"(0xaaaaaaaaU), "c"((u8)3));
669 report("shld (cl)", *mem == ((0x12345678 << 3) | 5));
670 *mem = 0x12345678;
671 asm("shrd %2, %1, %0" : "+m"(*mem) : "r"(0x55555555U), "c"((u8)3));
672 report("shrd (cl)", *mem == ((0x12345678 >> 3) | (5u << 29)));
673}
674
Avi Kivityd7143f32012-03-25 15:49:05 +0200675static void advance_rip_by_3_and_note_exception(struct ex_regs *regs)
676{
677 ++exceptions;
678 regs->rip += 3;
679}
680
681static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
682 uint8_t *alt_insn_page, void *insn_ram)
683{
684 uint16_t fcw = 0; // all exceptions unmasked
685 ulong *cr3 = (ulong *)read_cr3();
686
687 write_cr0(read_cr0() & ~6); // TS, EM
688 // Place a trapping instruction in the page to trigger a VMEXIT
689 insn_page[0] = 0x89; // mov %eax, (%rax)
690 insn_page[1] = 0x00;
691 insn_page[2] = 0x90; // nop
692 insn_page[3] = 0xc3; // ret
693 // Place the instruction we want the hypervisor to see in the alternate page
694 alt_insn_page[0] = 0x0f; // movq %mm0, (%rax)
695 alt_insn_page[1] = 0x7f;
696 alt_insn_page[2] = 0x00;
697 alt_insn_page[3] = 0xc3; // ret
698
699 exceptions = 0;
700 handle_exception(MF_VECTOR, advance_rip_by_3_and_note_exception);
701
702 // Load the code TLB with insn_page, but point the page tables at
703 // alt_insn_page (and keep the data TLB clear, for AMD decode assist).
704 // This will make the CPU trap on the insn_page instruction but the
705 // hypervisor will see alt_insn_page.
706 install_page(cr3, virt_to_phys(insn_page), insn_ram);
707 asm volatile("fninit; fldcw %0" : : "m"(fcw));
708 asm volatile("fldz; fldz; fdivp"); // generate exception
709 invlpg(insn_ram);
710 // Load code TLB
711 asm volatile("call *%0" : : "r"(insn_ram + 3));
712 install_page(cr3, virt_to_phys(alt_insn_page), insn_ram);
713 // Trap, let hypervisor emulate at alt_insn_page
714 asm volatile("call *%0" : : "r"(insn_ram), "a"(mem));
715 // exit MMX mode
716 asm volatile("fnclex; emms");
717 report("movq mmx generates #MF", exceptions == 1);
718 handle_exception(MF_VECTOR, 0);
719}
720
Avi Kivityec278ce2012-04-18 19:27:00 +0300721static void test_crosspage_mmio(volatile uint8_t *mem)
722{
723 volatile uint16_t w, *pw;
724
725 pw = (volatile uint16_t *)&mem[4095];
726 mem[4095] = 0x99;
727 mem[4096] = 0x77;
728 asm volatile("mov %1, %0" : "=r"(w) : "m"(*pw) : "memory");
729 report("cross-page mmio read", w == 0x7799);
730 asm volatile("mov %1, %0" : "=m"(*pw) : "r"((uint16_t)0x88aa));
731 report("cross-page mmio write", mem[4095] == 0xaa && mem[4096] == 0x88);
732}
733
Xiao Guangronga19c7db2012-10-19 15:39:08 +0800734static void test_string_io_mmio(volatile uint8_t *mem)
735{
736 /* Cross MMIO pages.*/
737 volatile uint8_t *mmio = mem + 4032;
738
739 asm volatile("outw %%ax, %%dx \n\t" : : "a"(0x9999), "d"(TESTDEV_IO_PORT));
740
741 asm volatile ("cld; rep insb" : : "d" (TESTDEV_IO_PORT), "D" (mmio), "c" (1024));
742
743 report("string_io_mmio", mmio[1023] == 0x99);
744}
745
Avi Kivity47c14612012-06-27 11:36:32 +0300746static void test_lgdt_lidt(volatile uint8_t *mem)
747{
748 struct descriptor_table_ptr orig, fresh = {};
749
750 sgdt(&orig);
751 *(struct descriptor_table_ptr *)mem = (struct descriptor_table_ptr) {
752 .limit = 0xf234,
753 .base = 0x12345678abcd,
754 };
755 cli();
756 asm volatile("lgdt %0" : : "m"(*(struct descriptor_table_ptr *)mem));
757 sgdt(&fresh);
758 lgdt(&orig);
759 sti();
760 report("lgdt (long address)", orig.limit == fresh.limit && orig.base == fresh.base);
761
762 sidt(&orig);
763 *(struct descriptor_table_ptr *)mem = (struct descriptor_table_ptr) {
764 .limit = 0x432f,
765 .base = 0xdbca87654321,
766 };
767 cli();
768 asm volatile("lidt %0" : : "m"(*(struct descriptor_table_ptr *)mem));
769 sidt(&fresh);
770 lidt(&orig);
771 sti();
772 report("lidt (long address)", orig.limit == fresh.limit && orig.base == fresh.base);
773}
774
Avi Kivityfc2f8802012-06-27 11:36:33 +0300775static void ss_bad_rpl(struct ex_regs *regs)
776{
777 extern char ss_bad_rpl_cont;
778
779 ++exceptions;
780 regs->rip = (ulong)&ss_bad_rpl_cont;
781}
782
783static void test_sreg(volatile uint16_t *mem)
784{
785 u16 ss = read_ss();
786
787 // check for null segment load
788 *mem = 0;
789 asm volatile("mov %0, %%ss" : : "m"(*mem));
790 report("mov null, %ss", read_ss() == 0);
791
792 // check for exception when ss.rpl != cpl on null segment load
793 exceptions = 0;
794 handle_exception(GP_VECTOR, ss_bad_rpl);
795 *mem = 3;
796 asm volatile("mov %0, %%ss; ss_bad_rpl_cont:" : : "m"(*mem));
797 report("mov null, %ss (with ss.rpl != cpl)", exceptions == 1 && read_ss() == 0);
798 handle_exception(GP_VECTOR, 0);
799 write_ss(ss);
800}
801
Avi Kivitycb615a42012-06-27 11:36:40 +0300802static void test_lldt(volatile uint16_t *mem)
803{
804 u64 gdt[] = { 0, 0x0000f82000000ffffull /* ldt descriptor */ };
805 struct descriptor_table_ptr gdt_ptr = { .limit = 0xffff, .base = (ulong)&gdt };
806 struct descriptor_table_ptr orig_gdt;
807
808 cli();
809 sgdt(&orig_gdt);
810 lgdt(&gdt_ptr);
811 *mem = 0x8;
812 asm volatile("lldt %0" : : "m"(*mem));
813 lgdt(&orig_gdt);
814 sti();
815 report("lldt", sldt() == *mem);
816}
817
Avi Kivity58a9d812012-06-27 11:36:41 +0300818static void test_ltr(volatile uint16_t *mem)
819{
820 struct descriptor_table_ptr gdt_ptr;
821 uint64_t *gdt, *trp;
822 uint16_t tr = str();
823 uint64_t busy_mask = (uint64_t)1 << 41;
824
825 sgdt(&gdt_ptr);
826 gdt = (uint64_t *)gdt_ptr.base;
827 trp = &gdt[tr >> 3];
828 *trp &= ~busy_mask;
829 *mem = tr;
830 asm volatile("ltr %0" : : "m"(*mem) : "memory");
831 report("ltr", str() == tr && (*trp & busy_mask));
832}
833
Avi Kivity7d36db32010-08-03 14:07:34 +0300834int main()
835{
836 void *mem;
Avi Kivityd7143f32012-03-25 15:49:05 +0200837 void *insn_page, *alt_insn_page;
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300838 void *insn_ram;
Avi Kivity7d36db32010-08-03 14:07:34 +0300839 unsigned long t1, t2;
840
841 setup_vm();
Avi Kivitya526e202010-08-24 14:01:10 +0300842 setup_idt();
Avi Kivityec278ce2012-04-18 19:27:00 +0300843 mem = alloc_vpages(2);
844 install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem);
845 // install the page twice to test cross-page mmio
846 install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem + 4096);
Avi Kivityd7143f32012-03-25 15:49:05 +0200847 insn_page = alloc_page();
848 alt_insn_page = alloc_page();
849 insn_ram = vmap(virt_to_phys(insn_page), 4096);
Avi Kivity7d36db32010-08-03 14:07:34 +0300850
851 // test mov reg, r/m and mov r/m, reg
852 t1 = 0x123456789abcdef;
853 asm volatile("mov %[t1], (%[mem]) \n\t"
854 "mov (%[mem]), %[t2]"
855 : [t2]"=r"(t2)
856 : [t1]"r"(t1), [mem]"r"(mem)
857 : "memory");
858 report("mov reg, r/m (1)", t2 == 0x123456789abcdef);
859
860 test_cmps(mem);
Avi Kivity80a4ea72010-08-17 17:44:14 +0300861 test_scas(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300862
863 test_push(mem);
864 test_pop(mem);
865
866 test_xchg(mem);
Wei Yongjun5647d552010-08-12 21:44:01 +0800867 test_xadd(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300868
869 test_cr8();
870
871 test_smsw();
872 test_lmsw();
873 test_ljmp(mem);
874 test_stringio();
875 test_incdecnotneg(mem);
Wei Yongjund4655ea2010-08-05 14:09:15 +0800876 test_btc(mem);
Wei Yongjun2e16c7f2010-08-09 18:01:13 +0800877 test_bsfbsr(mem);
Avi Kivity51d65a32010-08-19 19:15:31 +0300878 test_imul(mem);
Avi Kivityf12d86b2010-08-24 14:01:11 +0300879 test_div(mem);
Avi Kivityd7f3ee32011-03-29 14:44:49 +0200880 test_sse(mem);
Avi Kivity35870822012-03-22 12:58:06 +0200881 test_mmx(mem);
Avi Kivity8cfa5a02011-06-19 19:50:55 +0300882 test_rip_relative(mem, insn_ram);
Avi Kivityb212fcd2011-09-13 11:15:16 +0300883 test_shld_shrd(mem);
Avi Kivity47c14612012-06-27 11:36:32 +0300884 //test_lgdt_lidt(mem);
Avi Kivityfc2f8802012-06-27 11:36:33 +0300885 test_sreg(mem);
Avi Kivitycb615a42012-06-27 11:36:40 +0300886 test_lldt(mem);
Avi Kivity58a9d812012-06-27 11:36:41 +0300887 test_ltr(mem);
Avi Kivity7d36db32010-08-03 14:07:34 +0300888
Avi Kivityd7143f32012-03-25 15:49:05 +0200889 test_mmx_movq_mf(mem, insn_page, alt_insn_page, insn_ram);
890
Avi Kivityec278ce2012-04-18 19:27:00 +0300891 test_crosspage_mmio(mem);
892
Xiao Guangronga19c7db2012-10-19 15:39:08 +0800893 test_string_io_mmio(mem);
894
Avi Kivity7d36db32010-08-03 14:07:34 +0300895 printf("\nSUMMARY: %d tests, %d failures\n", tests, fails);
896 return fails ? 1 : 0;
897}