another tcg bug? test r,r case in tcg_out_brcond2()
Hi!
While trying to find out why a FreeBSD/amd64 guest on an i386 host
fails to mount its /usr from ad0s1d on a 5G images passed as -hda partitioned like this:
zsh saturn# fdisk -s ad0/dev/ad0: 652 cyl 255 hd 63 sec
Part Start Size Type Flags
1: 63 10485153 0xa5 0x80
zsh saturn# bsdlabel ad0s1
# /dev/ad0s1:
8 partitions:
# size offset fstype [fsize bsize bps/cpg]
a: 1048576 1048576 4.2BSD 2048 16384 8
b: 1048576 0 swap
c: 10485153 0 unused 0 0 # "raw" part, don't edit
d: 8388001 2097152 4.2BSD 2048 16384 28528
(the sizes and offsets are in 512 byte blocks, so its a 512 MB swap on
ad0s1b, followed by a 512 MB / on ad0s1a and a 4 GB /usr on ad0s1d), I came across the following translation of a
if (bp->bio_offset < 0)
line that checks the disk offset in g_io_check() in:
http://svn.freebsd.org/viewvc/base/head/sys/geom/geom_io.c
IN:
0xffffffff8044b2d0: test %r8,%r8 0xffffffff8044b2d3: js 0xffffffff8044b2db
OUT: [size=135]
0x2945c960: mov 0x40(%ebp),%eax 0x2945c963: mov 0x44(%ebp),%edx 0x2945c966: mov 0x40(%ebp),%ecx 0x2945c969: mov 0x44(%ebp),%ebx 0x2945c96c: and %ecx,%eax 0x2945c96e: and %ebx,%edx 0x2945c970: mov $0x19,%ecx 0x2945c975: mov %ecx,0xa0(%ebp) 0x2945c97b: mov %eax,0x98(%ebp) 0x2945c981: mov %edx,0x9c(%ebp) 0x2945c987: test %edx,%edx 0x2945c989: jl 0x2945c9c2 0x2945c98f: jne 0x2945c99d ; ????? 0x2945c995: test %eax,%eax ; ????? 0x2945c997: jl 0x2945c9c2 ; ????? 0x2945c99d: jmp 0x2945c9a2 0x2945c9a2: mov $0x8044b2d5,%eax 0x2945c9a7: mov %eax,0x80(%ebp) 0x2945c9ad: mov $0xffffffff,%eax 0x2945c9b2: mov %eax,0x84(%ebp) 0x2945c9b8: mov $0x112e5580,%eax 0x2945c9bd: jmp 0x83bdbe8 0x2945c9c2: jmp 0x2945c9c7 0x2945c9c7: mov $0x8044b2db,%eax 0x2945c9cc: mov %eax,0x80(%ebp) 0x2945c9d2: mov $0xffffffff,%eax 0x2945c9d7: mov %eax,0x84(%ebp) 0x2945c9dd: mov $0x112e5581,%eax 0x2945c9e2: jmp 0x83bdbe8
Am I right the marked insns cause the branch to also be taken when the
higher half of r8 is zero and the lower half has bit 31 set? (like the start offset of the /usr partition of the mentioned images gets...) And if yes, is the following patch correct that adds special handling for the `test r,r' case to the i386 version of tcg_out_brcond2()?
Index: qemu/tcg/i386/tcg-target.c
@@ -350,49 +350,83 @@
label_next = gen_new_label();
switch(args[4]) {
case TCG_COND_EQ:
+ cond_eq:
tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], label_next);
tcg_out_brcond(s, TCG_COND_EQ, args[1], args[3], const_args[3], args[5]);
break;
case TCG_COND_NE:
+ cond_ne:
tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], args[5]);
tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], args[5]);
break;
case TCG_COND_LT:
tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
+ if (const_args[2] && !args[2])
+ /* test r,r */
+ break;
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_LT, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_LE:
tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
+ if (const_args[2] && !args[2]) {
+ /* test r,r */
+ tcg_out_brcond(s, TCG_COND_EQ, args[0], args[2], const_args[2], args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_LE, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GT:
tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
+ if (const_args[2] && !args[2]) {
+ /* test r,r */
+ tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_GT, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GE:
+ if (const_args[2] && !args[2]) {
+ /* test r,r */
+ tcg_out_brcond(s, TCG_COND_GE, args[1], args[3], const_args[3], args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_GE, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_LTU:
+ if (const_args[2] && !args[2])
+ /* test r,r */
+ break;
tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_LEU:
+ if (const_args[2] && !args[2])
+ /* test r,r */
+ goto cond_eq;
tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GTU:
+ if (const_args[2] && !args[2])
+ /* test r,r */
+ goto cond_ne;
tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GEU:
+ if (const_args[2] && !args[2]) {
+ /* test r,r */
+ tcg_out_jxx(s, JCC_JMP, args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]); Unfortunately that patch doesnt seem to be enough i.e. /usr still fails
to mount. Have I missed another case of this problem? The following
if (bp->bio_offset > pp->mediasize)
line that might also cause the mount failure gets translated like the follwoing, can anyone see a bug there?
IN:
0xffffffff8044b2d5: cmp 0x48(%rdi),%r8 0xffffffff8044b2d9: jle 0xffffffff8044b33a
OUT: [size=227]
0x29487ec0: mov 0x38(%ebp),%eax 0x29487ec3: mov 0x3c(%ebp),%edx 0x29487ec6: add $0x48,%eax 0x29487ec9: adc $0x0,%edx 0x29487ecc: mov %eax,%ecx 0x29487ece: mov %edx,%ebx 0x29487ed0: mov %ecx,%edx 0x29487ed2: mov %ecx,%eax 0x29487ed4: shr $0x7,%edx 0x29487ed7: and $0xfffff007,%eax 0x29487edd: and $0x1fe0,%edx 0x29487ee3: lea 0x460(%edx,%ebp,1),%edx 0x29487eea: cmp (%edx),%eax 0x29487eec: mov %ecx,%eax 0x29487eee: jne 0x29487ef5 0x29487ef0: cmp 0x4(%edx),%ebx 0x29487ef3: je 0x29487f00 0x29487ef5: mov %ebx,%edx 0x29487ef7: xor %ecx,%ecx 0x29487ef9: call 0x8157a18 0x29487efe: jmp 0x29487f0a 0x29487f00: add 0x18(%edx),%eax 0x29487f03: mov %eax,%edx 0x29487f05: mov (%edx),%eax 0x29487f07: mov 0x4(%edx),%edx 0x29487f0a: mov 0x40(%ebp),%ecx 0x29487f0d: mov 0x44(%ebp),%ebx 0x29487f10: mov %eax,%esi 0x29487f12: mov %edx,%edi 0x29487f14: sub %eax,%ecx 0x29487f16: sbb %edx,%ebx 0x29487f18: mov %ecx,%eax 0x29487f1a: mov %ebx,%edx 0x29487f1c: add %esi,%eax 0x29487f1e: adc %edi,%edx 0x29487f20: mov %ecx,0x98(%ebp) 0x29487f26: mov $0x11,%ecx 0x29487f2b: mov %ecx,0xa0(%ebp) 0x29487f31: mov %esi,0x90(%ebp) 0x29487f37: mov %edi,0x94(%ebp) 0x29487f3d: mov %ebx,0x9c(%ebp) 0x29487f43: cmp %edi,%edx 0x29487f45: jl 0x29487f7e 0x29487f4b: jne 0x29487f59 0x29487f51: cmp %esi,%eax 0x29487f53: jle 0x29487f7e 0x29487f59: jmp 0x29487f5e 0x29487f5e: mov $0x8044b2db,%eax 0x29487f63: mov %eax,0x80(%ebp) 0x29487f69: mov $0xffffffff,%eax 0x29487f6e: mov %eax,0x84(%ebp) 0x29487f74: mov $0x112f1328,%eax 0x29487f79: jmp 0x83bd948 0x29487f7e: jmp 0x29487f83 0x29487f83: mov $0x8044b33a,%eax 0x29487f88: mov %eax,0x80(%ebp) 0x29487f8e: mov $0xffffffff,%eax 0x29487f93: mov %eax,0x84(%ebp) 0x29487f99: mov $0x112f1329,%eax 0x29487f9e: jmp 0x83bd948
Thanx,
Juergen
[Qemu-devel] fixed one bug, found another :) (was: another tcg bug? test r, r case in tcg_out_brcond2())
Quoted 51 lines · Juergen Lock
Hi!
[...]
Unfortunately that patch doesnt seem to be enough i.e. /usr still fails
to mount. Have I missed another case of this problem? The followingif (bp->bio_offset > pp->mediasize)line that might also cause the mount failure gets translated like the follwoing, can anyone see a bug there?
IN:
0xffffffff8044b2d5: cmp 0x48(%rdi),%r8 0xffffffff8044b2d9: jle 0xffffffff8044b33aOUT: [size=227]
0x29487ec0: mov 0x38(%ebp),%eax 0x29487ec3: mov 0x3c(%ebp),%edx 0x29487ec6: add $0x48,%eax 0x29487ec9: adc $0x0,%edx 0x29487ecc: mov %eax,%ecx 0x29487ece: mov %edx,%ebx 0x29487ed0: mov %ecx,%edx 0x29487ed2: mov %ecx,%eax 0x29487ed4: shr $0x7,%edx 0x29487ed7: and $0xfffff007,%eax 0x29487edd: and $0x1fe0,%edx 0x29487ee3: lea 0x460(%edx,%ebp,1),%edx 0x29487eea: cmp (%edx),%eax 0x29487eec: mov %ecx,%eax 0x29487eee: jne 0x29487ef5 0x29487ef0: cmp 0x4(%edx),%ebx 0x29487ef3: je 0x29487f00 0x29487ef5: mov %ebx,%edx 0x29487ef7: xor %ecx,%ecx 0x29487ef9: call 0x8157a18 0x29487efe: jmp 0x29487f0a 0x29487f00: add 0x18(%edx),%eax 0x29487f03: mov %eax,%edx 0x29487f05: mov (%edx),%eax 0x29487f07: mov 0x4(%edx),%edx 0x29487f0a: mov 0x40(%ebp),%ecx 0x29487f0d: mov 0x44(%ebp),%ebx 0x29487f10: mov %eax,%esi 0x29487f12: mov %edx,%edi 0x29487f14: sub %eax,%ecx 0x29487f16: sbb %edx,%ebx 0x29487f18: mov %ecx,%eax 0x29487f1a: mov %ebx,%edx 0x29487f1c: add %esi,%eax 0x29487f1e: adc %edi,%edx 0x29487f20: mov %ecx,0x98(%ebp) 0x29487f26: mov $0x11,%ecx 0x29487f2b: mov %ecx,0xa0(%ebp)
Quoted 8 lines
0x29487f31: mov %esi,0x90(%ebp) 0x29487f37: mov %edi,0x94(%ebp) 0x29487f3d: mov %ebx,0x9c(%ebp) 0x29487f43: cmp %edi,%edx 0x29487f45: jl 0x29487f7e 0x29487f4b: jne 0x29487f59 0x29487f51: cmp %esi,%eax 0x29487f53: jle 0x29487f7e
^^^ wrong!
Quoted 6 lines
0x29487f59: jmp 0x29487f5e 0x29487f5e: mov $0x8044b2db,%eax 0x29487f63: mov %eax,0x80(%ebp) 0x29487f69: mov $0xffffffff,%eax 0x29487f6e: mov %eax,0x84(%ebp) 0x29487f74: mov $0x112f1328,%eax
Quoted 8 lines
0x29487f79: jmp 0x83bd948 0x29487f7e: jmp 0x29487f83 0x29487f83: mov $0x8044b33a,%eax 0x29487f88: mov %eax,0x80(%ebp) 0x29487f8e: mov $0xffffffff,%eax 0x29487f93: mov %eax,0x84(%ebp) 0x29487f99: mov $0x112f1329,%eax 0x29487f9e: jmp 0x83bd948
OK so I was confused, the real problem was the use of signed conditions for the lower half, they need to be unsigned... But some of the `test r,r' special handling in my original patch is still useful as an optimization, so here is the updated patch that now at least lets the mentioned guest mount its /usr and finish booting:
Index: qemu/tcg/i386/tcg-target.c
@@ -359,25 +359,36 @@
break;
case TCG_COND_LT:
tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
+ if (const_args[2] && !args[2])
+ /* test r,r - carry can never be set */
+ break;
tcg_out_jxx(s, JCC_JNE, label_next);
- tcg_out_brcond(s, TCG_COND_LT, args[0], args[2], const_args[2], args[5]);
+ tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_LE:
tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
- tcg_out_brcond(s, TCG_COND_LE, args[0], args[2], const_args[2], args[5]);
+ tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GT:
tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
- tcg_out_brcond(s, TCG_COND_GT, args[0], args[2], const_args[2], args[5]);
+ tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GE:
+ if (const_args[2] && !args[2]) {
+ /* test r,r - carry can never be set */
+ tcg_out_brcond(s, TCG_COND_GE, args[1], args[3], const_args[3], args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
- tcg_out_brcond(s, TCG_COND_GE, args[0], args[2], const_args[2], args[5]);
+ tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_LTU:
+ if (const_args[2] && !args[2])
+ /* test r,r - carry can never be set */
+ break;
tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
@@ -393,6 +404,11 @@
tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
break;
case TCG_COND_GEU:
+ if (const_args[2] && !args[2]) {
+ /* test r,r - carry can never be set */
+ tcg_out_jxx(s, JCC_JMP, args[5]);
+ break;
+ }
tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
tcg_out_jxx(s, JCC_JNE, label_next);
tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]); (if you don't want the optimizations for sake of code clarity just remove
the `if (const_args[2] && !args[2])' clauses.)
Ok while writing this I was waiting for a Linux/amd64 guest to boot, which
still failed, and the reason seems to be that either floats or conversion of floats to integers are also broken for amd64 guests on i386 hosts, as can be seen by:
(gdb) p 1.
$1 = -nan(0x8000000000000)
The quickest way to reproduce this is probably a FreeBSD/amd64 livefs iso:
ftp://ftp.freebsd.org/pub/FreeBSD/ISO-IMAGES-amd64/7.0/7.0-RELEASE-amd64-livefs.iso start as: qemu-system-x86_64 -cdrom 7.0-RELEASE-amd64-livefs.iso
select fixit -> cdrom in the menu after the keymap configuration, then you can invoke gdb.
Thanx,
Juergen
PATCH 1/1 tcg: fix cvtsi2ssq & friends on 32 bit hosts (was: fixed one bug, found another :)
(I just noticed I didn't mark the previous post with `[PATCH]', its at
http://lists.gnu.org/archive/html/qemu-devel/2008-07/msg00137.html
if anyone missed it.)
Quoted 8 lines · On Sat, Jul 05, 2008 at 11:29:46PM +0200, I wrote:
[...]
Ok while writing this I was waiting for a Linux/amd64 guest to boot, which
still failed, and the reason seems to be that either floats or conversion of floats to integers are also broken for amd64 guests on i386 hosts, as can be seen by:(gdb) p 1.
$1 = -nan(0x8000000000000)
Ok and the reason for this was cvtsi2ssq and friends were not handled right:
Index: qemu/target-i386/translate.c
@@ -3330,8 +3330,12 @@
op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
sse_op2 = sse_op_table3[(s->dflag == 2) * 2 + ((b >> 8) - 2)];
- tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
- tcg_gen_helper_0_2(sse_op2, cpu_ptr0, cpu_tmp2_i32);
+ if (ot == OT_LONG) {+ tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
+ tcg_gen_helper_0_2(sse_op2, cpu_ptr0, cpu_tmp2_i32);
+ } else {
+ tcg_gen_helper_0_2(sse_op2, cpu_ptr0, cpu_T[0]);
+ }
break;
case 0x02c: /* cvttps2pi */
case 0x12c: /* cvttpd2pi */With this my Linux/amd64 guest (
sidux-2008-02-erebos-kde-full-i386-amd64-200806251839.iso
debian sid based, see
http://sidux.com
) now finally boots in 64 bit mode, only X doesn't start for some reason, manually invoking startx seems to work though. And more important, I get the same behaviour when using the older 20080302 snapshot, so that bug doesn't seem to have much to do with tcg at least. :)
Thanx,
Juergen