Skip to contentSkip to conversation

ARM : missing corrupted reg in __do_div_asm

PATCH Superseded Chen Gang 2 participants 5 messages Jun 2, 2015 – Jun 28, 2015 1 message not in the archive Arm (32-bit)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): /home/gachen/Re: [PATCH 1_1] ARM:r0 is also corrupted after calling __do_div64..eml

Chen Gang

author
#1 lore raw

PATCH 1/1 ARM : missing corrupted reg in __do_div_asm

__xl (r0 in little endian system, or R1 in big endian system) is corrupted after calling __do_div64 and compiler is not informed about this in macro __do_div_asm. If n is used again afterwards, __xl won't be reloaded and n will contain incorrect value.

---
 arch/arm/include/asm/div64.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm/include/asm/div64.h +6 -4
index 662c7bd..fa9489e 100644
@@ -34,12 +34,14 @@
 	register unsigned long long __n   asm("r0") = n;	\
 	register unsigned long long __res asm("r2");		\
 	register unsigned int __rem       asm(__xh);		\
+	register unsigned int __clobber   asm(__xl);		\
 	asm(	__asmeq("%0", __xh)				\
-		__asmeq("%1", "r2")				\
-		__asmeq("%2", "r0")				\
-		__asmeq("%3", "r4")				\
+		__asmeq("%1", __xl)				\
+		__asmeq("%2", "r2")				\
+		__asmeq("%3", "r0")				\
+		__asmeq("%4", "r4")				\
 		"bl	__do_div64"				\
-		: "=r" (__rem), "=r" (__res)			\
+		: "=r" (__rem), "=r" (__clobber), "=r" (__res)  \
 		: "r" (__n), "r" (__base)			\
 		: "ip", "lr", "cc");				\
 	n = __res;						\
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH 1/1 ARM : missing corrupted reg in __do_div_asm

Hi,

I tried to make a test program to illustrate the patch.

Without the patch, the log shows
"[250970.715392] remains1 is 2, remains2 is 0"
And the remains2 is wrong

With the patch, the log is
"[250977.724181] remains1 is 2, remains2 is 4".
now both remains1 and remains2 are correct.

I compared the output assembly and it showed in the faulty version, r0 ( I am using little endian system) is not reload after the first do_div and before the second do_div call in function mydiv64.

Br, Chen Gang

#include <asm/div64.h>
#include <linux/init.h>
#include <linux/printk.h>

typedef unsigned long long ull;

void mydiv64(ull tt, unsigned base1, unsigned base2,
unsigned *rems1, unsigned *rems2)

{
     ull t = tt;
     *rems1 = do_div(t, base1);
     *rems2 = do_div(tt, base2);
     return ;
}

static int __init div_test(void)

{
     ull t;
     unsigned int remains1;
     unsigned int remains2;
     unsigned base1, base2;

     t = 100;
     base1 = 7;
     base2 = 8;

     mydiv64(t, base1, base2, &remains1, &remains2);
     printk(KERN_ERR"remains1 is %d, remains2 is %d\n",
             remains1, remains2);

     return -1;
}

static void __exit div_test_exit(void)
{
}

module_init(div_test);
module_exit(div_test_exit);

Quoted 31 lines · Chen Gang

__xl (r0 in little endian system, or R1 in big endian system) is corrupted after calling __do_div64 and compiler is not informed about this in macro __do_div_asm. If n is used again afterwards, __xl won't be reloaded and n will contain incorrect value.

---
  arch/arm/include/asm/div64.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm/include/asm/div64.h +6 -4
index 662c7bd..fa9489e 100644
@@ -34,12 +34,14 @@
  	register unsigned long long __n   asm("r0") = n;	\
  	register unsigned long long __res asm("r2");		\
  	register unsigned int __rem       asm(__xh);		\
+	register unsigned int __clobber   asm(__xl);		\
  	asm(	__asmeq("%0", __xh)				\
-		__asmeq("%1", "r2")				\
-		__asmeq("%2", "r0")				\
-		__asmeq("%3", "r4")				\
+		__asmeq("%1", __xl)				\
+		__asmeq("%2", "r2")				\
+		__asmeq("%3", "r0")				\
+		__asmeq("%4", "r4")				\
  		"bl	__do_div64"				\
-		: "=r" (__rem), "=r" (__res)			\
+		: "=r" (__rem), "=r" (__clobber), "=r" (__res)  \
  		: "r" (__n), "r" (__base)			\
  		: "ip", "lr", "cc");				\
  	n = __res;						\
#3 lore raw

PATCH 1/1 ARM : missing corrupted reg in __do_div_asm

In reply to Chen Gang
Quoted 5 lines · Chen Gang

__xl (r0 in little endian system, or R1 in big endian system) is corrupted after calling __do_div64 and compiler is not informed about this in macro __do_div_asm. If n is used again afterwards, __xl won't be reloaded and n will contain incorrect value.
---

This commit message needs improving. Please either upper case both r0 and r1 in the first line, or lower case them both. Secondly, the Linux kernel has a requirement that all contributions are signed-off. Please read Documentation/SubmittingPatches for information, particularly section 11.

Other than that, the patch looks fine, thanks.

Quoted 30 lines
 arch/arm/include/asm/div64.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm/include/asm/div64.h +6 -4
index 662c7bd..fa9489e 100644
@@ -34,12 +34,14 @@
 	register unsigned long long __n   asm("r0") = n;	\
 	register unsigned long long __res asm("r2");		\
 	register unsigned int __rem       asm(__xh);		\
+	register unsigned int __clobber   asm(__xl);		\
 	asm(	__asmeq("%0", __xh)				\
-		__asmeq("%1", "r2")				\
-		__asmeq("%2", "r0")				\
-		__asmeq("%3", "r4")				\
+		__asmeq("%1", __xl)				\
+		__asmeq("%2", "r2")				\
+		__asmeq("%3", "r0")				\
+		__asmeq("%4", "r4")				\
 		"bl	__do_div64"				\
-		: "=r" (__rem), "=r" (__res)			\
+		: "=r" (__rem), "=r" (__clobber), "=r" (__res)  \
 		: "r" (__n), "r" (__base)			\
 		: "ip", "lr", "cc");				\
 	n = __res;						\
Signature · 1 line
1.9.1
Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up
according to speedtest.net.
author
#4 lore raw

PATCH v2 1/1 ARM : missing corrupted reg in __do_div_asm

__xl (R0 in little endian system, or R1 in big endian system) is corrupted after calling __do_div64 and compiler is not informed about this in macro __do_div_asm. If n is used again afterwards, __xl won't be reloaded and n will contain incorrect value.

---
 arch/arm/include/asm/div64.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm/include/asm/div64.h +6 -4
index 662c7bd..fa9489e 100644
@@ -34,12 +34,14 @@
 	register unsigned long long __n   asm("r0") = n;	\
 	register unsigned long long __res asm("r2");		\
 	register unsigned int __rem       asm(__xh);		\
+	register unsigned int __clobber   asm(__xl);		\
 	asm(	__asmeq("%0", __xh)				\
-		__asmeq("%1", "r2")				\
-		__asmeq("%2", "r0")				\
-		__asmeq("%3", "r4")				\
+		__asmeq("%1", __xl)				\
+		__asmeq("%2", "r2")				\
+		__asmeq("%3", "r0")				\
+		__asmeq("%4", "r4")				\
 		"bl	__do_div64"				\
-		: "=r" (__rem), "=r" (__res)			\
+		: "=r" (__rem), "=r" (__clobber), "=r" (__res)  \
 		: "r" (__n), "r" (__base)			\
 		: "ip", "lr", "cc");				\
 	n = __res;						\
Signature · 1 line
2.4.2
author
#5 lore raw

PATCH 1/1 ARM : missing corrupted reg in __do_div_asm

In reply to Russell King - ARM Linux

Hi, Russell

I have uploaded V2 of the patch based on your comments on Jun-19. Hope you can review that again.

Br, Chen Gang

Quoted 44 lines · Russell King - ARM Linux
Quoted 5 lines · Chen Gang

__xl (r0 in little endian system, or R1 in big endian system) is corrupted after calling __do_div64 and compiler is not informed about this in macro __do_div_asm. If n is used again afterwards, __xl won't be reloaded and n will contain incorrect value.
---

This commit message needs improving. Please either upper case both r0 and r1 in the first line, or lower case them both. Secondly, the Linux kernel has a requirement that all contributions are signed-off. Please read Documentation/SubmittingPatches for information, particularly section 11.

Other than that, the patch looks fine, thanks.

Quoted 30 lines
  arch/arm/include/asm/div64.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm/include/asm/div64.h +6 -4
index 662c7bd..fa9489e 100644
@@ -34,12 +34,14 @@
  	register unsigned long long __n   asm("r0") = n;	\
  	register unsigned long long __res asm("r2");		\
  	register unsigned int __rem       asm(__xh);		\
+	register unsigned int __clobber   asm(__xl);		\
  	asm(	__asmeq("%0", __xh)				\
-		__asmeq("%1", "r2")				\
-		__asmeq("%2", "r0")				\
-		__asmeq("%3", "r4")				\
+		__asmeq("%1", __xl)				\
+		__asmeq("%2", "r2")				\
+		__asmeq("%3", "r0")				\
+		__asmeq("%4", "r4")				\
  		"bl	__do_div64"				\
-		: "=r" (__rem), "=r" (__res)			\
+		: "=r" (__rem), "=r" (__clobber), "=r" (__res)  \
  		: "r" (__n), "r" (__base)			\
  		: "ip", "lr", "cc");				\
  	n = __res;						\
Signature · 1 line
1.9.1