Skip to contentSkip to conversation

mn10300: Truncate base in do_div()

PATCH New Geert Uytterhoeven 1 participant 1 message Aug 22, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mn10300: Truncate base in do_div()

Explicitly truncate the second operand of do_div() to 32 bits to guard against bogus code calling it with a 64-bit divisor.

Based on commit ea077b1b96e073eac5c3c5590529e964767fc5f7 ("m68k: Truncate base in do_div()")

While it doesn't really hurt on little-endian mn10300, unlike on m68k, mn10300 was the only remaining architecture not doing this.

---
Compile-tested only.

 arch/mn10300/include/asm/div64.h |   13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/mn10300/include/asm/div64.h +7 -6
index 503efab..ff69f34 100644
@@ -36,12 +36,13 @@ extern void ____unhandled_size_in_do_div___(void);
 #define do_div(n, base)							\
 ({									\
 	unsigned __rem = 0;						\
+	unsigned __base = (base);					\
 	if (sizeof(n) <= 4) {						\
 		asm("mov	%1,mdr	\n"				\
 		    "divu	%2,%0	\n"				\
 		    "mov	mdr,%1	\n"				\
 		    : "+r"(n), "=d"(__rem)				\
-		    : "r"(base), "1"(__rem)				\
+		    : "r"(__base), "1"(__rem)				\
 		    : CLOBBER_MDR_CC					\
 		    );							\
 	} else if (sizeof(n) <= 8) {					\
@@ -52,14 +53,14 @@ extern void ____unhandled_size_in_do_div___(void);
 		__quot.l = n;						\
 		asm("mov	%0,mdr	\n"	/* MDR = 0 */		\
 		    "divu	%3,%1	\n"				\
-		    /* __quot.MSL = __div.MSL / base, */		\
-		    /* MDR = MDR:__div.MSL % base */			\
+		    /* __quot.MSL = __div.MSL / __base, */		\
+		    /* MDR = MDR:__div.MSL % __base */			\
 		    "divu	%3,%2	\n"				\
-		    /* __quot.LSL = MDR:__div.LSL / base, */		\
-		    /* MDR = MDR:__div.LSL % base */			\
+		    /* __quot.LSL = MDR:__div.LSL / __base, */		\
+		    /* MDR = MDR:__div.LSL % __base */			\
 		    "mov	mdr,%0	\n"				\
 		    : "=d"(__rem), "=r"(__quot.w[1]), "=r"(__quot.w[0])	\
-		    : "r"(base), "0"(__rem), "1"(__quot.w[1]),		\
+		    : "r"(__base), "0"(__rem), "1"(__quot.w[1]),	\
 		      "2"(__quot.w[0])					\
 		    : CLOBBER_MDR_CC					\
 		    );							\
Signature · 1 line
1.7.9.5