root / tests / test-i386.c @ 829309c7
History | View | Annotate | Download (42.9 kB)
1 |
/*
|
---|---|
2 |
* x86 CPU test
|
3 |
*
|
4 |
* Copyright (c) 2003 Fabrice Bellard
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation; either version 2 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 |
*/
|
20 |
#define _GNU_SOURCE
|
21 |
#include <stdlib.h> |
22 |
#include <stdio.h> |
23 |
#include <string.h> |
24 |
#include <inttypes.h> |
25 |
#include <math.h> |
26 |
#include <signal.h> |
27 |
#include <setjmp.h> |
28 |
#include <errno.h> |
29 |
#include <sys/ucontext.h> |
30 |
#include <sys/mman.h> |
31 |
#include <asm/vm86.h> |
32 |
|
33 |
#define TEST_CMOV 0 |
34 |
#define TEST_FCOMI 0 |
35 |
//#define LINUX_VM86_IOPL_FIX
|
36 |
//#define TEST_P4_FLAGS
|
37 |
|
38 |
#define xglue(x, y) x ## y |
39 |
#define glue(x, y) xglue(x, y)
|
40 |
#define stringify(s) tostring(s)
|
41 |
#define tostring(s) #s |
42 |
|
43 |
#define CC_C 0x0001 |
44 |
#define CC_P 0x0004 |
45 |
#define CC_A 0x0010 |
46 |
#define CC_Z 0x0040 |
47 |
#define CC_S 0x0080 |
48 |
#define CC_O 0x0800 |
49 |
|
50 |
#define __init_call __attribute__ ((unused,__section__ (".initcall.init"))) |
51 |
|
52 |
static void *call_start __init_call = NULL; |
53 |
|
54 |
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
|
55 |
|
56 |
#define OP add
|
57 |
#include "test-i386.h" |
58 |
|
59 |
#define OP sub
|
60 |
#include "test-i386.h" |
61 |
|
62 |
#define OP xor
|
63 |
#include "test-i386.h" |
64 |
|
65 |
#define OP and
|
66 |
#include "test-i386.h" |
67 |
|
68 |
#define OP or
|
69 |
#include "test-i386.h" |
70 |
|
71 |
#define OP cmp
|
72 |
#include "test-i386.h" |
73 |
|
74 |
#define OP adc
|
75 |
#define OP_CC
|
76 |
#include "test-i386.h" |
77 |
|
78 |
#define OP sbb
|
79 |
#define OP_CC
|
80 |
#include "test-i386.h" |
81 |
|
82 |
#define OP inc
|
83 |
#define OP_CC
|
84 |
#define OP1
|
85 |
#include "test-i386.h" |
86 |
|
87 |
#define OP dec
|
88 |
#define OP_CC
|
89 |
#define OP1
|
90 |
#include "test-i386.h" |
91 |
|
92 |
#define OP neg
|
93 |
#define OP_CC
|
94 |
#define OP1
|
95 |
#include "test-i386.h" |
96 |
|
97 |
#define OP not
|
98 |
#define OP_CC
|
99 |
#define OP1
|
100 |
#include "test-i386.h" |
101 |
|
102 |
#undef CC_MASK
|
103 |
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
|
104 |
|
105 |
#define OP shl
|
106 |
#include "test-i386-shift.h" |
107 |
|
108 |
#define OP shr
|
109 |
#include "test-i386-shift.h" |
110 |
|
111 |
#define OP sar
|
112 |
#include "test-i386-shift.h" |
113 |
|
114 |
#define OP rol
|
115 |
#include "test-i386-shift.h" |
116 |
|
117 |
#define OP ror
|
118 |
#include "test-i386-shift.h" |
119 |
|
120 |
#define OP rcr
|
121 |
#define OP_CC
|
122 |
#include "test-i386-shift.h" |
123 |
|
124 |
#define OP rcl
|
125 |
#define OP_CC
|
126 |
#include "test-i386-shift.h" |
127 |
|
128 |
#define OP shld
|
129 |
#define OP_SHIFTD
|
130 |
#define OP_NOBYTE
|
131 |
#include "test-i386-shift.h" |
132 |
|
133 |
#define OP shrd
|
134 |
#define OP_SHIFTD
|
135 |
#define OP_NOBYTE
|
136 |
#include "test-i386-shift.h" |
137 |
|
138 |
/* XXX: should be more precise ? */
|
139 |
#undef CC_MASK
|
140 |
#define CC_MASK (CC_C)
|
141 |
|
142 |
#define OP bt
|
143 |
#define OP_NOBYTE
|
144 |
#include "test-i386-shift.h" |
145 |
|
146 |
#define OP bts
|
147 |
#define OP_NOBYTE
|
148 |
#include "test-i386-shift.h" |
149 |
|
150 |
#define OP btr
|
151 |
#define OP_NOBYTE
|
152 |
#include "test-i386-shift.h" |
153 |
|
154 |
#define OP btc
|
155 |
#define OP_NOBYTE
|
156 |
#include "test-i386-shift.h" |
157 |
|
158 |
/* lea test (modrm support) */
|
159 |
#define TEST_LEA(STR)\
|
160 |
{\ |
161 |
asm("leal " STR ", %0"\ |
162 |
: "=r" (res)\
|
163 |
: "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\ |
164 |
printf("lea %s = %08x\n", STR, res);\
|
165 |
} |
166 |
|
167 |
#define TEST_LEA16(STR)\
|
168 |
{\ |
169 |
asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\ |
170 |
: "=wq" (res)\
|
171 |
: "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\ |
172 |
printf("lea %s = %08x\n", STR, res);\
|
173 |
} |
174 |
|
175 |
|
176 |
void test_lea(void) |
177 |
{ |
178 |
int eax, ebx, ecx, edx, esi, edi, res;
|
179 |
eax = 0x0001;
|
180 |
ebx = 0x0002;
|
181 |
ecx = 0x0004;
|
182 |
edx = 0x0008;
|
183 |
esi = 0x0010;
|
184 |
edi = 0x0020;
|
185 |
|
186 |
TEST_LEA("0x4000");
|
187 |
|
188 |
TEST_LEA("(%%eax)");
|
189 |
TEST_LEA("(%%ebx)");
|
190 |
TEST_LEA("(%%ecx)");
|
191 |
TEST_LEA("(%%edx)");
|
192 |
TEST_LEA("(%%esi)");
|
193 |
TEST_LEA("(%%edi)");
|
194 |
|
195 |
TEST_LEA("0x40(%%eax)");
|
196 |
TEST_LEA("0x40(%%ebx)");
|
197 |
TEST_LEA("0x40(%%ecx)");
|
198 |
TEST_LEA("0x40(%%edx)");
|
199 |
TEST_LEA("0x40(%%esi)");
|
200 |
TEST_LEA("0x40(%%edi)");
|
201 |
|
202 |
TEST_LEA("0x4000(%%eax)");
|
203 |
TEST_LEA("0x4000(%%ebx)");
|
204 |
TEST_LEA("0x4000(%%ecx)");
|
205 |
TEST_LEA("0x4000(%%edx)");
|
206 |
TEST_LEA("0x4000(%%esi)");
|
207 |
TEST_LEA("0x4000(%%edi)");
|
208 |
|
209 |
TEST_LEA("(%%eax, %%ecx)");
|
210 |
TEST_LEA("(%%ebx, %%edx)");
|
211 |
TEST_LEA("(%%ecx, %%ecx)");
|
212 |
TEST_LEA("(%%edx, %%ecx)");
|
213 |
TEST_LEA("(%%esi, %%ecx)");
|
214 |
TEST_LEA("(%%edi, %%ecx)");
|
215 |
|
216 |
TEST_LEA("0x40(%%eax, %%ecx)");
|
217 |
TEST_LEA("0x4000(%%ebx, %%edx)");
|
218 |
|
219 |
TEST_LEA("(%%ecx, %%ecx, 2)");
|
220 |
TEST_LEA("(%%edx, %%ecx, 4)");
|
221 |
TEST_LEA("(%%esi, %%ecx, 8)");
|
222 |
|
223 |
TEST_LEA("(,%%eax, 2)");
|
224 |
TEST_LEA("(,%%ebx, 4)");
|
225 |
TEST_LEA("(,%%ecx, 8)");
|
226 |
|
227 |
TEST_LEA("0x40(,%%eax, 2)");
|
228 |
TEST_LEA("0x40(,%%ebx, 4)");
|
229 |
TEST_LEA("0x40(,%%ecx, 8)");
|
230 |
|
231 |
|
232 |
TEST_LEA("-10(%%ecx, %%ecx, 2)");
|
233 |
TEST_LEA("-10(%%edx, %%ecx, 4)");
|
234 |
TEST_LEA("-10(%%esi, %%ecx, 8)");
|
235 |
|
236 |
TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
|
237 |
TEST_LEA("0x4000(%%edx, %%ecx, 4)");
|
238 |
TEST_LEA("0x4000(%%esi, %%ecx, 8)");
|
239 |
|
240 |
/* limited 16 bit addressing test */
|
241 |
TEST_LEA16("0x4000");
|
242 |
TEST_LEA16("(%%bx)");
|
243 |
TEST_LEA16("(%%si)");
|
244 |
TEST_LEA16("(%%di)");
|
245 |
TEST_LEA16("0x40(%%bx)");
|
246 |
TEST_LEA16("0x40(%%si)");
|
247 |
TEST_LEA16("0x40(%%di)");
|
248 |
TEST_LEA16("0x4000(%%bx)");
|
249 |
TEST_LEA16("0x4000(%%si)");
|
250 |
TEST_LEA16("(%%bx,%%si)");
|
251 |
TEST_LEA16("(%%bx,%%di)");
|
252 |
TEST_LEA16("0x40(%%bx,%%si)");
|
253 |
TEST_LEA16("0x40(%%bx,%%di)");
|
254 |
TEST_LEA16("0x4000(%%bx,%%si)");
|
255 |
TEST_LEA16("0x4000(%%bx,%%di)");
|
256 |
} |
257 |
|
258 |
#define TEST_JCC(JCC, v1, v2)\
|
259 |
{\ |
260 |
int res;\
|
261 |
asm("movl $1, %0\n\t"\ |
262 |
"cmpl %2, %1\n\t"\
|
263 |
"j" JCC " 1f\n\t"\ |
264 |
"movl $0, %0\n\t"\
|
265 |
"1:\n\t"\
|
266 |
: "=r" (res)\
|
267 |
: "r" (v1), "r" (v2));\ |
268 |
printf("%-10s %d\n", "j" JCC, res);\ |
269 |
\ |
270 |
asm("movl $0, %0\n\t"\ |
271 |
"cmpl %2, %1\n\t"\
|
272 |
"set" JCC " %b0\n\t"\ |
273 |
: "=r" (res)\
|
274 |
: "r" (v1), "r" (v2));\ |
275 |
printf("%-10s %d\n", "set" JCC, res);\ |
276 |
if (TEST_CMOV) {\
|
277 |
asm("movl $0x12345678, %0\n\t"\ |
278 |
"cmpl %2, %1\n\t"\
|
279 |
"cmov" JCC "l %3, %0\n\t"\ |
280 |
: "=r" (res)\
|
281 |
: "r" (v1), "r" (v2), "m" (1));\ |
282 |
printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\ |
283 |
asm("movl $0x12345678, %0\n\t"\ |
284 |
"cmpl %2, %1\n\t"\
|
285 |
"cmov" JCC "w %w3, %w0\n\t"\ |
286 |
: "=r" (res)\
|
287 |
: "r" (v1), "r" (v2), "r" (1));\ |
288 |
printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\ |
289 |
} \ |
290 |
} |
291 |
|
292 |
/* various jump tests */
|
293 |
void test_jcc(void) |
294 |
{ |
295 |
TEST_JCC("ne", 1, 1); |
296 |
TEST_JCC("ne", 1, 0); |
297 |
|
298 |
TEST_JCC("e", 1, 1); |
299 |
TEST_JCC("e", 1, 0); |
300 |
|
301 |
TEST_JCC("l", 1, 1); |
302 |
TEST_JCC("l", 1, 0); |
303 |
TEST_JCC("l", 1, -1); |
304 |
|
305 |
TEST_JCC("le", 1, 1); |
306 |
TEST_JCC("le", 1, 0); |
307 |
TEST_JCC("le", 1, -1); |
308 |
|
309 |
TEST_JCC("ge", 1, 1); |
310 |
TEST_JCC("ge", 1, 0); |
311 |
TEST_JCC("ge", -1, 1); |
312 |
|
313 |
TEST_JCC("g", 1, 1); |
314 |
TEST_JCC("g", 1, 0); |
315 |
TEST_JCC("g", 1, -1); |
316 |
|
317 |
TEST_JCC("b", 1, 1); |
318 |
TEST_JCC("b", 1, 0); |
319 |
TEST_JCC("b", 1, -1); |
320 |
|
321 |
TEST_JCC("be", 1, 1); |
322 |
TEST_JCC("be", 1, 0); |
323 |
TEST_JCC("be", 1, -1); |
324 |
|
325 |
TEST_JCC("ae", 1, 1); |
326 |
TEST_JCC("ae", 1, 0); |
327 |
TEST_JCC("ae", 1, -1); |
328 |
|
329 |
TEST_JCC("a", 1, 1); |
330 |
TEST_JCC("a", 1, 0); |
331 |
TEST_JCC("a", 1, -1); |
332 |
|
333 |
|
334 |
TEST_JCC("p", 1, 1); |
335 |
TEST_JCC("p", 1, 0); |
336 |
|
337 |
TEST_JCC("np", 1, 1); |
338 |
TEST_JCC("np", 1, 0); |
339 |
|
340 |
TEST_JCC("o", 0x7fffffff, 0); |
341 |
TEST_JCC("o", 0x7fffffff, -1); |
342 |
|
343 |
TEST_JCC("no", 0x7fffffff, 0); |
344 |
TEST_JCC("no", 0x7fffffff, -1); |
345 |
|
346 |
TEST_JCC("s", 0, 1); |
347 |
TEST_JCC("s", 0, -1); |
348 |
TEST_JCC("s", 0, 0); |
349 |
|
350 |
TEST_JCC("ns", 0, 1); |
351 |
TEST_JCC("ns", 0, -1); |
352 |
TEST_JCC("ns", 0, 0); |
353 |
} |
354 |
|
355 |
#undef CC_MASK
|
356 |
#ifdef TEST_P4_FLAGS
|
357 |
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
|
358 |
#else
|
359 |
#define CC_MASK (CC_O | CC_C)
|
360 |
#endif
|
361 |
|
362 |
#define OP mul
|
363 |
#include "test-i386-muldiv.h" |
364 |
|
365 |
#define OP imul
|
366 |
#include "test-i386-muldiv.h" |
367 |
|
368 |
void test_imulw2(int op0, int op1) |
369 |
{ |
370 |
int res, s1, s0, flags;
|
371 |
s0 = op0; |
372 |
s1 = op1; |
373 |
res = s0; |
374 |
flags = 0;
|
375 |
asm ("push %4\n\t" |
376 |
"popf\n\t"
|
377 |
"imulw %w2, %w0\n\t"
|
378 |
"pushf\n\t"
|
379 |
"popl %1\n\t"
|
380 |
: "=q" (res), "=g" (flags) |
381 |
: "q" (s1), "0" (res), "1" (flags)); |
382 |
printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
|
383 |
"imulw", s0, s1, res, flags & CC_MASK);
|
384 |
} |
385 |
|
386 |
void test_imull2(int op0, int op1) |
387 |
{ |
388 |
int res, s1, s0, flags;
|
389 |
s0 = op0; |
390 |
s1 = op1; |
391 |
res = s0; |
392 |
flags = 0;
|
393 |
asm ("push %4\n\t" |
394 |
"popf\n\t"
|
395 |
"imull %2, %0\n\t"
|
396 |
"pushf\n\t"
|
397 |
"popl %1\n\t"
|
398 |
: "=q" (res), "=g" (flags) |
399 |
: "q" (s1), "0" (res), "1" (flags)); |
400 |
printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
|
401 |
"imull", s0, s1, res, flags & CC_MASK);
|
402 |
} |
403 |
|
404 |
#define TEST_IMUL_IM(size, size1, op0, op1)\
|
405 |
{\ |
406 |
int res, flags;\
|
407 |
flags = 0;\
|
408 |
res = 0;\
|
409 |
asm ("push %3\n\t"\ |
410 |
"popf\n\t"\
|
411 |
"imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \ |
412 |
"pushf\n\t"\
|
413 |
"popl %1\n\t"\
|
414 |
: "=r" (res), "=g" (flags)\ |
415 |
: "r" (op1), "1" (flags), "0" (res));\ |
416 |
printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",\
|
417 |
"imul" size, op0, op1, res, flags & CC_MASK);\
|
418 |
} |
419 |
|
420 |
|
421 |
#undef CC_MASK
|
422 |
#define CC_MASK (0) |
423 |
|
424 |
#define OP div
|
425 |
#include "test-i386-muldiv.h" |
426 |
|
427 |
#define OP idiv
|
428 |
#include "test-i386-muldiv.h" |
429 |
|
430 |
void test_mul(void) |
431 |
{ |
432 |
test_imulb(0x1234561d, 4); |
433 |
test_imulb(3, -4); |
434 |
test_imulb(0x80, 0x80); |
435 |
test_imulb(0x10, 0x10); |
436 |
|
437 |
test_imulw(0, 0x1234001d, 45); |
438 |
test_imulw(0, 23, -45); |
439 |
test_imulw(0, 0x8000, 0x8000); |
440 |
test_imulw(0, 0x100, 0x100); |
441 |
|
442 |
test_imull(0, 0x1234001d, 45); |
443 |
test_imull(0, 23, -45); |
444 |
test_imull(0, 0x80000000, 0x80000000); |
445 |
test_imull(0, 0x10000, 0x10000); |
446 |
|
447 |
test_mulb(0x1234561d, 4); |
448 |
test_mulb(3, -4); |
449 |
test_mulb(0x80, 0x80); |
450 |
test_mulb(0x10, 0x10); |
451 |
|
452 |
test_mulw(0, 0x1234001d, 45); |
453 |
test_mulw(0, 23, -45); |
454 |
test_mulw(0, 0x8000, 0x8000); |
455 |
test_mulw(0, 0x100, 0x100); |
456 |
|
457 |
test_mull(0, 0x1234001d, 45); |
458 |
test_mull(0, 23, -45); |
459 |
test_mull(0, 0x80000000, 0x80000000); |
460 |
test_mull(0, 0x10000, 0x10000); |
461 |
|
462 |
test_imulw2(0x1234001d, 45); |
463 |
test_imulw2(23, -45); |
464 |
test_imulw2(0x8000, 0x8000); |
465 |
test_imulw2(0x100, 0x100); |
466 |
|
467 |
test_imull2(0x1234001d, 45); |
468 |
test_imull2(23, -45); |
469 |
test_imull2(0x80000000, 0x80000000); |
470 |
test_imull2(0x10000, 0x10000); |
471 |
|
472 |
TEST_IMUL_IM("w", "w", 45, 0x1234); |
473 |
TEST_IMUL_IM("w", "w", -45, 23); |
474 |
TEST_IMUL_IM("w", "w", 0x8000, 0x80000000); |
475 |
TEST_IMUL_IM("w", "w", 0x7fff, 0x1000); |
476 |
|
477 |
TEST_IMUL_IM("l", "", 45, 0x1234); |
478 |
TEST_IMUL_IM("l", "", -45, 23); |
479 |
TEST_IMUL_IM("l", "", 0x8000, 0x80000000); |
480 |
TEST_IMUL_IM("l", "", 0x7fff, 0x1000); |
481 |
|
482 |
test_idivb(0x12341678, 0x127e); |
483 |
test_idivb(0x43210123, -5); |
484 |
test_idivb(0x12340004, -1); |
485 |
|
486 |
test_idivw(0, 0x12345678, 12347); |
487 |
test_idivw(0, -23223, -45); |
488 |
test_idivw(0, 0x12348000, -1); |
489 |
test_idivw(0x12343, 0x12345678, 0x81238567); |
490 |
|
491 |
test_idivl(0, 0x12345678, 12347); |
492 |
test_idivl(0, -233223, -45); |
493 |
test_idivl(0, 0x80000000, -1); |
494 |
test_idivl(0x12343, 0x12345678, 0x81234567); |
495 |
|
496 |
test_divb(0x12341678, 0x127e); |
497 |
test_divb(0x43210123, -5); |
498 |
test_divb(0x12340004, -1); |
499 |
|
500 |
test_divw(0, 0x12345678, 12347); |
501 |
test_divw(0, -23223, -45); |
502 |
test_divw(0, 0x12348000, -1); |
503 |
test_divw(0x12343, 0x12345678, 0x81238567); |
504 |
|
505 |
test_divl(0, 0x12345678, 12347); |
506 |
test_divl(0, -233223, -45); |
507 |
test_divl(0, 0x80000000, -1); |
508 |
test_divl(0x12343, 0x12345678, 0x81234567); |
509 |
} |
510 |
|
511 |
#define TEST_BSX(op, size, op0)\
|
512 |
{\ |
513 |
int res, val, resz;\
|
514 |
val = op0;\ |
515 |
asm("xorl %1, %1\n"\ |
516 |
"movl $0x12345678, %0\n"\
|
517 |
#op " %" size "2, %" size "0 ; setz %b1" \ |
518 |
: "=r" (res), "=q" (resz)\ |
519 |
: "g" (val));\
|
520 |
printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\ |
521 |
} |
522 |
|
523 |
void test_bsx(void) |
524 |
{ |
525 |
TEST_BSX(bsrw, "w", 0); |
526 |
TEST_BSX(bsrw, "w", 0x12340128); |
527 |
TEST_BSX(bsrl, "", 0); |
528 |
TEST_BSX(bsrl, "", 0x00340128); |
529 |
TEST_BSX(bsfw, "w", 0); |
530 |
TEST_BSX(bsfw, "w", 0x12340128); |
531 |
TEST_BSX(bsfl, "", 0); |
532 |
TEST_BSX(bsfl, "", 0x00340128); |
533 |
} |
534 |
|
535 |
/**********************************************/
|
536 |
|
537 |
void test_fops(double a, double b) |
538 |
{ |
539 |
printf("a=%f b=%f a+b=%f\n", a, b, a + b);
|
540 |
printf("a=%f b=%f a-b=%f\n", a, b, a - b);
|
541 |
printf("a=%f b=%f a*b=%f\n", a, b, a * b);
|
542 |
printf("a=%f b=%f a/b=%f\n", a, b, a / b);
|
543 |
printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
|
544 |
printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
|
545 |
printf("a=%f sin(a)=%f\n", a, sin(a));
|
546 |
printf("a=%f cos(a)=%f\n", a, cos(a));
|
547 |
printf("a=%f tan(a)=%f\n", a, tan(a));
|
548 |
printf("a=%f log(a)=%f\n", a, log(a));
|
549 |
printf("a=%f exp(a)=%f\n", a, exp(a));
|
550 |
printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
|
551 |
/* just to test some op combining */
|
552 |
printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
|
553 |
printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
|
554 |
printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
|
555 |
|
556 |
} |
557 |
|
558 |
void test_fcmp(double a, double b) |
559 |
{ |
560 |
printf("(%f<%f)=%d\n",
|
561 |
a, b, a < b); |
562 |
printf("(%f<=%f)=%d\n",
|
563 |
a, b, a <= b); |
564 |
printf("(%f==%f)=%d\n",
|
565 |
a, b, a == b); |
566 |
printf("(%f>%f)=%d\n",
|
567 |
a, b, a > b); |
568 |
printf("(%f<=%f)=%d\n",
|
569 |
a, b, a >= b); |
570 |
if (TEST_FCOMI) {
|
571 |
unsigned int eflags; |
572 |
/* test f(u)comi instruction */
|
573 |
asm("fcomi %2, %1\n" |
574 |
"pushf\n"
|
575 |
"pop %0\n"
|
576 |
: "=r" (eflags)
|
577 |
: "t" (a), "u" (b)); |
578 |
printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
|
579 |
} |
580 |
} |
581 |
|
582 |
void test_fcvt(double a) |
583 |
{ |
584 |
float fa;
|
585 |
long double la; |
586 |
int16_t fpuc; |
587 |
int i;
|
588 |
int64_t lla; |
589 |
int ia;
|
590 |
int16_t wa; |
591 |
double ra;
|
592 |
|
593 |
fa = a; |
594 |
la = a; |
595 |
printf("(float)%f = %f\n", a, fa);
|
596 |
printf("(long double)%f = %Lf\n", a, la);
|
597 |
printf("a=%016Lx\n", *(long long *)&a); |
598 |
printf("la=%016Lx %04x\n", *(long long *)&la, |
599 |
*(unsigned short *)((char *)(&la) + 8)); |
600 |
|
601 |
/* test all roundings */
|
602 |
asm volatile ("fstcw %0" : "=m" (fpuc)); |
603 |
for(i=0;i<4;i++) { |
604 |
asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10))); |
605 |
asm volatile ("fist %0" : "=m" (wa) : "t" (a)); |
606 |
asm volatile ("fistl %0" : "=m" (ia) : "t" (a)); |
607 |
asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st"); |
608 |
asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a)); |
609 |
asm volatile ("fldcw %0" : : "m" (fpuc)); |
610 |
printf("(short)a = %d\n", wa);
|
611 |
printf("(int)a = %d\n", ia);
|
612 |
printf("(int64_t)a = %Ld\n", lla);
|
613 |
printf("rint(a) = %f\n", ra);
|
614 |
} |
615 |
} |
616 |
|
617 |
#define TEST(N) \
|
618 |
asm("fld" #N : "=t" (a)); \ |
619 |
printf("fld" #N "= %f\n", a); |
620 |
|
621 |
void test_fconst(void) |
622 |
{ |
623 |
double a;
|
624 |
TEST(1);
|
625 |
TEST(l2t); |
626 |
TEST(l2e); |
627 |
TEST(pi); |
628 |
TEST(lg2); |
629 |
TEST(ln2); |
630 |
TEST(z); |
631 |
} |
632 |
|
633 |
void test_fbcd(double a) |
634 |
{ |
635 |
unsigned short bcd[5]; |
636 |
double b;
|
637 |
|
638 |
asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st"); |
639 |
asm("fbld %1" : "=t" (b) : "m" (bcd[0])); |
640 |
printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
|
641 |
a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b); |
642 |
} |
643 |
|
644 |
#define TEST_ENV(env, save, restore)\
|
645 |
{\ |
646 |
memset((env), 0xaa, sizeof(*(env)));\ |
647 |
for(i=0;i<5;i++)\ |
648 |
asm volatile ("fldl %0" : : "m" (dtab[i]));\ |
649 |
asm(save " %0\n" : : "m" (*(env)));\ |
650 |
asm(restore " %0\n": : "m" (*(env)));\ |
651 |
for(i=0;i<5;i++)\ |
652 |
asm volatile ("fstpl %0" : "=m" (rtab[i]));\ |
653 |
for(i=0;i<5;i++)\ |
654 |
printf("res[%d]=%f\n", i, rtab[i]);\
|
655 |
printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
|
656 |
(env)->fpuc,\ |
657 |
(env)->fpus & 0xff00,\
|
658 |
(env)->fptag);\ |
659 |
} |
660 |
|
661 |
void test_fenv(void) |
662 |
{ |
663 |
struct __attribute__((packed)) {
|
664 |
uint16_t fpuc; |
665 |
uint16_t dummy1; |
666 |
uint16_t fpus; |
667 |
uint16_t dummy2; |
668 |
uint16_t fptag; |
669 |
uint16_t dummy3; |
670 |
uint32_t ignored[4];
|
671 |
long double fpregs[8]; |
672 |
} float_env32; |
673 |
struct __attribute__((packed)) {
|
674 |
uint16_t fpuc; |
675 |
uint16_t fpus; |
676 |
uint16_t fptag; |
677 |
uint16_t ignored[4];
|
678 |
long double fpregs[8]; |
679 |
} float_env16; |
680 |
double dtab[8]; |
681 |
double rtab[8]; |
682 |
int i;
|
683 |
|
684 |
for(i=0;i<8;i++) |
685 |
dtab[i] = i + 1;
|
686 |
|
687 |
TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv"); |
688 |
TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor"); |
689 |
TEST_ENV(&float_env32, "fnstenv", "fldenv"); |
690 |
TEST_ENV(&float_env32, "fnsave", "frstor"); |
691 |
} |
692 |
|
693 |
|
694 |
#define TEST_FCMOV(a, b, eflags, CC)\
|
695 |
{\ |
696 |
double res;\
|
697 |
asm("push %3\n"\ |
698 |
"popf\n"\
|
699 |
"fcmov" CC " %2, %0\n"\ |
700 |
: "=t" (res)\
|
701 |
: "0" (a), "u" (b), "g" (eflags));\ |
702 |
printf("fcmov%s eflags=0x%04x-> %f\n", \
|
703 |
CC, eflags, res);\ |
704 |
} |
705 |
|
706 |
void test_fcmov(void) |
707 |
{ |
708 |
double a, b;
|
709 |
int eflags, i;
|
710 |
|
711 |
a = 1.0; |
712 |
b = 2.0; |
713 |
for(i = 0; i < 4; i++) { |
714 |
eflags = 0;
|
715 |
if (i & 1) |
716 |
eflags |= CC_C; |
717 |
if (i & 2) |
718 |
eflags |= CC_Z; |
719 |
TEST_FCMOV(a, b, eflags, "b");
|
720 |
TEST_FCMOV(a, b, eflags, "e");
|
721 |
TEST_FCMOV(a, b, eflags, "be");
|
722 |
TEST_FCMOV(a, b, eflags, "nb");
|
723 |
TEST_FCMOV(a, b, eflags, "ne");
|
724 |
TEST_FCMOV(a, b, eflags, "nbe");
|
725 |
} |
726 |
TEST_FCMOV(a, b, 0, "u"); |
727 |
TEST_FCMOV(a, b, CC_P, "u");
|
728 |
TEST_FCMOV(a, b, 0, "nu"); |
729 |
TEST_FCMOV(a, b, CC_P, "nu");
|
730 |
} |
731 |
|
732 |
void test_floats(void) |
733 |
{ |
734 |
test_fops(2, 3); |
735 |
test_fops(1.4, -5); |
736 |
test_fcmp(2, -1); |
737 |
test_fcmp(2, 2); |
738 |
test_fcmp(2, 3); |
739 |
test_fcvt(0.5); |
740 |
test_fcvt(-0.5); |
741 |
test_fcvt(1.0/7.0); |
742 |
test_fcvt(-1.0/9.0); |
743 |
test_fcvt(32768);
|
744 |
test_fcvt(-1e20);
|
745 |
test_fconst(); |
746 |
test_fbcd(1234567890123456);
|
747 |
test_fbcd(-123451234567890);
|
748 |
test_fenv(); |
749 |
if (TEST_CMOV) {
|
750 |
test_fcmov(); |
751 |
} |
752 |
} |
753 |
|
754 |
/**********************************************/
|
755 |
|
756 |
#define TEST_BCD(op, op0, cc_in, cc_mask)\
|
757 |
{\ |
758 |
int res, flags;\
|
759 |
res = op0;\ |
760 |
flags = cc_in;\ |
761 |
asm ("push %3\n\t"\ |
762 |
"popf\n\t"\
|
763 |
#op "\n\t"\ |
764 |
"pushf\n\t"\
|
765 |
"popl %1\n\t"\
|
766 |
: "=a" (res), "=g" (flags)\ |
767 |
: "0" (res), "1" (flags));\ |
768 |
printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
|
769 |
#op, op0, res, cc_in, flags & cc_mask);\
|
770 |
} |
771 |
|
772 |
void test_bcd(void) |
773 |
{ |
774 |
TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
775 |
TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
776 |
TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
777 |
TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
778 |
TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
779 |
TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
780 |
TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
781 |
TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); |
782 |
TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); |
783 |
TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
784 |
TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
785 |
TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
786 |
TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
787 |
|
788 |
TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
789 |
TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
790 |
TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
791 |
TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
792 |
TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
793 |
TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
794 |
TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
795 |
TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); |
796 |
TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); |
797 |
TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
798 |
TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
799 |
TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
800 |
TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
|
801 |
|
802 |
TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
|
803 |
TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
|
804 |
TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
|
805 |
TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
|
806 |
TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A)); |
807 |
TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A)); |
808 |
TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A)); |
809 |
TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A)); |
810 |
|
811 |
TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
|
812 |
TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
|
813 |
TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
|
814 |
TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
|
815 |
TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A)); |
816 |
TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A)); |
817 |
TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A)); |
818 |
TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A)); |
819 |
|
820 |
TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
|
821 |
TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
|
822 |
} |
823 |
|
824 |
#define TEST_XCHG(op, size, opconst)\
|
825 |
{\ |
826 |
int op0, op1;\
|
827 |
op0 = 0x12345678;\
|
828 |
op1 = 0xfbca7654;\
|
829 |
asm(#op " %" size "0, %" size "1" \ |
830 |
: "=q" (op0), opconst (op1) \
|
831 |
: "0" (op0), "1" (op1));\ |
832 |
printf("%-10s A=%08x B=%08x\n",\
|
833 |
#op, op0, op1);\
|
834 |
} |
835 |
|
836 |
#define TEST_CMPXCHG(op, size, opconst, eax)\
|
837 |
{\ |
838 |
int op0, op1;\
|
839 |
op0 = 0x12345678;\
|
840 |
op1 = 0xfbca7654;\
|
841 |
asm(#op " %" size "0, %" size "1" \ |
842 |
: "=q" (op0), opconst (op1) \
|
843 |
: "0" (op0), "1" (op1), "a" (eax));\ |
844 |
printf("%-10s EAX=%08x A=%08x C=%08x\n",\
|
845 |
#op, eax, op0, op1);\
|
846 |
} |
847 |
|
848 |
void test_xchg(void) |
849 |
{ |
850 |
TEST_XCHG(xchgl, "", "=q"); |
851 |
TEST_XCHG(xchgw, "w", "=q"); |
852 |
TEST_XCHG(xchgb, "b", "=q"); |
853 |
|
854 |
TEST_XCHG(xchgl, "", "=m"); |
855 |
TEST_XCHG(xchgw, "w", "=m"); |
856 |
TEST_XCHG(xchgb, "b", "=m"); |
857 |
|
858 |
TEST_XCHG(xaddl, "", "=q"); |
859 |
TEST_XCHG(xaddw, "w", "=q"); |
860 |
TEST_XCHG(xaddb, "b", "=q"); |
861 |
|
862 |
{ |
863 |
int res;
|
864 |
res = 0x12345678;
|
865 |
asm("xaddl %1, %0" : "=r" (res) : "0" (res)); |
866 |
printf("xaddl same res=%08x\n", res);
|
867 |
} |
868 |
|
869 |
TEST_XCHG(xaddl, "", "=m"); |
870 |
TEST_XCHG(xaddw, "w", "=m"); |
871 |
TEST_XCHG(xaddb, "b", "=m"); |
872 |
|
873 |
TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654); |
874 |
TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654); |
875 |
TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654); |
876 |
|
877 |
TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc); |
878 |
TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc); |
879 |
TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc); |
880 |
|
881 |
TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654); |
882 |
TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654); |
883 |
TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654); |
884 |
|
885 |
TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc); |
886 |
TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc); |
887 |
TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc); |
888 |
|
889 |
{ |
890 |
uint64_t op0, op1, op2; |
891 |
int i, eflags;
|
892 |
|
893 |
for(i = 0; i < 2; i++) { |
894 |
op0 = 0x123456789abcd;
|
895 |
if (i == 0) |
896 |
op1 = 0xfbca765423456;
|
897 |
else
|
898 |
op1 = op0; |
899 |
op2 = 0x6532432432434;
|
900 |
asm("cmpxchg8b %1\n" |
901 |
"pushf\n"
|
902 |
"popl %2\n"
|
903 |
: "=A" (op0), "=m" (op1), "=g" (eflags) |
904 |
: "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32))); |
905 |
printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
|
906 |
op0, op1, eflags & CC_Z); |
907 |
} |
908 |
} |
909 |
} |
910 |
|
911 |
/**********************************************/
|
912 |
/* segmentation tests */
|
913 |
|
914 |
#include <asm/ldt.h> |
915 |
#include <linux/unistd.h> |
916 |
#include <linux/version.h> |
917 |
|
918 |
_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount) |
919 |
|
920 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66) |
921 |
#define modify_ldt_ldt_s user_desc
|
922 |
#endif
|
923 |
|
924 |
uint8_t seg_data1[4096];
|
925 |
uint8_t seg_data2[4096];
|
926 |
|
927 |
#define MK_SEL(n) (((n) << 3) | 7) |
928 |
|
929 |
#define TEST_LR(op, size, seg, mask)\
|
930 |
{\ |
931 |
int res, res2;\
|
932 |
res = 0x12345678;\
|
933 |
asm (op " %" size "2, %" size "0\n" \ |
934 |
"movl $0, %1\n"\
|
935 |
"jnz 1f\n"\
|
936 |
"movl $1, %1\n"\
|
937 |
"1:\n"\
|
938 |
: "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\ |
939 |
printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
|
940 |
} |
941 |
|
942 |
/* NOTE: we use Linux modify_ldt syscall */
|
943 |
void test_segs(void) |
944 |
{ |
945 |
struct modify_ldt_ldt_s ldt;
|
946 |
long long ldt_table[3]; |
947 |
int res, res2;
|
948 |
char tmp;
|
949 |
struct {
|
950 |
uint32_t offset; |
951 |
uint16_t seg; |
952 |
} __attribute__((packed)) segoff; |
953 |
|
954 |
ldt.entry_number = 1;
|
955 |
ldt.base_addr = (unsigned long)&seg_data1; |
956 |
ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12; |
957 |
ldt.seg_32bit = 1;
|
958 |
ldt.contents = MODIFY_LDT_CONTENTS_DATA; |
959 |
ldt.read_exec_only = 0;
|
960 |
ldt.limit_in_pages = 1;
|
961 |
ldt.seg_not_present = 0;
|
962 |
ldt.useable = 1;
|
963 |
modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ |
964 |
|
965 |
ldt.entry_number = 2;
|
966 |
ldt.base_addr = (unsigned long)&seg_data2; |
967 |
ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12; |
968 |
ldt.seg_32bit = 1;
|
969 |
ldt.contents = MODIFY_LDT_CONTENTS_DATA; |
970 |
ldt.read_exec_only = 0;
|
971 |
ldt.limit_in_pages = 1;
|
972 |
ldt.seg_not_present = 0;
|
973 |
ldt.useable = 1;
|
974 |
modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ |
975 |
|
976 |
modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */ |
977 |
#if 0
|
978 |
{
|
979 |
int i;
|
980 |
for(i=0;i<3;i++)
|
981 |
printf("%d: %016Lx\n", i, ldt_table[i]);
|
982 |
}
|
983 |
#endif
|
984 |
/* do some tests with fs or gs */
|
985 |
asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1))); |
986 |
|
987 |
seg_data1[1] = 0xaa; |
988 |
seg_data2[1] = 0x55; |
989 |
|
990 |
asm volatile ("fs movzbl 0x1, %0" : "=r" (res)); |
991 |
printf("FS[1] = %02x\n", res);
|
992 |
|
993 |
asm volatile ("pushl %%gs\n" |
994 |
"movl %1, %%gs\n"
|
995 |
"gs movzbl 0x1, %0\n"
|
996 |
"popl %%gs\n"
|
997 |
: "=r" (res)
|
998 |
: "r" (MK_SEL(2))); |
999 |
printf("GS[1] = %02x\n", res);
|
1000 |
|
1001 |
/* tests with ds/ss (implicit segment case) */
|
1002 |
tmp = 0xa5;
|
1003 |
asm volatile ("pushl %%ebp\n\t" |
1004 |
"pushl %%ds\n\t"
|
1005 |
"movl %2, %%ds\n\t"
|
1006 |
"movl %3, %%ebp\n\t"
|
1007 |
"movzbl 0x1, %0\n\t"
|
1008 |
"movzbl (%%ebp), %1\n\t"
|
1009 |
"popl %%ds\n\t"
|
1010 |
"popl %%ebp\n\t"
|
1011 |
: "=r" (res), "=r" (res2) |
1012 |
: "r" (MK_SEL(1)), "r" (&tmp)); |
1013 |
printf("DS[1] = %02x\n", res);
|
1014 |
printf("SS[tmp] = %02x\n", res2);
|
1015 |
|
1016 |
segoff.seg = MK_SEL(2);
|
1017 |
segoff.offset = 0xabcdef12;
|
1018 |
asm volatile("lfs %2, %0\n\t" |
1019 |
"movl %%fs, %1\n\t"
|
1020 |
: "=r" (res), "=g" (res2) |
1021 |
: "m" (segoff));
|
1022 |
printf("FS:reg = %04x:%08x\n", res2, res);
|
1023 |
|
1024 |
TEST_LR("larw", "w", MK_SEL(2), 0x0100); |
1025 |
TEST_LR("larl", "", MK_SEL(2), 0x0100); |
1026 |
TEST_LR("lslw", "w", MK_SEL(2), 0); |
1027 |
TEST_LR("lsll", "", MK_SEL(2), 0); |
1028 |
|
1029 |
TEST_LR("larw", "w", 0xfff8, 0); |
1030 |
TEST_LR("larl", "", 0xfff8, 0); |
1031 |
TEST_LR("lslw", "w", 0xfff8, 0); |
1032 |
TEST_LR("lsll", "", 0xfff8, 0); |
1033 |
} |
1034 |
|
1035 |
/* 16 bit code test */
|
1036 |
extern char code16_start, code16_end; |
1037 |
extern char code16_func1; |
1038 |
extern char code16_func2; |
1039 |
extern char code16_func3; |
1040 |
|
1041 |
void test_code16(void) |
1042 |
{ |
1043 |
struct modify_ldt_ldt_s ldt;
|
1044 |
int res, res2;
|
1045 |
|
1046 |
/* build a code segment */
|
1047 |
ldt.entry_number = 1;
|
1048 |
ldt.base_addr = (unsigned long)&code16_start; |
1049 |
ldt.limit = &code16_end - &code16_start; |
1050 |
ldt.seg_32bit = 0;
|
1051 |
ldt.contents = MODIFY_LDT_CONTENTS_CODE; |
1052 |
ldt.read_exec_only = 0;
|
1053 |
ldt.limit_in_pages = 0;
|
1054 |
ldt.seg_not_present = 0;
|
1055 |
ldt.useable = 1;
|
1056 |
modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ |
1057 |
|
1058 |
/* call the first function */
|
1059 |
asm volatile ("lcall %1, %2" |
1060 |
: "=a" (res)
|
1061 |
: "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc"); |
1062 |
printf("func1() = 0x%08x\n", res);
|
1063 |
asm volatile ("lcall %2, %3" |
1064 |
: "=a" (res), "=c" (res2) |
1065 |
: "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc"); |
1066 |
printf("func2() = 0x%08x spdec=%d\n", res, res2);
|
1067 |
asm volatile ("lcall %1, %2" |
1068 |
: "=a" (res)
|
1069 |
: "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc"); |
1070 |
printf("func3() = 0x%08x\n", res);
|
1071 |
} |
1072 |
|
1073 |
extern char func_lret32; |
1074 |
extern char func_iret32; |
1075 |
|
1076 |
void test_misc(void) |
1077 |
{ |
1078 |
char table[256]; |
1079 |
int res, i;
|
1080 |
|
1081 |
for(i=0;i<256;i++) table[i] = 256 - i; |
1082 |
res = 0x12345678;
|
1083 |
asm ("xlat" : "=a" (res) : "b" (table), "0" (res)); |
1084 |
printf("xlat: EAX=%08x\n", res);
|
1085 |
|
1086 |
asm volatile ("pushl %%cs ; call %1" |
1087 |
: "=a" (res)
|
1088 |
: "m" (func_lret32): "memory", "cc"); |
1089 |
printf("func_lret32=%x\n", res);
|
1090 |
|
1091 |
asm volatile ("pushfl ; pushl %%cs ; call %1" |
1092 |
: "=a" (res)
|
1093 |
: "m" (func_iret32): "memory", "cc"); |
1094 |
printf("func_iret32=%x\n", res);
|
1095 |
|
1096 |
/* specific popl test */
|
1097 |
asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0" |
1098 |
: "=g" (res));
|
1099 |
printf("popl esp=%x\n", res);
|
1100 |
|
1101 |
/* specific popw test */
|
1102 |
asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0" |
1103 |
: "=g" (res));
|
1104 |
printf("popw esp=%x\n", res);
|
1105 |
} |
1106 |
|
1107 |
uint8_t str_buffer[4096];
|
1108 |
|
1109 |
#define TEST_STRING1(OP, size, DF, REP)\
|
1110 |
{\ |
1111 |
int esi, edi, eax, ecx, eflags;\
|
1112 |
\ |
1113 |
esi = (long)(str_buffer + sizeof(str_buffer) / 2);\ |
1114 |
edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\ |
1115 |
eax = 0x12345678;\
|
1116 |
ecx = 17;\
|
1117 |
\ |
1118 |
asm volatile ("pushl $0\n\t"\ |
1119 |
"popf\n\t"\
|
1120 |
DF "\n\t"\
|
1121 |
REP #OP size "\n\t"\ |
1122 |
"cld\n\t"\
|
1123 |
"pushf\n\t"\
|
1124 |
"popl %4\n\t"\
|
1125 |
: "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\ |
1126 |
: "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\ |
1127 |
printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
|
1128 |
REP #OP size, esi, edi, eax, ecx,\
|
1129 |
eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\ |
1130 |
} |
1131 |
|
1132 |
#define TEST_STRING(OP, REP)\
|
1133 |
TEST_STRING1(OP, "b", "", REP);\ |
1134 |
TEST_STRING1(OP, "w", "", REP);\ |
1135 |
TEST_STRING1(OP, "l", "", REP);\ |
1136 |
TEST_STRING1(OP, "b", "std", REP);\ |
1137 |
TEST_STRING1(OP, "w", "std", REP);\ |
1138 |
TEST_STRING1(OP, "l", "std", REP) |
1139 |
|
1140 |
void test_string(void) |
1141 |
{ |
1142 |
int i;
|
1143 |
for(i = 0;i < sizeof(str_buffer); i++) |
1144 |
str_buffer[i] = i + 0x56;
|
1145 |
TEST_STRING(stos, "");
|
1146 |
TEST_STRING(stos, "rep ");
|
1147 |
TEST_STRING(lods, ""); /* to verify stos */ |
1148 |
TEST_STRING(lods, "rep ");
|
1149 |
TEST_STRING(movs, "");
|
1150 |
TEST_STRING(movs, "rep ");
|
1151 |
TEST_STRING(lods, ""); /* to verify stos */ |
1152 |
|
1153 |
/* XXX: better tests */
|
1154 |
TEST_STRING(scas, "");
|
1155 |
TEST_STRING(scas, "repz ");
|
1156 |
TEST_STRING(scas, "repnz ");
|
1157 |
TEST_STRING(cmps, "");
|
1158 |
TEST_STRING(cmps, "repz ");
|
1159 |
TEST_STRING(cmps, "repnz ");
|
1160 |
} |
1161 |
|
1162 |
/* VM86 test */
|
1163 |
|
1164 |
static inline void set_bit(uint8_t *a, unsigned int bit) |
1165 |
{ |
1166 |
a[bit / 8] |= (1 << (bit % 8)); |
1167 |
} |
1168 |
|
1169 |
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) |
1170 |
{ |
1171 |
return (uint8_t *)((seg << 4) + (reg & 0xffff)); |
1172 |
} |
1173 |
|
1174 |
static inline void pushw(struct vm86_regs *r, int val) |
1175 |
{ |
1176 |
r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff); |
1177 |
*(uint16_t *)seg_to_linear(r->ss, r->esp) = val; |
1178 |
} |
1179 |
|
1180 |
#undef __syscall_return
|
1181 |
#define __syscall_return(type, res) \
|
1182 |
do { \
|
1183 |
return (type) (res); \
|
1184 |
} while (0) |
1185 |
|
1186 |
_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86) |
1187 |
|
1188 |
extern char vm86_code_start; |
1189 |
extern char vm86_code_end; |
1190 |
|
1191 |
#define VM86_CODE_CS 0x100 |
1192 |
#define VM86_CODE_IP 0x100 |
1193 |
|
1194 |
void test_vm86(void) |
1195 |
{ |
1196 |
struct vm86plus_struct ctx;
|
1197 |
struct vm86_regs *r;
|
1198 |
uint8_t *vm86_mem; |
1199 |
int seg, ret;
|
1200 |
|
1201 |
vm86_mem = mmap((void *)0x00000000, 0x110000, |
1202 |
PROT_WRITE | PROT_READ | PROT_EXEC, |
1203 |
MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); |
1204 |
if (vm86_mem == MAP_FAILED) {
|
1205 |
printf("ERROR: could not map vm86 memory");
|
1206 |
return;
|
1207 |
} |
1208 |
memset(&ctx, 0, sizeof(ctx)); |
1209 |
|
1210 |
/* init basic registers */
|
1211 |
r = &ctx.regs; |
1212 |
r->eip = VM86_CODE_IP; |
1213 |
r->esp = 0xfffe;
|
1214 |
seg = VM86_CODE_CS; |
1215 |
r->cs = seg; |
1216 |
r->ss = seg; |
1217 |
r->ds = seg; |
1218 |
r->es = seg; |
1219 |
r->fs = seg; |
1220 |
r->gs = seg; |
1221 |
r->eflags = VIF_MASK; |
1222 |
|
1223 |
/* move code to proper address. We use the same layout as a .com
|
1224 |
dos program. */
|
1225 |
memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
|
1226 |
&vm86_code_start, &vm86_code_end - &vm86_code_start); |
1227 |
|
1228 |
/* mark int 0x21 as being emulated */
|
1229 |
set_bit((uint8_t *)&ctx.int_revectored, 0x21);
|
1230 |
|
1231 |
for(;;) {
|
1232 |
ret = vm86(VM86_ENTER, &ctx); |
1233 |
switch(VM86_TYPE(ret)) {
|
1234 |
case VM86_INTx:
|
1235 |
{ |
1236 |
int int_num, ah, v;
|
1237 |
|
1238 |
int_num = VM86_ARG(ret); |
1239 |
if (int_num != 0x21) |
1240 |
goto unknown_int;
|
1241 |
ah = (r->eax >> 8) & 0xff; |
1242 |
switch(ah) {
|
1243 |
case 0x00: /* exit */ |
1244 |
goto the_end;
|
1245 |
case 0x02: /* write char */ |
1246 |
{ |
1247 |
uint8_t c = r->edx; |
1248 |
putchar(c); |
1249 |
} |
1250 |
break;
|
1251 |
case 0x09: /* write string */ |
1252 |
{ |
1253 |
uint8_t c, *ptr; |
1254 |
ptr = seg_to_linear(r->ds, r->edx); |
1255 |
for(;;) {
|
1256 |
c = *ptr++; |
1257 |
if (c == '$') |
1258 |
break;
|
1259 |
putchar(c); |
1260 |
} |
1261 |
r->eax = (r->eax & ~0xff) | '$'; |
1262 |
} |
1263 |
break;
|
1264 |
case 0xff: /* extension: write eflags number in edx */ |
1265 |
v = (int)r->edx;
|
1266 |
#ifndef LINUX_VM86_IOPL_FIX
|
1267 |
v &= ~0x3000;
|
1268 |
#endif
|
1269 |
printf("%08x\n", v);
|
1270 |
break;
|
1271 |
default:
|
1272 |
unknown_int:
|
1273 |
printf("unsupported int 0x%02x\n", int_num);
|
1274 |
goto the_end;
|
1275 |
} |
1276 |
} |
1277 |
break;
|
1278 |
case VM86_SIGNAL:
|
1279 |
/* a signal came, we just ignore that */
|
1280 |
break;
|
1281 |
case VM86_STI:
|
1282 |
break;
|
1283 |
default:
|
1284 |
printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
|
1285 |
goto the_end;
|
1286 |
} |
1287 |
} |
1288 |
the_end:
|
1289 |
printf("VM86 end\n");
|
1290 |
munmap(vm86_mem, 0x110000);
|
1291 |
} |
1292 |
|
1293 |
/* exception tests */
|
1294 |
#ifndef REG_EAX
|
1295 |
#define REG_EAX EAX
|
1296 |
#define REG_EBX EBX
|
1297 |
#define REG_ECX ECX
|
1298 |
#define REG_EDX EDX
|
1299 |
#define REG_ESI ESI
|
1300 |
#define REG_EDI EDI
|
1301 |
#define REG_EBP EBP
|
1302 |
#define REG_ESP ESP
|
1303 |
#define REG_EIP EIP
|
1304 |
#define REG_EFL EFL
|
1305 |
#define REG_TRAPNO TRAPNO
|
1306 |
#define REG_ERR ERR
|
1307 |
#endif
|
1308 |
|
1309 |
jmp_buf jmp_env; |
1310 |
int v1;
|
1311 |
int tab[2]; |
1312 |
|
1313 |
void sig_handler(int sig, siginfo_t *info, void *puc) |
1314 |
{ |
1315 |
struct ucontext *uc = puc;
|
1316 |
|
1317 |
printf("si_signo=%d si_errno=%d si_code=%d",
|
1318 |
info->si_signo, info->si_errno, info->si_code); |
1319 |
printf(" si_addr=0x%08lx",
|
1320 |
(unsigned long)info->si_addr); |
1321 |
printf("\n");
|
1322 |
|
1323 |
printf("trapno=0x%02x err=0x%08x",
|
1324 |
uc->uc_mcontext.gregs[REG_TRAPNO], |
1325 |
uc->uc_mcontext.gregs[REG_ERR]); |
1326 |
printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
|
1327 |
printf("\n");
|
1328 |
longjmp(jmp_env, 1);
|
1329 |
} |
1330 |
|
1331 |
void test_exceptions(void) |
1332 |
{ |
1333 |
struct modify_ldt_ldt_s ldt;
|
1334 |
struct sigaction act;
|
1335 |
volatile int val; |
1336 |
|
1337 |
act.sa_sigaction = sig_handler; |
1338 |
sigemptyset(&act.sa_mask); |
1339 |
act.sa_flags = SA_SIGINFO; |
1340 |
sigaction(SIGFPE, &act, NULL);
|
1341 |
sigaction(SIGILL, &act, NULL);
|
1342 |
sigaction(SIGSEGV, &act, NULL);
|
1343 |
sigaction(SIGBUS, &act, NULL);
|
1344 |
sigaction(SIGTRAP, &act, NULL);
|
1345 |
|
1346 |
/* test division by zero reporting */
|
1347 |
printf("DIVZ exception:\n");
|
1348 |
if (setjmp(jmp_env) == 0) { |
1349 |
/* now divide by zero */
|
1350 |
v1 = 0;
|
1351 |
v1 = 2 / v1;
|
1352 |
} |
1353 |
|
1354 |
printf("BOUND exception:\n");
|
1355 |
if (setjmp(jmp_env) == 0) { |
1356 |
/* bound exception */
|
1357 |
tab[0] = 1; |
1358 |
tab[1] = 10; |
1359 |
asm volatile ("bound %0, %1" : : "r" (11), "m" (tab)); |
1360 |
} |
1361 |
|
1362 |
printf("segment exceptions:\n");
|
1363 |
if (setjmp(jmp_env) == 0) { |
1364 |
/* load an invalid segment */
|
1365 |
asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1)); |
1366 |
} |
1367 |
if (setjmp(jmp_env) == 0) { |
1368 |
/* null data segment is valid */
|
1369 |
asm volatile ("movl %0, %%fs" : : "r" (3)); |
1370 |
/* null stack segment */
|
1371 |
asm volatile ("movl %0, %%ss" : : "r" (3)); |
1372 |
} |
1373 |
|
1374 |
ldt.entry_number = 1;
|
1375 |
ldt.base_addr = (unsigned long)&seg_data1; |
1376 |
ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12; |
1377 |
ldt.seg_32bit = 1;
|
1378 |
ldt.contents = MODIFY_LDT_CONTENTS_DATA; |
1379 |
ldt.read_exec_only = 0;
|
1380 |
ldt.limit_in_pages = 1;
|
1381 |
ldt.seg_not_present = 1;
|
1382 |
ldt.useable = 1;
|
1383 |
modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ |
1384 |
|
1385 |
if (setjmp(jmp_env) == 0) { |
1386 |
/* segment not present */
|
1387 |
asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1))); |
1388 |
} |
1389 |
|
1390 |
/* test SEGV reporting */
|
1391 |
printf("PF exception:\n");
|
1392 |
if (setjmp(jmp_env) == 0) { |
1393 |
val = 1;
|
1394 |
/* we add a nop to test a weird PC retrieval case */
|
1395 |
asm volatile ("nop"); |
1396 |
/* now store in an invalid address */
|
1397 |
*(char *)0x1234 = 1; |
1398 |
} |
1399 |
|
1400 |
/* test SEGV reporting */
|
1401 |
printf("PF exception:\n");
|
1402 |
if (setjmp(jmp_env) == 0) { |
1403 |
val = 1;
|
1404 |
/* read from an invalid address */
|
1405 |
v1 = *(char *)0x1234; |
1406 |
} |
1407 |
|
1408 |
/* test illegal instruction reporting */
|
1409 |
printf("UD2 exception:\n");
|
1410 |
if (setjmp(jmp_env) == 0) { |
1411 |
/* now execute an invalid instruction */
|
1412 |
asm volatile("ud2"); |
1413 |
} |
1414 |
printf("lock nop exception:\n");
|
1415 |
if (setjmp(jmp_env) == 0) { |
1416 |
/* now execute an invalid instruction */
|
1417 |
asm volatile("lock nop"); |
1418 |
} |
1419 |
|
1420 |
printf("INT exception:\n");
|
1421 |
if (setjmp(jmp_env) == 0) { |
1422 |
asm volatile ("int $0xfd"); |
1423 |
} |
1424 |
if (setjmp(jmp_env) == 0) { |
1425 |
asm volatile ("int $0x01"); |
1426 |
} |
1427 |
if (setjmp(jmp_env) == 0) { |
1428 |
asm volatile (".byte 0xcd, 0x03"); |
1429 |
} |
1430 |
if (setjmp(jmp_env) == 0) { |
1431 |
asm volatile ("int $0x04"); |
1432 |
} |
1433 |
if (setjmp(jmp_env) == 0) { |
1434 |
asm volatile ("int $0x05"); |
1435 |
} |
1436 |
|
1437 |
printf("INT3 exception:\n");
|
1438 |
if (setjmp(jmp_env) == 0) { |
1439 |
asm volatile ("int3"); |
1440 |
} |
1441 |
|
1442 |
printf("CLI exception:\n");
|
1443 |
if (setjmp(jmp_env) == 0) { |
1444 |
asm volatile ("cli"); |
1445 |
} |
1446 |
|
1447 |
printf("STI exception:\n");
|
1448 |
if (setjmp(jmp_env) == 0) { |
1449 |
asm volatile ("cli"); |
1450 |
} |
1451 |
|
1452 |
printf("INTO exception:\n");
|
1453 |
if (setjmp(jmp_env) == 0) { |
1454 |
/* overflow exception */
|
1455 |
asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff)); |
1456 |
} |
1457 |
|
1458 |
printf("OUTB exception:\n");
|
1459 |
if (setjmp(jmp_env) == 0) { |
1460 |
asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0)); |
1461 |
} |
1462 |
|
1463 |
printf("INB exception:\n");
|
1464 |
if (setjmp(jmp_env) == 0) { |
1465 |
asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321)); |
1466 |
} |
1467 |
|
1468 |
printf("REP OUTSB exception:\n");
|
1469 |
if (setjmp(jmp_env) == 0) { |
1470 |
asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1)); |
1471 |
} |
1472 |
|
1473 |
printf("REP INSB exception:\n");
|
1474 |
if (setjmp(jmp_env) == 0) { |
1475 |
asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1)); |
1476 |
} |
1477 |
|
1478 |
printf("HLT exception:\n");
|
1479 |
if (setjmp(jmp_env) == 0) { |
1480 |
asm volatile ("hlt"); |
1481 |
} |
1482 |
|
1483 |
printf("single step exception:\n");
|
1484 |
val = 0;
|
1485 |
if (setjmp(jmp_env) == 0) { |
1486 |
asm volatile ("pushf\n" |
1487 |
"orl $0x00100, (%%esp)\n"
|
1488 |
"popf\n"
|
1489 |
"movl $0xabcd, %0\n"
|
1490 |
"movl $0x0, %0\n" : "=m" (val) : : "cc", "memory"); |
1491 |
} |
1492 |
printf("val=0x%x\n", val);
|
1493 |
} |
1494 |
|
1495 |
/* specific precise single step test */
|
1496 |
void sig_trap_handler(int sig, siginfo_t *info, void *puc) |
1497 |
{ |
1498 |
struct ucontext *uc = puc;
|
1499 |
printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
|
1500 |
} |
1501 |
|
1502 |
const uint8_t sstep_buf1[4] = { 1, 2, 3, 4}; |
1503 |
uint8_t sstep_buf2[4];
|
1504 |
|
1505 |
void test_single_step(void) |
1506 |
{ |
1507 |
struct sigaction act;
|
1508 |
volatile int val; |
1509 |
int i;
|
1510 |
|
1511 |
val = 0;
|
1512 |
act.sa_sigaction = sig_trap_handler; |
1513 |
sigemptyset(&act.sa_mask); |
1514 |
act.sa_flags = SA_SIGINFO; |
1515 |
sigaction(SIGTRAP, &act, NULL);
|
1516 |
asm volatile ("pushf\n" |
1517 |
"orl $0x00100, (%%esp)\n"
|
1518 |
"popf\n"
|
1519 |
"movl $0xabcd, %0\n"
|
1520 |
|
1521 |
/* jmp test */
|
1522 |
"movl $3, %%ecx\n"
|
1523 |
"1:\n"
|
1524 |
"addl $1, %0\n"
|
1525 |
"decl %%ecx\n"
|
1526 |
"jnz 1b\n"
|
1527 |
|
1528 |
/* movsb: the single step should stop at each movsb iteration */
|
1529 |
"movl $sstep_buf1, %%esi\n"
|
1530 |
"movl $sstep_buf2, %%edi\n"
|
1531 |
"movl $0, %%ecx\n"
|
1532 |
"rep movsb\n"
|
1533 |
"movl $3, %%ecx\n"
|
1534 |
"rep movsb\n"
|
1535 |
"movl $1, %%ecx\n"
|
1536 |
"rep movsb\n"
|
1537 |
|
1538 |
/* cmpsb: the single step should stop at each cmpsb iteration */
|
1539 |
"movl $sstep_buf1, %%esi\n"
|
1540 |
"movl $sstep_buf2, %%edi\n"
|
1541 |
"movl $0, %%ecx\n"
|
1542 |
"rep cmpsb\n"
|
1543 |
"movl $4, %%ecx\n"
|
1544 |
"rep cmpsb\n"
|
1545 |
|
1546 |
/* getpid() syscall: single step should skip one
|
1547 |
instruction */
|
1548 |
"movl $20, %%eax\n"
|
1549 |
"int $0x80\n"
|
1550 |
"movl $0, %%eax\n"
|
1551 |
|
1552 |
/* when modifying SS, trace is not done on the next
|
1553 |
instruction */
|
1554 |
"movl %%ss, %%ecx\n"
|
1555 |
"movl %%ecx, %%ss\n"
|
1556 |
"addl $1, %0\n"
|
1557 |
"movl $1, %%eax\n"
|
1558 |
"movl %%ecx, %%ss\n"
|
1559 |
"jmp 1f\n"
|
1560 |
"addl $1, %0\n"
|
1561 |
"1:\n"
|
1562 |
"movl $1, %%eax\n"
|
1563 |
"pushl %%ecx\n"
|
1564 |
"popl %%ss\n"
|
1565 |
"addl $1, %0\n"
|
1566 |
"movl $1, %%eax\n"
|
1567 |
|
1568 |
"pushf\n"
|
1569 |
"andl $~0x00100, (%%esp)\n"
|
1570 |
"popf\n"
|
1571 |
: "=m" (val)
|
1572 |
: |
1573 |
: "cc", "memory", "eax", "ecx", "esi", "edi"); |
1574 |
printf("val=%d\n", val);
|
1575 |
for(i = 0; i < 4; i++) |
1576 |
printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
|
1577 |
} |
1578 |
|
1579 |
/* self modifying code test */
|
1580 |
uint8_t code[] = { |
1581 |
0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */ |
1582 |
0xc3, /* ret */ |
1583 |
}; |
1584 |
|
1585 |
asm("smc_code2:\n" |
1586 |
"movl 4(%esp), %eax\n"
|
1587 |
"movl %eax, smc_patch_addr2 + 1\n"
|
1588 |
"nop\n"
|
1589 |
"nop\n"
|
1590 |
"nop\n"
|
1591 |
"nop\n"
|
1592 |
"nop\n"
|
1593 |
"nop\n"
|
1594 |
"nop\n"
|
1595 |
"nop\n"
|
1596 |
"smc_patch_addr2:\n"
|
1597 |
"movl $1, %eax\n"
|
1598 |
"ret\n");
|
1599 |
|
1600 |
typedef int FuncType(void); |
1601 |
extern int smc_code2(int); |
1602 |
void test_self_modifying_code(void) |
1603 |
{ |
1604 |
int i;
|
1605 |
|
1606 |
printf("self modifying code:\n");
|
1607 |
printf("func1 = 0x%x\n", ((FuncType *)code)());
|
1608 |
for(i = 2; i <= 4; i++) { |
1609 |
code[1] = i;
|
1610 |
printf("func%d = 0x%x\n", i, ((FuncType *)code)());
|
1611 |
} |
1612 |
|
1613 |
/* more difficult test : the modified code is just after the
|
1614 |
modifying instruction. It is forbidden in Intel specs, but it
|
1615 |
is used by old DOS programs */
|
1616 |
for(i = 2; i <= 4; i++) { |
1617 |
printf("smc_code2(%d) = %d\n", i, smc_code2(i));
|
1618 |
} |
1619 |
} |
1620 |
|
1621 |
static void *call_end __init_call = NULL; |
1622 |
|
1623 |
int main(int argc, char **argv) |
1624 |
{ |
1625 |
void **ptr;
|
1626 |
void (*func)(void); |
1627 |
|
1628 |
ptr = &call_start + 1;
|
1629 |
while (*ptr != NULL) { |
1630 |
func = *ptr++; |
1631 |
func(); |
1632 |
} |
1633 |
test_bsx(); |
1634 |
test_mul(); |
1635 |
test_jcc(); |
1636 |
test_floats(); |
1637 |
test_bcd(); |
1638 |
test_xchg(); |
1639 |
test_string(); |
1640 |
test_misc(); |
1641 |
test_lea(); |
1642 |
test_segs(); |
1643 |
test_code16(); |
1644 |
test_vm86(); |
1645 |
test_exceptions(); |
1646 |
test_self_modifying_code(); |
1647 |
test_single_step(); |
1648 |
return 0; |
1649 |
} |