]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
29 #include <sys/ucontext.h>
35 //#define LINUX_VM86_IOPL_FIX
36 //#define TEST_P4_FLAGS
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
50 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
52 static void *call_start __init_call
= NULL
;
54 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
57 #include "test-i386.h"
60 #include "test-i386.h"
63 #include "test-i386.h"
66 #include "test-i386.h"
69 #include "test-i386.h"
72 #include "test-i386.h"
76 #include "test-i386.h"
80 #include "test-i386.h"
85 #include "test-i386.h"
90 #include "test-i386.h"
95 #include "test-i386.h"
100 #include "test-i386.h"
103 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
106 #include "test-i386-shift.h"
109 #include "test-i386-shift.h"
112 #include "test-i386-shift.h"
115 #include "test-i386-shift.h"
118 #include "test-i386-shift.h"
122 #include "test-i386-shift.h"
126 #include "test-i386-shift.h"
131 #include "test-i386-shift.h"
136 #include "test-i386-shift.h"
138 /* XXX: should be more precise ? */
140 #define CC_MASK (CC_C)
144 #include "test-i386-shift.h"
148 #include "test-i386-shift.h"
152 #include "test-i386-shift.h"
156 #include "test-i386-shift.h"
158 /* lea test (modrm support) */
159 #define TEST_LEA(STR)\
161 asm("leal " STR ", %0"\
163 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
164 printf("lea %s = %08x\n", STR, res);\
167 #define TEST_LEA16(STR)\
169 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
171 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
172 printf("lea %s = %08x\n", STR, res);\
178 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
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)");
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)");
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)");
216 TEST_LEA("0x40(%%eax, %%ecx)");
217 TEST_LEA("0x4000(%%ebx, %%edx)");
219 TEST_LEA("(%%ecx, %%ecx, 2)");
220 TEST_LEA("(%%edx, %%ecx, 4)");
221 TEST_LEA("(%%esi, %%ecx, 8)");
223 TEST_LEA("(,%%eax, 2)");
224 TEST_LEA("(,%%ebx, 4)");
225 TEST_LEA("(,%%ecx, 8)");
227 TEST_LEA("0x40(,%%eax, 2)");
228 TEST_LEA("0x40(,%%ebx, 4)");
229 TEST_LEA("0x40(,%%ecx, 8)");
232 TEST_LEA("-10(%%ecx, %%ecx, 2)");
233 TEST_LEA("-10(%%edx, %%ecx, 4)");
234 TEST_LEA("-10(%%esi, %%ecx, 8)");
236 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
237 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
238 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
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)");
258 #define TEST_JCC(JCC, v1, v2)\
261 asm("movl $1, %0\n\t"\
267 : "r" (v1), "r" (v2));\
268 printf("%-10s %d\n", "j" JCC, res);\
270 asm("movl $0, %0\n\t"\
272 "set" JCC " %b0\n\t"\
274 : "r" (v1), "r" (v2));\
275 printf("%-10s %d\n", "set" JCC, res);\
277 asm("movl $0x12345678, %0\n\t"\
279 "cmov" JCC "l %3, %0\n\t"\
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"\
285 "cmov" JCC "w %w3, %w0\n\t"\
287 : "r" (v1), "r" (v2), "r" (1));\
288 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
292 /* various jump tests */
295 TEST_JCC("ne", 1, 1);
296 TEST_JCC("ne", 1, 0);
303 TEST_JCC("l", 1, -1);
305 TEST_JCC("le", 1, 1);
306 TEST_JCC("le", 1, 0);
307 TEST_JCC("le", 1, -1);
309 TEST_JCC("ge", 1, 1);
310 TEST_JCC("ge", 1, 0);
311 TEST_JCC("ge", -1, 1);
315 TEST_JCC("g", 1, -1);
319 TEST_JCC("b", 1, -1);
321 TEST_JCC("be", 1, 1);
322 TEST_JCC("be", 1, 0);
323 TEST_JCC("be", 1, -1);
325 TEST_JCC("ae", 1, 1);
326 TEST_JCC("ae", 1, 0);
327 TEST_JCC("ae", 1, -1);
331 TEST_JCC("a", 1, -1);
337 TEST_JCC("np", 1, 1);
338 TEST_JCC("np", 1, 0);
340 TEST_JCC("o", 0x7fffffff, 0);
341 TEST_JCC("o", 0x7fffffff, -1);
343 TEST_JCC("no", 0x7fffffff, 0);
344 TEST_JCC("no", 0x7fffffff, -1);
347 TEST_JCC("s", 0, -1);
350 TEST_JCC("ns", 0, 1);
351 TEST_JCC("ns", 0, -1);
352 TEST_JCC("ns", 0, 0);
357 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
359 #define CC_MASK (CC_O | CC_C)
363 #include "test-i386-muldiv.h"
366 #include "test-i386-muldiv.h"
368 void test_imulw2(int op0
, int op1
)
370 int res
, s1
, s0
, flags
;
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
);
386 void test_imull2(int op0
, int op1
)
388 int res
, s1
, s0
, flags
;
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
);
404 #define TEST_IMUL_IM(size, size1, op0, op1)\
411 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\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);\
425 #include "test-i386-muldiv.h"
428 #include "test-i386-muldiv.h"
432 test_imulb(0x1234561d, 4);
434 test_imulb(0x80, 0x80);
435 test_imulb(0x10, 0x10);
437 test_imulw(0, 0x1234001d, 45);
438 test_imulw(0, 23, -45);
439 test_imulw(0, 0x8000, 0x8000);
440 test_imulw(0, 0x100, 0x100);
442 test_imull(0, 0x1234001d, 45);
443 test_imull(0, 23, -45);
444 test_imull(0, 0x80000000, 0x80000000);
445 test_imull(0, 0x10000, 0x10000);
447 test_mulb(0x1234561d, 4);
449 test_mulb(0x80, 0x80);
450 test_mulb(0x10, 0x10);
452 test_mulw(0, 0x1234001d, 45);
453 test_mulw(0, 23, -45);
454 test_mulw(0, 0x8000, 0x8000);
455 test_mulw(0, 0x100, 0x100);
457 test_mull(0, 0x1234001d, 45);
458 test_mull(0, 23, -45);
459 test_mull(0, 0x80000000, 0x80000000);
460 test_mull(0, 0x10000, 0x10000);
462 test_imulw2(0x1234001d, 45);
463 test_imulw2(23, -45);
464 test_imulw2(0x8000, 0x8000);
465 test_imulw2(0x100, 0x100);
467 test_imull2(0x1234001d, 45);
468 test_imull2(23, -45);
469 test_imull2(0x80000000, 0x80000000);
470 test_imull2(0x10000, 0x10000);
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);
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);
482 test_idivb(0x12341678, 0x127e);
483 test_idivb(0x43210123, -5);
484 test_idivb(0x12340004, -1);
486 test_idivw(0, 0x12345678, 12347);
487 test_idivw(0, -23223, -45);
488 test_idivw(0, 0x12348000, -1);
489 test_idivw(0x12343, 0x12345678, 0x81238567);
491 test_idivl(0, 0x12345678, 12347);
492 test_idivl(0, -233223, -45);
493 test_idivl(0, 0x80000000, -1);
494 test_idivl(0x12343, 0x12345678, 0x81234567);
496 test_divb(0x12341678, 0x127e);
497 test_divb(0x43210123, -5);
498 test_divb(0x12340004, -1);
500 test_divw(0, 0x12345678, 12347);
501 test_divw(0, -23223, -45);
502 test_divw(0, 0x12348000, -1);
503 test_divw(0x12343, 0x12345678, 0x81238567);
505 test_divl(0, 0x12345678, 12347);
506 test_divl(0, -233223, -45);
507 test_divl(0, 0x80000000, -1);
508 test_divl(0x12343, 0x12345678, 0x81234567);
511 #define TEST_BSX(op, size, op0)\
515 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
516 : "=r" (res), "=q" (resz)\
518 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
523 TEST_BSX(bsrw
, "w", 0);
524 TEST_BSX(bsrw
, "w", 0x12340128);
525 TEST_BSX(bsrl
, "", 0);
526 TEST_BSX(bsrl
, "", 0x00340128);
527 TEST_BSX(bsfw
, "w", 0);
528 TEST_BSX(bsfw
, "w", 0x12340128);
529 TEST_BSX(bsfl
, "", 0);
530 TEST_BSX(bsfl
, "", 0x00340128);
533 /**********************************************/
535 void test_fops(double a
, double b
)
537 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
538 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
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 fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
542 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
543 printf("a=%f sin(a)=%f\n", a
, sin(a
));
544 printf("a=%f cos(a)=%f\n", a
, cos(a
));
545 printf("a=%f tan(a)=%f\n", a
, tan(a
));
546 printf("a=%f log(a)=%f\n", a
, log(a
));
547 printf("a=%f exp(a)=%f\n", a
, exp(a
));
548 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
549 /* just to test some op combining */
550 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
551 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
552 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
556 void test_fcmp(double a
, double b
)
558 printf("(%f<%f)=%d\n",
560 printf("(%f<=%f)=%d\n",
562 printf("(%f==%f)=%d\n",
564 printf("(%f>%f)=%d\n",
566 printf("(%f<=%f)=%d\n",
570 /* test f(u)comi instruction */
576 printf("fcomi(%f %f)=%08x\n", a
, b
, eflags
& (CC_Z
| CC_P
| CC_C
));
580 void test_fcvt(double a
)
593 printf("(float)%f = %f\n", a
, fa
);
594 printf("(long double)%f = %Lf\n", a
, la
);
595 printf("a=%016Lx\n", *(long long *)&a
);
596 printf("la=%016Lx %04x\n", *(long long *)&la
,
597 *(unsigned short *)((char *)(&la
) + 8));
599 /* test all roundings */
600 asm volatile ("fstcw %0" : "=m" (fpuc
));
602 asm volatile ("fldcw %0" : : "m" ((fpuc
& ~0x0c00) | (i
<< 10)));
603 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
604 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
605 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
606 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
607 asm volatile ("fldcw %0" : : "m" (fpuc
));
608 printf("(short)a = %d\n", wa
);
609 printf("(int)a = %d\n", ia
);
610 printf("(int64_t)a = %Ld\n", lla
);
611 printf("rint(a) = %f\n", ra
);
616 asm("fld" #N : "=t" (a)); \
617 printf("fld" #N "= %f\n", a);
619 void test_fconst(void)
631 void test_fbcd(double a
)
633 unsigned short bcd
[5];
636 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
637 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
638 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
639 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
642 #define TEST_ENV(env, save, restore)\
644 memset((env), 0xaa, sizeof(*(env)));\
646 asm volatile ("fldl %0" : : "m" (dtab[i]));\
647 asm(save " %0\n" : : "m" (*(env)));\
648 asm(restore " %0\n": : "m" (*(env)));\
650 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
652 printf("res[%d]=%f\n", i, rtab[i]);\
653 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
655 (env)->fpus & 0xff00,\
661 struct __attribute__((packed
)) {
669 long double fpregs
[8];
671 struct __attribute__((packed
)) {
676 long double fpregs
[8];
685 TEST_ENV(&float_env16
, "data16 fnstenv", "data16 fldenv");
686 TEST_ENV(&float_env16
, "data16 fnsave", "data16 frstor");
687 TEST_ENV(&float_env32
, "fnstenv", "fldenv");
688 TEST_ENV(&float_env32
, "fnsave", "frstor");
692 #define TEST_FCMOV(a, b, eflags, CC)\
697 "fcmov" CC " %2, %0\n"\
699 : "0" (a), "u" (b), "g" (eflags));\
700 printf("fcmov%s eflags=0x%04x-> %f\n", \
704 void test_fcmov(void)
711 for(i
= 0; i
< 4; i
++) {
717 TEST_FCMOV(a
, b
, eflags
, "b");
718 TEST_FCMOV(a
, b
, eflags
, "e");
719 TEST_FCMOV(a
, b
, eflags
, "be");
720 TEST_FCMOV(a
, b
, eflags
, "nb");
721 TEST_FCMOV(a
, b
, eflags
, "ne");
722 TEST_FCMOV(a
, b
, eflags
, "nbe");
724 TEST_FCMOV(a
, b
, 0, "u");
725 TEST_FCMOV(a
, b
, CC_P
, "u");
726 TEST_FCMOV(a
, b
, 0, "nu");
727 TEST_FCMOV(a
, b
, CC_P
, "nu");
730 void test_floats(void)
744 test_fbcd(1234567890123456);
745 test_fbcd(-123451234567890);
752 /**********************************************/
754 #define TEST_BCD(op, op0, cc_in, cc_mask)\
764 : "=a" (res), "=g" (flags)\
765 : "0" (res), "1" (flags));\
766 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
767 #op, op0, res, cc_in, flags & cc_mask);\
772 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
773 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
774 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
775 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
776 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
777 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
778 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
779 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
780 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
781 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
782 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
783 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
784 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
786 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
787 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
788 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
789 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
790 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
791 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
792 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
793 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
794 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
795 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
796 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
797 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
798 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
800 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
801 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
802 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
803 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
804 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
805 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
806 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
807 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
809 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
810 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
811 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
812 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
813 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
814 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
815 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
816 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
818 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
819 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
822 #define TEST_XCHG(op, size, opconst)\
827 asm(#op " %" size "0, %" size "1" \
828 : "=q" (op0), opconst (op1) \
829 : "0" (op0), "1" (op1));\
830 printf("%-10s A=%08x B=%08x\n",\
834 #define TEST_CMPXCHG(op, size, opconst, eax)\
839 asm(#op " %" size "0, %" size "1" \
840 : "=q" (op0), opconst (op1) \
841 : "0" (op0), "1" (op1), "a" (eax));\
842 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
843 #op, eax, op0, op1);\
848 TEST_XCHG(xchgl
, "", "=q");
849 TEST_XCHG(xchgw
, "w", "=q");
850 TEST_XCHG(xchgb
, "b", "=q");
852 TEST_XCHG(xchgl
, "", "=m");
853 TEST_XCHG(xchgw
, "w", "=m");
854 TEST_XCHG(xchgb
, "b", "=m");
856 TEST_XCHG(xaddl
, "", "=q");
857 TEST_XCHG(xaddw
, "w", "=q");
858 TEST_XCHG(xaddb
, "b", "=q");
863 asm("xaddl %1, %0" : "=r" (res
) : "0" (res
));
864 printf("xaddl same res=%08x\n", res
);
867 TEST_XCHG(xaddl
, "", "=m");
868 TEST_XCHG(xaddw
, "w", "=m");
869 TEST_XCHG(xaddb
, "b", "=m");
871 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
872 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
873 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
875 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
876 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
877 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
879 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
880 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
881 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
883 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
884 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
885 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
888 uint64_t op0
, op1
, op2
;
891 for(i
= 0; i
< 2; i
++) {
892 op0
= 0x123456789abcd;
894 op1
= 0xfbca765423456;
897 op2
= 0x6532432432434;
901 : "=A" (op0
), "=m" (op1
), "=g" (eflags
)
902 : "0" (op0
), "m" (op1
), "b" ((int)op2
), "c" ((int)(op2
>> 32)));
903 printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
904 op0
, op1
, eflags
& CC_Z
);
909 /**********************************************/
910 /* segmentation tests */
913 #include <linux/unistd.h>
914 #include <linux/version.h>
916 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
918 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
919 #define modify_ldt_ldt_s user_desc
922 uint8_t seg_data1
[4096];
923 uint8_t seg_data2
[4096];
925 #define MK_SEL(n) (((n) << 3) | 7)
927 #define TEST_LR(op, size, seg, mask)\
931 asm (op " %" size "2, %" size "0\n" \
936 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
937 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
940 /* NOTE: we use Linux modify_ldt syscall */
943 struct modify_ldt_ldt_s ldt
;
944 long long ldt_table
[3];
950 } __attribute__((packed
)) segoff
;
952 ldt
.entry_number
= 1;
953 ldt
.base_addr
= (unsigned long)&seg_data1
;
954 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
956 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
957 ldt
.read_exec_only
= 0;
958 ldt
.limit_in_pages
= 1;
959 ldt
.seg_not_present
= 0;
961 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
963 ldt
.entry_number
= 2;
964 ldt
.base_addr
= (unsigned long)&seg_data2
;
965 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
967 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
968 ldt
.read_exec_only
= 0;
969 ldt
.limit_in_pages
= 1;
970 ldt
.seg_not_present
= 0;
972 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
974 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
979 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
982 /* do some tests with fs or gs */
983 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
988 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
989 printf("FS[1] = %02x\n", res
);
991 asm volatile ("pushl %%gs\n"
993 "gs movzbl 0x1, %0\n"
997 printf("GS[1] = %02x\n", res
);
999 /* tests with ds/ss (implicit segment case) */
1001 asm volatile ("pushl %%ebp\n\t"
1004 "movl %3, %%ebp\n\t"
1005 "movzbl 0x1, %0\n\t"
1006 "movzbl (%%ebp), %1\n\t"
1009 : "=r" (res
), "=r" (res2
)
1010 : "r" (MK_SEL(1)), "r" (&tmp
));
1011 printf("DS[1] = %02x\n", res
);
1012 printf("SS[tmp] = %02x\n", res2
);
1014 segoff
.seg
= MK_SEL(2);
1015 segoff
.offset
= 0xabcdef12;
1016 asm volatile("lfs %2, %0\n\t"
1018 : "=r" (res
), "=g" (res2
)
1020 printf("FS:reg = %04x:%08x\n", res2
, res
);
1022 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1023 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1024 TEST_LR("lslw", "w", MK_SEL(2), 0);
1025 TEST_LR("lsll", "", MK_SEL(2), 0);
1027 TEST_LR("larw", "w", 0xfff8, 0);
1028 TEST_LR("larl", "", 0xfff8, 0);
1029 TEST_LR("lslw", "w", 0xfff8, 0);
1030 TEST_LR("lsll", "", 0xfff8, 0);
1033 /* 16 bit code test */
1034 extern char code16_start
, code16_end
;
1035 extern char code16_func1
;
1036 extern char code16_func2
;
1037 extern char code16_func3
;
1039 void test_code16(void)
1041 struct modify_ldt_ldt_s ldt
;
1044 /* build a code segment */
1045 ldt
.entry_number
= 1;
1046 ldt
.base_addr
= (unsigned long)&code16_start
;
1047 ldt
.limit
= &code16_end
- &code16_start
;
1049 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
1050 ldt
.read_exec_only
= 0;
1051 ldt
.limit_in_pages
= 0;
1052 ldt
.seg_not_present
= 0;
1054 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1056 /* call the first function */
1057 asm volatile ("lcall %1, %2"
1059 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
1060 printf("func1() = 0x%08x\n", res
);
1061 asm volatile ("lcall %2, %3"
1062 : "=a" (res
), "=c" (res2
)
1063 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
1064 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
1065 asm volatile ("lcall %1, %2"
1067 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
1068 printf("func3() = 0x%08x\n", res
);
1071 extern char func_lret32
;
1072 extern char func_iret32
;
1074 void test_misc(void)
1079 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
1081 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
1082 printf("xlat: EAX=%08x\n", res
);
1084 asm volatile ("pushl %%cs ; call %1"
1086 : "m" (func_lret32
): "memory", "cc");
1087 printf("func_lret32=%x\n", res
);
1089 asm volatile ("pushfl ; pushl %%cs ; call %1"
1091 : "m" (func_iret32
): "memory", "cc");
1092 printf("func_iret32=%x\n", res
);
1094 /* specific popl test */
1095 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1097 printf("popl esp=%x\n", res
);
1099 /* specific popw test */
1100 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1102 printf("popw esp=%x\n", res
);
1105 uint8_t str_buffer
[4096];
1107 #define TEST_STRING1(OP, size, DF, REP)\
1109 int esi, edi, eax, ecx, eflags;\
1111 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1112 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1116 asm volatile ("pushl $0\n\t"\
1119 REP #OP size "\n\t"\
1123 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1124 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1125 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1126 REP #OP size, esi, edi, eax, ecx,\
1127 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1130 #define TEST_STRING(OP, REP)\
1131 TEST_STRING1(OP, "b", "", REP);\
1132 TEST_STRING1(OP, "w", "", REP);\
1133 TEST_STRING1(OP, "l", "", REP);\
1134 TEST_STRING1(OP, "b", "std", REP);\
1135 TEST_STRING1(OP, "w", "std", REP);\
1136 TEST_STRING1(OP, "l", "std", REP)
1138 void test_string(void)
1141 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1142 str_buffer
[i
] = i
+ 0x56;
1143 TEST_STRING(stos
, "");
1144 TEST_STRING(stos
, "rep ");
1145 TEST_STRING(lods
, ""); /* to verify stos */
1146 TEST_STRING(lods
, "rep ");
1147 TEST_STRING(movs
, "");
1148 TEST_STRING(movs
, "rep ");
1149 TEST_STRING(lods
, ""); /* to verify stos */
1151 /* XXX: better tests */
1152 TEST_STRING(scas
, "");
1153 TEST_STRING(scas
, "repz ");
1154 TEST_STRING(scas
, "repnz ");
1155 TEST_STRING(cmps
, "");
1156 TEST_STRING(cmps
, "repz ");
1157 TEST_STRING(cmps
, "repnz ");
1162 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1164 a
[bit
/ 8] |= (1 << (bit
% 8));
1167 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1169 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1172 static inline void pushw(struct vm86_regs
*r
, int val
)
1174 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1175 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1178 #undef __syscall_return
1179 #define __syscall_return(type, res) \
1181 return (type) (res); \
1184 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
1186 extern char vm86_code_start
;
1187 extern char vm86_code_end
;
1189 #define VM86_CODE_CS 0x100
1190 #define VM86_CODE_IP 0x100
1192 void test_vm86(void)
1194 struct vm86plus_struct ctx
;
1195 struct vm86_regs
*r
;
1199 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1200 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1201 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1202 if (vm86_mem
== MAP_FAILED
) {
1203 printf("ERROR: could not map vm86 memory");
1206 memset(&ctx
, 0, sizeof(ctx
));
1208 /* init basic registers */
1210 r
->eip
= VM86_CODE_IP
;
1219 r
->eflags
= VIF_MASK
;
1221 /* move code to proper address. We use the same layout as a .com
1223 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1224 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1226 /* mark int 0x21 as being emulated */
1227 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1230 ret
= vm86(VM86_ENTER
, &ctx
);
1231 switch(VM86_TYPE(ret
)) {
1236 int_num
= VM86_ARG(ret
);
1237 if (int_num
!= 0x21)
1239 ah
= (r
->eax
>> 8) & 0xff;
1241 case 0x00: /* exit */
1243 case 0x02: /* write char */
1249 case 0x09: /* write string */
1252 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1259 r
->eax
= (r
->eax
& ~0xff) | '$';
1262 case 0xff: /* extension: write eflags number in edx */
1264 #ifndef LINUX_VM86_IOPL_FIX
1267 printf("%08x\n", v
);
1271 printf("unsupported int 0x%02x\n", int_num
);
1277 /* a signal came, we just ignore that */
1282 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1287 printf("VM86 end\n");
1288 munmap(vm86_mem
, 0x110000);
1291 /* exception tests */
1303 #define REG_TRAPNO TRAPNO
1311 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1313 struct ucontext
*uc
= puc
;
1315 printf("si_signo=%d si_errno=%d si_code=%d",
1316 info
->si_signo
, info
->si_errno
, info
->si_code
);
1317 printf(" si_addr=0x%08lx",
1318 (unsigned long)info
->si_addr
);
1321 printf("trapno=0x%02x err=0x%08x",
1322 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1323 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1324 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1326 longjmp(jmp_env
, 1);
1329 void test_exceptions(void)
1331 struct modify_ldt_ldt_s ldt
;
1332 struct sigaction act
;
1335 act
.sa_sigaction
= sig_handler
;
1336 sigemptyset(&act
.sa_mask
);
1337 act
.sa_flags
= SA_SIGINFO
;
1338 sigaction(SIGFPE
, &act
, NULL
);
1339 sigaction(SIGILL
, &act
, NULL
);
1340 sigaction(SIGSEGV
, &act
, NULL
);
1341 sigaction(SIGBUS
, &act
, NULL
);
1342 sigaction(SIGTRAP
, &act
, NULL
);
1344 /* test division by zero reporting */
1345 printf("DIVZ exception:\n");
1346 if (setjmp(jmp_env
) == 0) {
1347 /* now divide by zero */
1352 printf("BOUND exception:\n");
1353 if (setjmp(jmp_env
) == 0) {
1354 /* bound exception */
1357 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
));
1360 printf("segment exceptions:\n");
1361 if (setjmp(jmp_env
) == 0) {
1362 /* load an invalid segment */
1363 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1365 if (setjmp(jmp_env
) == 0) {
1366 /* null data segment is valid */
1367 asm volatile ("movl %0, %%fs" : : "r" (3));
1368 /* null stack segment */
1369 asm volatile ("movl %0, %%ss" : : "r" (3));
1372 ldt
.entry_number
= 1;
1373 ldt
.base_addr
= (unsigned long)&seg_data1
;
1374 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1376 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1377 ldt
.read_exec_only
= 0;
1378 ldt
.limit_in_pages
= 1;
1379 ldt
.seg_not_present
= 1;
1381 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1383 if (setjmp(jmp_env
) == 0) {
1384 /* segment not present */
1385 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1388 /* test SEGV reporting */
1389 printf("PF exception:\n");
1390 if (setjmp(jmp_env
) == 0) {
1392 /* we add a nop to test a weird PC retrieval case */
1393 asm volatile ("nop");
1394 /* now store in an invalid address */
1395 *(char *)0x1234 = 1;
1398 /* test SEGV reporting */
1399 printf("PF exception:\n");
1400 if (setjmp(jmp_env
) == 0) {
1402 /* read from an invalid address */
1403 v1
= *(char *)0x1234;
1406 /* test illegal instruction reporting */
1407 printf("UD2 exception:\n");
1408 if (setjmp(jmp_env
) == 0) {
1409 /* now execute an invalid instruction */
1410 asm volatile("ud2");
1412 printf("lock nop exception:\n");
1413 if (setjmp(jmp_env
) == 0) {
1414 /* now execute an invalid instruction */
1415 asm volatile("lock nop");
1418 printf("INT exception:\n");
1419 if (setjmp(jmp_env
) == 0) {
1420 asm volatile ("int $0xfd");
1422 if (setjmp(jmp_env
) == 0) {
1423 asm volatile ("int $0x01");
1425 if (setjmp(jmp_env
) == 0) {
1426 asm volatile (".byte 0xcd, 0x03");
1428 if (setjmp(jmp_env
) == 0) {
1429 asm volatile ("int $0x04");
1431 if (setjmp(jmp_env
) == 0) {
1432 asm volatile ("int $0x05");
1435 printf("INT3 exception:\n");
1436 if (setjmp(jmp_env
) == 0) {
1437 asm volatile ("int3");
1440 printf("CLI exception:\n");
1441 if (setjmp(jmp_env
) == 0) {
1442 asm volatile ("cli");
1445 printf("STI exception:\n");
1446 if (setjmp(jmp_env
) == 0) {
1447 asm volatile ("cli");
1450 printf("INTO exception:\n");
1451 if (setjmp(jmp_env
) == 0) {
1452 /* overflow exception */
1453 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1456 printf("OUTB exception:\n");
1457 if (setjmp(jmp_env
) == 0) {
1458 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1461 printf("INB exception:\n");
1462 if (setjmp(jmp_env
) == 0) {
1463 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1466 printf("REP OUTSB exception:\n");
1467 if (setjmp(jmp_env
) == 0) {
1468 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1471 printf("REP INSB exception:\n");
1472 if (setjmp(jmp_env
) == 0) {
1473 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1476 printf("HLT exception:\n");
1477 if (setjmp(jmp_env
) == 0) {
1478 asm volatile ("hlt");
1481 printf("single step exception:\n");
1483 if (setjmp(jmp_env
) == 0) {
1484 asm volatile ("pushf\n"
1485 "orl $0x00100, (%%esp)\n"
1487 "movl $0xabcd, %0\n"
1488 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1490 printf("val=0x%x\n", val
);
1493 /* specific precise single step test */
1494 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1496 struct ucontext
*uc
= puc
;
1497 printf("EIP=0x%08x\n", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1500 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1501 uint8_t sstep_buf2
[4];
1503 void test_single_step(void)
1505 struct sigaction act
;
1510 act
.sa_sigaction
= sig_trap_handler
;
1511 sigemptyset(&act
.sa_mask
);
1512 act
.sa_flags
= SA_SIGINFO
;
1513 sigaction(SIGTRAP
, &act
, NULL
);
1514 asm volatile ("pushf\n"
1515 "orl $0x00100, (%%esp)\n"
1517 "movl $0xabcd, %0\n"
1526 /* movsb: the single step should stop at each movsb iteration */
1527 "movl $sstep_buf1, %%esi\n"
1528 "movl $sstep_buf2, %%edi\n"
1536 /* cmpsb: the single step should stop at each cmpsb iteration */
1537 "movl $sstep_buf1, %%esi\n"
1538 "movl $sstep_buf2, %%edi\n"
1544 /* getpid() syscall: single step should skip one
1550 /* when modifying SS, trace is not done on the next
1552 "movl %%ss, %%ecx\n"
1553 "movl %%ecx, %%ss\n"
1556 "movl %%ecx, %%ss\n"
1567 "andl $~0x00100, (%%esp)\n"
1571 : "cc", "memory", "eax", "ecx", "esi", "edi");
1572 printf("val=%d\n", val
);
1573 for(i
= 0; i
< 4; i
++)
1574 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1577 /* self modifying code test */
1579 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1584 "movl 4(%esp), %eax\n"
1585 "movl %eax, smc_patch_addr2 + 1\n"
1594 "smc_patch_addr2:\n"
1598 typedef int FuncType(void);
1599 extern int smc_code2(int);
1600 void test_self_modifying_code(void)
1604 printf("self modifying code:\n");
1605 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
1606 for(i
= 2; i
<= 4; i
++) {
1608 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
1611 /* more difficult test : the modified code is just after the
1612 modifying instruction. It is forbidden in Intel specs, but it
1613 is used by old DOS programs */
1614 for(i
= 2; i
<= 4; i
++) {
1615 printf("smc_code2(%d) = %d\n", i
, smc_code2(i
));
1619 static void *call_end __init_call
= NULL
;
1621 int main(int argc
, char **argv
)
1626 ptr
= &call_start
+ 1;
1627 while (*ptr
!= NULL
) {
1644 test_self_modifying_code();