Qemu arm emulation
Hi Fabrice
I have had a look at some faults in the ARM port of Qemu. I have discovered a couple of bugs. To avoid difficulties with my employer I can't distribute my patch which contains the fixes, but they are fairly trivial so I am detailing the changes required here.
1. The RRX operand shift on data processing instructions is incorrectly decoded as a rotate right (ROR) of 0 bits.
RRX should have the effect of an extended rotate right of 1 bit where the carry flag is shifted into the top bit of the result. If the S bit is set, then the carry flag is set to the bottom bit of the source value.
2. Shifter carry out for immediates
When an immediate value is generated the shifter carry out is set to bit31 of the resulting immediate if the shift value is non zero. If the shift value is zero, then the shifter carry out has the value of the C flag.
Therefore, the following instructions should alter the carry flag when used with an immediate which has a non-zero shift.
ANDS BICS EORS MOVS MVNS ORRS TEQS and TSTS
(The remaining data processing instructions generate the C flag from the calculation performed by the instruction)
Test case is attached as a C file and as assembler file. The assembler function f will return if these bugs are fixed and loops forever otherwise. I can email you a statically linked binary if you don't have an ARM toolchain.
Apologies for not being able to just send a patch.
Regards
Charlie
#include <unistd.h>
extern unsigned f();
int main(unsigned int argc, char**argv)
{
f();
return 0;
}
.file "ps.s" .section .rodata .align 2 .global f
f:
cmp r0, r0 @ set carry flag
mov r0, r0, rrx
cmp r0, #0x80000000loop1: bne loop1
cmp r0, r0 @ set carry flag
tst r0, #0x04000000
loop2: bcs loop2
mov pc, lr
Quoted 7 lines · Charlie Baylis
Hi Fabrice
I have had a look at some faults in the ARM port of Qemu. I have discovered a couple of bugs. To avoid difficulties with my employer I can't distribute my patch which contains the fixes, but they are fairly trivial so I am detailing the changes required here.
Quoted 5 lines
1. The RRX operand shift on data processing instructions is incorrectly decoded as a rotate right (ROR) of 0 bits.
RRX should have the effect of an extended rotate right of 1 bit where the carry flag is shifted into the top bit of the result. If the S bit is set, then the carry flag is set to the bottom bit of the source value.
The attached patch fixes this.
I posted it a while back, but it never got into CVS and I never chased it. Paul
Index: target-arm/op.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/op.c,v
retrieving revision 1.3
op.c +13 -0
@@ -485,6 +502,11 @@ void OPPROTO op_rorl_T1_im(void)T1 = ((uint32_t )T1 >> shift) | (T1 << (32 - shift)); } +void OPPROTO op_rrxl_T1(void) +{ +T1 = ((uint32_t )T1 >>1 ) | ((uint32_t )env->CF <<31 ); +} +/* T1 based, set C flag */ void OPPROTO op_shll_T1_im_cc(void) {
@@ -512,6 +534,14 @@ void OPPROTO op_rorl_T1_im_cc(void)T1 = ((uint32_t )T1 >> shift) | (T1 << (32 - shift)); } +void OPPROTO op_rrxl_T1_cc(void) +{ +uint32_t c; + c =T1 &1 ; +T1 = ((uint32_t )T1 >>1 ) | ((uint32_t )env->CF <<31 ); + env->CF = c; +} +/* T2 based */ void OPPROTO op_shll_T2_im(void) {
Index: target-arm/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/translate.c,v retrieving revision 1.10
translate.c +5 -0
@@ -365,6 +484,11 @@ static void disas_arm_insn(DisasContext }else { gen_shift_T1_im[shiftop](shift); } + }else if (shiftop ==3 ) { +if (logic_cc) +gen_op_rrxl_T1_cc (); +else +gen_op_rrxl_T1 (); } }else { rs = (insn >>8 ) &0xf ;
Thank you for the patch ! From the spec, I think there are still errors for 0 shifts when shiftop != 3. Have you made a patch for that too ?
Fabrice.
Quoted 85 lines · Paul Brook
Quoted 7 lines · Charlie Baylis
Hi Fabrice
I have had a look at some faults in the ARM port of Qemu. I have discovered a couple of bugs. To avoid difficulties with my employer I can't distribute my patch which contains the fixes, but they are fairly trivial so I am detailing the changes required here.
Quoted 5 lines
1. The RRX operand shift on data processing instructions is incorrectly decoded as a rotate right (ROR) of 0 bits.
RRX should have the effect of an extended rotate right of 1 bit where the carry flag is shifted into the top bit of the result. If the S bit is set, then the carry flag is set to the bottom bit of the source value.The attached patch fixes this.
I posted it a while back, but it never got into CVS and I never chased it. Paul
------------------------------------------------------------------------
Index:
target-arm/op.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/op.c,v
retrieving revision 1.3
diff -u -p -r1.3op.cop.c +13
-0@@ -485,6 +502,11 @@ void OPPROTO op_rorl_T1_im(void)T1 = ((uint32_t )T1 >> shift) | (T1 << (32 - shift)); } +void OPPROTO op_rrxl_T1(void) +{ +T1 = ((uint32_t )T1 >>1 ) | ((uint32_t )env->CF <<31 ); +} +/* T1 based, set C flag */ void OPPROTO op_shll_T1_im_cc(void) {@@ -512,6 +534,14 @@ void OPPROTO op_rorl_T1_im_cc(void)T1 = ((uint32_t )T1 >> shift) | (T1 << (32 - shift)); } +void OPPROTO op_rrxl_T1_cc(void) +{ +uint32_t c; + c =T1 &1 ; +T1 = ((uint32_t )T1 >>1 ) | ((uint32_t )env->CF <<31 ); + env->CF = c; +} +/* T2 based */ void OPPROTO op_shll_T2_im(void) {Index:
target-arm/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/translate.c,v retrieving revision 1.10
diff -u -p -r1.10translate.ctranslate.c +5
-1@@ -365,6 +484,11 @@ static void disas_arm_insn(DisasContext }else { gen_shift_T1_im[shiftop](shift); } + }else if (shiftop ==3 ) { +if (logic_cc) +gen_op_rrxl_T1_cc (); +else +gen_op_rrxl_T1 (); } }else { rs = (insn >>8 ) &0xf ; ------------------------------------------------------------------------_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel
Quoted 2 lines · Fabrice Bellard
Thank you for the patch ! From the spec, I think there are still errors for 0 shifts when shiftop != 3. Have you made a patch for that too ?
No, and I probably won't have chance to look at it in the next week or so.
Paul
Hi!
Quoted 11 lines · Charlie Baylis
2. Shifter carry out for immediates
When an immediate value is generated the shifter carry out is set to bit31 of the resulting immediate if the shift value is non zero. If the shift value is zero, then the shifter carry out has the value of the C flag.Therefore, the following instructions should alter the carry flag when used with an immediate which has a non-zero shift.
ANDS BICS EORS MOVS MVNS ORRS TEQS and TSTS
(The remaining data processing instructions generate the C flag from the calculation performed by the instruction)
Here's a patch that fixes the testcase. I made it for 0.6.1, but it still applies and works for CVS.
CU
Uli
op.c +6 -0
@@ -98,6 +98,12 @@T1 =PARAM1 ; } +void OPPROTO op_movl_T1_im_cc(void) +{ +T1 =PARAM1 ; + env->CF =PARAM1 >>31 ; +} + void OPPROTO op_movl_T2_im(void) {T2 =PARAM1 ;
translate.c +5 -1
@@ -350,7 +350,11 @@ shift = ((insn >>8 ) &0xf ) *2 ;if (shift) val = (val >> shift) | (val << (32 - shift)); -gen_op_movl_T1_im (val); +if (logic_cc && shift) +gen_op_movl_T1_im_cc (val); +else +gen_op_movl_T1_im (val); +/* XXX: is CF modified ? */ }else {/* register */
Quoted 18 lines · Ulrich Hecht
Hi!
Quoted 11 lines · Charlie Baylis
2. Shifter carry out for immediates
When an immediate value is generated the shifter carry out is set to bit31 of the resulting immediate if the shift value is non zero. If the shift value is zero, then the shifter carry out has the value of the C flag.Therefore, the following instructions should alter the carry flag when used with an immediate which has a non-zero shift.
ANDS BICS EORS MOVS MVNS ORRS TEQS and TSTS
(The remaining data processing instructions generate the C flag from the calculation performed by the instruction)Here's a patch that fixes the testcase. I made it for 0.6.1, but it still applies and works for CVS.
op.c line 105
Quoted 5 lines
op.c +5
-0+void OPPROTO op_movl_T1_im_cc(void) +{ +T1 =PARAM1 ; + env->CF =PARAM1 >>31 ; +}
This should be "((uint32_t) PARAM1) >> 31".
The comments say CF is 0 or 1. PARAM1 is signed, so your code will result in 0 or -1 on 32-bit hosts.
Quoted 1 line
/* XXX: is CF modified ? */
Probably want to remove this comment now.
Paul
Hi!
Quoted 11 lines · Paul Brook
Review
Quoted 5 lines · Ulrich Hecht
(quoted diff) +5
-0+void OPPROTO op_movl_T1_im_cc(void) +{ + T1 = PARAM1; + env->CF = PARAM1 >> 31; +}This should be "((
uint32_t) PARAM1) >> 31".
The comments say CF is 0 or 1. PARAM1 is signed, so your code will result in 0 or -1 on 32-bit hosts.
Quoted 3 lines
Quoted 1 line
/* XXX: is CF modified ? */Probably want to remove this comment now.
OK.
CU
Uli
Index: target-arm/op.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/op.c,v
retrieving revision 1.7
op.c +6 -0
@@ -105,6 +105,12 @@T1 =PARAM1 ; } +void OPPROTO op_movl_T1_im_cc(void) +{ +T1 =PARAM1 ; + env->CF = ((uint32_t )PARAM1 ) >>31 ; +} + void OPPROTO op_movl_T2_im(void) {T2 =PARAM1 ;
Index: target-arm/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-arm/translate.c,v retrieving revision 1.15
translate.c +4 -2
@@ -535,8 +535,10 @@ shift = ((insn >>8 ) &0xf ) *2 ;if (shift) val = (val >> shift) | (val << (32 - shift)); -gen_op_movl_T1_im (val); -/* XXX: is CF modified ? */ +if (logic_cc && shift) +gen_op_movl_T1_im_cc (val); +else +gen_op_movl_T1_im (val); }else {/* register */ rm = (insn) &0xf ;