Skip to contentSkip to conversation

Problem with 64-bit registers on i.MX53

DISCUSSION Michael Olbrich 4 participants 10 messages Oct 8, 2012 – Oct 9, 2012 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

Hi,

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

If I start the program twice in parallel, then on aborts immediately with the assert().
When looking at it with the debugger, it seems as if the calculation "b = (b + y) % (1 << 30)" produces an incorrect value.

A single instance of the program runs longer, but hits the assert() eventually. So running multiple processes that use 64-bit registers triggers the problem really fast. This is why I think the problem has something to do with context switches.
Not using 64-bit registers (-msoft-float) makes the problem go away. So it's definitely related to 64-bit registers.

If I disable all MX31/MX35 boards (and therefore all ARMv6 support), then it works as expected.

Any ideas what goes wrong here?

Regards,
Michael Olbrich

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#1 lore raw
Quoted 30 lines · Michael Olbrich

Hi,

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}
Quoted 3 lines

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

Hmm.

Can you send me the output of 'grep ^Features /proc/cpuinfo' please?

Thanks.

#2 lore raw
Quoted 35 lines · Russell King - ARM Linux
Quoted 30 lines · Michael Olbrich

Hi,

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}
Quoted 3 lines

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

Quoted 3 lines

Hmm.

Can you send me the output of 'grep ^Features /proc/cpuinfo' please?

You may also like to try the patch below... it will probably fix your problem.

arch/arm/include/asm/vfpmacros.h +2 -2
index a7aadbd..6a6f1e4 100644
@@ -28,7 +28,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	ldceq	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
+	ldceql	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
@@ -52,7 +52,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	stceq	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
+	stceql	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
#3 lore raw
Quoted 23 lines · Russell King - ARM Linux
Quoted 22 lines · Russell King - ARM Linux
Quoted 21 lines · Michael Olbrich

Hi,

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
Quoted 8 lines
Quoted 8 lines
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

Just for my curiosity, can you let me know what compiler version you're using and the disassembly? I'm actually a little surprised to see NEON code being generated here, though the patch below fixes what definitely looks like a context switch bug for combined v6+v7 kernels...

Cheers
---Dave

Quoted 36 lines
Quoted 4 lines

Hmm.

Can you send me the output of 'grep ^Features /proc/cpuinfo' please?

You may also like to try the patch below... it will probably fix your problem.

arch/arm/include/asm/vfpmacros.h +2 -2
index a7aadbd..6a6f1e4 100644
@@ -28,7 +28,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	ldceq	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
+	ldceql	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
@@ -52,7 +52,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	stceq	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
+	stceql	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#4 lore raw
Quoted 4 lines · Dave Martin

Just for my curiosity, can you let me know what compiler version you're using and the disassembly? I'm actually a little surprised to see NEON code being generated here, though the patch below fixes what definitely looks like a context switch bug for combined v6+v7 kernels...

Well, one such compiler is gcc 4.6.3 in Ubuntu Precise LTS.

#5 lore raw
Quoted 7 lines · Russell King - ARM Linux
Quoted 4 lines · Dave Martin

Just for my curiosity, can you let me know what compiler version you're using and the disassembly? I'm actually a little surprised to see NEON code being generated here, though the patch below fixes what definitely looks like a context switch bug for combined v6+v7 kernels...

Well, one such compiler is gcc 4.6.3 in Ubuntu Precise LTS.

Hmmm, I really need to upgrade...

Cheers
---Dave

author Tested-by
#7 lore raw
In reply to Russell King - ARM Linux
Quoted 33 lines · Russell King - ARM Linux
Quoted 32 lines · Russell King - ARM Linux
Quoted 30 lines · Michael Olbrich

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

Quoted 3 lines
Quoted 3 lines

Hmm.

Can you send me the output of 'grep ^Features /proc/cpuinfo' please?

Features : swp half thumb fastmult vfp edsp neon vfpv3 tls

Quoted 2 lines

You may also like to try the patch below... it will probably fix your problem.

This does indeed fix my problem. Is this a real fix or just a test to narrow down the issue? I don't really understand what it does. If it is a real fix,

Regards,
Michael

Quoted 22 lines
arch/arm/include/asm/vfpmacros.h +2 -2
index a7aadbd..6a6f1e4 100644
@@ -28,7 +28,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	ldceq	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
+	ldceql	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
@@ -52,7 +52,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	stceq	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
+	stceql	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw
In reply to Dave Martin
Quoted 34 lines · Dave Martin
Quoted 20 lines · Russell King - ARM Linux
Quoted 19 lines · Russell King - ARM Linux
Quoted 18 lines · Michael Olbrich

I have a problem that looks like that 64-bit registers (I think) are not saved/restored correctly on a context switch. I've reduced it to the following test case:

- Latest Linux mainline kernel (v3.6-8559-ge9eca4d) v3.5 is also affected
- imx_v6_v7_defconfig
- arch/arm/boot/dts/imx53-evk.dts

The following test program is compiled with "-mcpu=cortex-a8 -mfpu=neon -O2". ------------------------>8-------------------------------- #include <inttypes.h>
#include <assert.h>

volatile int x = 2;
volatile int64_t y = 2;

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
int main() {
	volatile int a = 0;
	volatile int64_t b = 0;
	while (1) {
Quoted 8 lines
Quoted 8 lines
		a = (a + x) % (1 << 30);
		b = (b + y) % (1 << 30);
		assert(a == b);
	}
}

------------------------>8-------------------------------- The ".. (b + y) .." should result in "vadd.i64 d19, d18, d16" or something like that.

Quoted 3 lines

Just for my curiosity, can you let me know what compiler version you're using and the disassembly? I'm actually a little surprised to see NEON code being generated here,

Im using oselas.toolchain which includes a gcc-linaro-4.6-2011.11. It generates quite a bit of NEON code actually. I originally tracked down the issue to a commit in libxcb: "xcb_in: Use 64-bit sequence numbers internally everywhere.". The compiler generated NEON code to calculate sequence numbers...

Regards,
Michael

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw
In reply to Russell King - ARM Linux

Hello,

Quoted 19 lines · Russell King - ARM Linux
Quoted 1 line · Russell King - ARM Linux

On Mon, Oct 08, 2012 at 06:08:41PM +0200, Michael Olbrich wrote:

You may also like to try the patch below... it will probably fix your problem.

arch/arm/include/asm/vfpmacros.h +1 -1
index a7aadbd..6a6f1e4 100644
@@ -28,7 +28,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	ldceq	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
+	ldceql	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0

arch/arm/include/asm/vfpmacros.h line 55

Quoted 5 lines
arch/arm/include/asm/vfpmacros.h +1 -1
@@ -52,7 +52,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	stceq	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
+	stceql	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}

According to the ARMARM for v7-A and v7-R (ARM DDI 0406B errata 2010 Q2) the syntax is "STC{L}<c> ...", with a note "The pre-UAL syntax STC<c>L is equivalent to STCL<c>.". Maybe the UAL-syntax should better be used?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#9 lore raw
Quoted 22 lines · Uwe Kleine-K?nig

Hello,

Quoted 19 lines · Russell King - ARM Linux
Quoted 1 line · Russell King - ARM Linux

On Mon, Oct 08, 2012 at 06:08:41PM +0200, Michael Olbrich wrote:

You may also like to try the patch below... it will probably fix your problem.

arch/arm/include/asm/vfpmacros.h +1 -1
index a7aadbd..6a6f1e4 100644
@@ -28,7 +28,7 @@
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	ldceq	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
+	ldceql	p11, cr0, [\base],#32*4		    @ FLDMIAD \base!, {d16-d31}
 	addne	\base, \base, #32*4		    @ step over unused register space
 #else
 	VFPFMRX	\tmp, MVFR0			    @ Media and VFP Feature Register 0
@@ -52,7 +52,7 @@
Quoted 8 lines

arch/arm/include/asm/vfpmacros.h line 55

Quoted 5 lines
arch/arm/include/asm/vfpmacros.h +1 -1
 	ldr	\tmp, =elf_hwcap		    @ may not have MVFR regs
 	ldr	\tmp, [\tmp, #0]
 	tst	\tmp, #HWCAP_VFPv3D16
-	stceq	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}
+	stceql	p11, cr0, [\base],#32*4		    @ FSTMIAD \base!, {d16-d31}

According to the ARMARM for v7-A and v7-R (ARM DDI 0406B errata 2010 Q2) the syntax is "STC{L}<c> ...", with a note "The pre-UAL syntax STC<c>L is equivalent to STCL<c>.". Maybe the UAL-syntax should better be used?

The older stc<c>l type of syntax is used all over the place. Code which might need to be built by tools which pre-date unified syntax needs to use the old syntax, so it is in common usage in the kernel in general.

This code presumably only gets built by new-enough tools for the unified syntax to be usable, but support for the old syntax isn't going to disappear from the tools any time soon, AFAIK.

The bug here was that the presence or absence of the "L" suffix is used to encode bit 4 of the starting d-register number for these instructions. The comment says d16-d31, but the instructions as written are actually saving and restoring d0-d15...which is not so helpful since we already handled those registers in the neighbouring code.

We could avoid this kind of bug by writing those VFP instructions using the unified syntax native mnemonics (vstmia, vldmia -- since d16- d31 never existed while the old fldmiad/fstmiad mnemonics were in use, and the assembler doesn't accept them), but it is tricky to change the assembler's notion of target CPU and FPU on-the-fly inside a header or macro without messing things up.

Cheers
---Dave

Quoted 12 lines

Best regards
Uwe

Signature · 7 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel