Skip to contentSkip to conversation

parisc/unaligned: Rewrite 64-bit inline assembly of emulate_ldd()

PATCH In review Guenter Roeck 2 participants 5 messages Feb 16, 2024 – Feb 26, 2024 PA-RISC architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 parisc/unaligned: Rewrite 64-bit inline assembly of emulate_ldd()

Convert to use real temp variables instead of clobbering processor registers. This aligns the 64-bit inline assembly code with the 32-bit assembly code which was rewritten with commit 427c1073a2a1 ("parisc/unaligned: Rewrite 32-bit inline assembly of emulate_ldd()").

While at it, fix comment in 32-bit rewrite code. Temporary variables are now used for both 32-bit and 64-bit code, so move their declarations to the function header.

No functional change intended.

---
Implemented while analyzing a bug. I am not really sure of it is worth the effort, but I figured that I might as well submit it.

 arch/parisc/kernel/unaligned.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
arch/parisc/kernel/unaligned.c +13 -16
index c520e551a165..622c7b549fb8 100644
@@ -169,7 +169,8 @@ static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
 static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 {
 	unsigned long saddr = regs->ior;
-	__u64 val = 0;
+	unsigned long shift;
+	__u64 val = 0, temp1;
 	ASM_EXCEPTIONTABLE_VAR(ret);
 
 	DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n", 
@@ -180,25 +181,22 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
 #ifdef CONFIG_64BIT
 	__asm__ __volatile__  (
-"	depd,z	%3,60,3,%%r19\n"		/* r19=(ofs&7)*8 */
-"	mtsp	%4, %%sr1\n"
-"	depd	%%r0,63,3,%3\n"
-"1:	ldd	0(%%sr1,%3),%0\n"
-"2:	ldd	8(%%sr1,%3),%%r20\n"
-"	subi	64,%%r19,%%r19\n"
-"	mtsar	%%r19\n"
-"	shrpd	%0,%%r20,%%sar,%0\n"
+"	depd,z	%4,60,3,%2\n"		/* shift=(ofs&7)*8 */
+"	mtsp	%5, %%sr1\n"
+"	depd	%%r0,63,3,%4\n"
+"1:	ldd	0(%%sr1,%4),%0\n"
+"2:	ldd	8(%%sr1,%4),%3\n"
+"	subi	64,%2,%2\n"
+"	mtsar	%2\n"
+"	shrpd	%0,%3,%%sar,%0\n"
 "3:	\n"
 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b, "%1")
 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b, "%1")
-	: "=r" (val), "+r" (ret)
-	: "0" (val), "r" (saddr), "r" (regs->isr)
-	: "r19", "r20" );
+	: "+r" (val), "+r" (ret), "=&r" (shift), "=&r" (temp1)
+	: "r" (saddr), "r" (regs->isr) );
 #else
-    {
-	unsigned long shift, temp1;
 	__asm__ __volatile__  (
-"	zdep	%2,29,2,%3\n"		/* r19=(ofs&3)*8 */
+"	zdep	%2,29,2,%3\n"		/* shift=(ofs&3)*8 */
 "	mtsp	%5, %%sr1\n"
 "	dep	%%r0,31,2,%2\n"
 "1:	ldw	0(%%sr1,%2),%0\n"
@@ -214,7 +212,6 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 4b, "%1")
 	: "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
 	: "r" (regs->isr) );
-    }
 #endif
 
 	DPRINTF("val = 0x%llx\n", val);
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 2 lines · Guenter Roeck

Convert to use real temp variables instead of clobbering processor registers.

Thanks for doing this.
It was on my todo list since quite some time :-)

Quoted 23 lines

This aligns the 64-bit inline assembly code with the 32-bit assembly code which was rewritten with commit 427c1073a2a1 ("parisc/unaligned: Rewrite 32-bit inline assembly of emulate_ldd()").

While at it, fix comment in 32-bit rewrite code. Temporary variables are now used for both 32-bit and 64-bit code, so move their declarations to the function header.

No functional change intended.

---
Implemented while analyzing a bug. I am not really sure of it is worth the effort, but I figured that I might as well submit it.

  arch/parisc/kernel/unaligned.c | 29 +++++++++++++----------------
  1 file changed, 13 insertions(+), 16 deletions(-)
arch/parisc/kernel/unaligned.c +0 -0
index c520e551a165..622c7b549fb8 100644

arch/parisc/kernel/unaligned.c static int emulate_ldw(struct pt_regs *regs, int toreg, int flop) · line 173

Quoted 6 lines
arch/parisc/kernel/unaligned.c +2 -1
@@ -169,7 +169,8 @@ static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
  static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
  {
  	unsigned long saddr = regs->ior;
-	__u64 val = 0;
+	unsigned long shift;
+	__u64 val = 0, temp1;

temp1 is ok to be "long".

Quoted 21 lines
arch/parisc/kernel/unaligned.c +6 -8
  	ASM_EXCEPTIONTABLE_VAR(ret);
 
  	DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
@@ -180,25 +181,22 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
  #ifdef CONFIG_64BIT
  	__asm__ __volatile__  (
-"	depd,z	%3,60,3,%%r19\n"		/* r19=(ofs&7)*8 */
-"	mtsp	%4, %%sr1\n"
-"	depd	%%r0,63,3,%3\n"
-"1:	ldd	0(%%sr1,%3),%0\n"
-"2:	ldd	8(%%sr1,%3),%%r20\n"
-"	subi	64,%%r19,%%r19\n"
-"	mtsar	%%r19\n"
-"	shrpd	%0,%%r20,%%sar,%0\n"
+"	depd,z	%4,60,3,%2\n"		/* shift=(ofs&7)*8 */
+"	mtsp	%5, %%sr1\n"
+"	depd	%%r0,63,3,%4\n"
+"1:	ldd	0(%%sr1,%4),%0\n"
+"2:	ldd	8(%%sr1,%4),%3\n"
+"	subi	64,%2,%2\n"

arch/parisc/kernel/unaligned.c static int emulate_ldd(struct pt_regs *regs, int toreg, int flop) · line 196

Quoted 10 lines
arch/parisc/kernel/unaligned.c +4 -3
+"	mtsar	%2\n"
+"	shrpd	%0,%3,%%sar,%0\n"
  "3:	\n"
  	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b, "%1")
  	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b, "%1")
-	: "=r" (val), "+r" (ret)
-	: "0" (val), "r" (saddr), "r" (regs->isr)
-	: "r19", "r20" );
+	: "+r" (val), "+r" (ret), "=&r" (shift), "=&r" (temp1)
+	: "r" (saddr), "r" (regs->isr) );

addr is actually being modified.
That's why I moved it into the output registers and shuffled shift and temp1 one backwards, so that the registers are now in the same ordering as on the 32-bit path.

I've pushed the modified patch here:
https://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux.git/commit/?h=for-next&id=a6ea53ce77e9dd6e388d673bdd4d80741f97b914

Please double-check!

Thanks!
Helge

Quoted 17 lines
arch/parisc/kernel/unaligned.c +1 -4
  #else
-    {
-	unsigned long shift, temp1;
  	__asm__ __volatile__  (
-"	zdep	%2,29,2,%3\n"		/* r19=(ofs&3)*8 */
+"	zdep	%2,29,2,%3\n"		/* shift=(ofs&3)*8 */
  "	mtsp	%5, %%sr1\n"
  "	dep	%%r0,31,2,%2\n"
  "1:	ldw	0(%%sr1,%2),%0\n"
@@ -214,7 +212,6 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
  	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 4b, "%1")
  	: "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
  	: "r" (regs->isr) );
-    }
  #endif
 
  	DPRINTF("val = 0x%llx\n", val);
#2 lore raw
Quoted 76 lines · Helge Deller
Quoted 2 lines · Guenter Roeck

Convert to use real temp variables instead of clobbering processor registers.

Thanks for doing this.
It was on my todo list since quite some time :-)

Quoted 23 lines

This aligns the 64-bit inline assembly code with the 32-bit assembly code which was rewritten with commit 427c1073a2a1 ("parisc/unaligned: Rewrite 32-bit inline assembly of emulate_ldd()").

While at it, fix comment in 32-bit rewrite code. Temporary variables are now used for both 32-bit and 64-bit code, so move their declarations to the function header.

No functional change intended.

---
Implemented while analyzing a bug. I am not really sure of it is worth the effort, but I figured that I might as well submit it.

  arch/parisc/kernel/unaligned.c | 29 +++++++++++++----------------

  1 file changed, 13 insertions(+), 16 deletions(-)
arch/parisc/kernel/unaligned.c +0 -0
index c520e551a165..622c7b549fb8 100644

arch/parisc/kernel/unaligned.c static int emulate_ldw(struct pt_regs *regs, int toreg, int flop) · line 173

Quoted 6 lines
arch/parisc/kernel/unaligned.c +2 -1
@@ -169,7 +169,8 @@ static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
   static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
   {
       unsigned long saddr = regs->ior;
-    __u64 val = 0;
+    unsigned long shift;
+    __u64 val = 0, temp1;

temp1 is ok to be "long".

Quoted 23 lines
arch/parisc/kernel/unaligned.c +0 -0
       ASM_EXCEPTIONTABLE_VAR(ret);
 
       DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
@@ -180,25 +181,22 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
   #ifdef CONFIG_64BIT

      __asm__ __volatile__  (
-"    depd,z    %3,60,3,%%r19\n"        /* r19=(ofs&7)*8 */ -"    mtsp    %4, %%sr1\n"
-"    depd    %%r0,63,3,%3\n"
-"1:    ldd    0(%%sr1,%3),%0\n"
-"2:    ldd    8(%%sr1,%3),%%r20\n"
-"    subi    64,%%r19,%%r19\n"
-"    mtsar    %%r19\n"
-"    shrpd    %0,%%r20,%%sar,%0\n"
+"    depd,z    %4,60,3,%2\n"        /* shift=(ofs&7)*8 */ +"    mtsp    %5, %%sr1\n"
+"    depd    %%r0,63,3,%4\n"
+"1:    ldd    0(%%sr1,%4),%0\n"
+"2:    ldd    8(%%sr1,%4),%3\n"
+"    subi    64,%2,%2\n"
+"    mtsar    %2\n"
+"    shrpd    %0,%3,%%sar,%0\n"

Quoted 8 lines

  "3:    \n"
      ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b, "%1") ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b, "%1") -    : "=r" (val), "+r" (ret)
-    : "0" (val), "r" (saddr), "r" (regs->isr)
-    : "r19", "r20" );
+    : "+r" (val), "+r" (ret), "=&r" (shift), "=&r" (temp1) +    : "r" (saddr), "r" (regs->isr) );

addr is actually being modified.
That's why I moved it into the output registers and shuffled shift and temp1 one backwards, so that the registers are now in the same ordering as on the 32-bit path.

Quoted 5 lines

I've pushed the modified patch here:
https://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux.git/commit/?h=for-next&id=a6ea53ce77e9dd6e388d673bdd4d80741f97b914

Please double-check!

Confirmed working.

Thanks,
Guenter

Quoted 22 lines

Thanks!
Helge

Quoted 17 lines

  #else
-    {
-    unsigned long shift, temp1;
      __asm__ __volatile__  (
-"    zdep    %2,29,2,%3\n"        /* r19=(ofs&3)*8 */ +"    zdep    %2,29,2,%3\n"        /* shift=(ofs&3)*8 */ "    mtsp    %5, %%sr1\n"
  "    dep    %%r0,31,2,%2\n"
  "1:    ldw    0(%%sr1,%2),%0\n"

arch/parisc/kernel/unaligned.c +0 -1
@@ -214,7 +212,6 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
       ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 4b, "%1")
       : "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
       : "r" (regs->isr) );
-    }
   #endif
 
       DPRINTF("val = 0x%llx\n", val);
#3 lore raw
In reply to Guenter Roeck

Hi Helge,

Quoted 51 lines · Guenter Roeck

Convert to use real temp variables instead of clobbering processor registers. This aligns the 64-bit inline assembly code with the 32-bit assembly code which was rewritten with commit 427c1073a2a1 ("parisc/unaligned: Rewrite 32-bit inline assembly of emulate_ldd()").

While at it, fix comment in 32-bit rewrite code. Temporary variables are now used for both 32-bit and 64-bit code, so move their declarations to the function header.

No functional change intended.

---
Implemented while analyzing a bug. I am not really sure of it is worth the effort, but I figured that I might as well submit it.

 arch/parisc/kernel/unaligned.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
arch/parisc/kernel/unaligned.c +8 -9
index c520e551a165..622c7b549fb8 100644
@@ -169,7 +169,8 @@ static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
 static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 {
 	unsigned long saddr = regs->ior;
-	__u64 val = 0;
+	unsigned long shift;
+	__u64 val = 0, temp1;
 	ASM_EXCEPTIONTABLE_VAR(ret);
 
 	DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n", 
@@ -180,25 +181,22 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
 #ifdef CONFIG_64BIT
 	__asm__ __volatile__  (
-"	depd,z	%3,60,3,%%r19\n"		/* r19=(ofs&7)*8 */
-"	mtsp	%4, %%sr1\n"
-"	depd	%%r0,63,3,%3\n"
-"1:	ldd	0(%%sr1,%3),%0\n"
-"2:	ldd	8(%%sr1,%3),%%r20\n"
-"	subi	64,%%r19,%%r19\n"
-"	mtsar	%%r19\n"
-"	shrpd	%0,%%r20,%%sar,%0\n"
+"	depd,z	%4,60,3,%2\n"		/* shift=(ofs&7)*8 */
+"	mtsp	%5, %%sr1\n"
+"	depd	%%r0,63,3,%4\n"
+"1:	ldd	0(%%sr1,%4),%0\n"
+"2:	ldd	8(%%sr1,%4),%3\n"
+"	subi	64,%2,%2\n"

arch/parisc/kernel/unaligned.c static int emulate_ldd(struct pt_regs *regs, int toreg, int flop) · line 196

Quoted 10 lines
arch/parisc/kernel/unaligned.c +4 -3
+"	mtsar	%2\n"
+"	shrpd	%0,%3,%%sar,%0\n"
 "3:	\n"
 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b, "%1")
 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b, "%1")
-	: "=r" (val), "+r" (ret)
-	: "0" (val), "r" (saddr), "r" (regs->isr)
-	: "r19", "r20" );
+	: "+r" (val), "+r" (ret), "=&r" (shift), "=&r" (temp1)
+	: "r" (saddr), "r" (regs->isr) );

It looks like something went wrong when this patch was applied. It is now

+" depd,z %4,60,3,%3\n" /* shift=(ofs&7)*8 */
...
+ : "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
+ : "r" (regs->isr) );

meaning saddr is now %2, but the depd,z instruction still assumes it is %4. Unfortunately this results in a crash when trying to boot linux-next on parisc64.

The patch below on top of linux-next fixes the problem for me.

Guenter

---
iff --git a/arch/parisc/kernel/unaligned.c b/arch/parisc/kernel/unaligned.c index 31974eddedc9..a8e75e5b884a 100644

arch/parisc/kernel/unaligned.c +1 -1
@@ -181,7 +181,7 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
 #ifdef CONFIG_64BIT
        __asm__ __volatile__  (
-"      depd,z  %4,60,3,%3\n"           /* shift=(ofs&7)*8 */
+"      depd,z  %2,60,3,%3\n"           /* shift=(ofs&7)*8 */
 "      mtsp    %5, %%sr1\n"
 "      depd    %%r0,63,3,%2\n"
 "1:    ldd     0(%%sr1,%2),%0\n"
maintainer
#4 lore raw
Quoted 58 lines · Guenter Roeck

Hi Helge,

Quoted 55 lines · Guenter Roeck

Convert to use real temp variables instead of clobbering processor registers. This aligns the 64-bit inline assembly code with the 32-bit assembly code which was rewritten with commit 427c1073a2a1 ("parisc/unaligned: Rewrite 32-bit inline assembly of emulate_ldd()").

While at it, fix comment in 32-bit rewrite code. Temporary variables are now used for both 32-bit and 64-bit code, so move their declarations to the function header.

No functional change intended.

---
Implemented while analyzing a bug. I am not really sure of it is worth the effort, but I figured that I might as well submit it.

  arch/parisc/kernel/unaligned.c | 29 +++++++++++++----------------
  1 file changed, 13 insertions(+), 16 deletions(-)
arch/parisc/kernel/unaligned.c +10 -9
index c520e551a165..622c7b549fb8 100644
@@ -169,7 +169,8 @@ static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
  static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
  {
  	unsigned long saddr = regs->ior;
-	__u64 val = 0;
+	unsigned long shift;
+	__u64 val = 0, temp1;
  	ASM_EXCEPTIONTABLE_VAR(ret);
 
  	DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
@@ -180,25 +181,22 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
  #ifdef CONFIG_64BIT
  	__asm__ __volatile__  (
-"	depd,z	%3,60,3,%%r19\n"		/* r19=(ofs&7)*8 */
-"	mtsp	%4, %%sr1\n"
-"	depd	%%r0,63,3,%3\n"
-"1:	ldd	0(%%sr1,%3),%0\n"
-"2:	ldd	8(%%sr1,%3),%%r20\n"
-"	subi	64,%%r19,%%r19\n"
-"	mtsar	%%r19\n"
-"	shrpd	%0,%%r20,%%sar,%0\n"
+"	depd,z	%4,60,3,%2\n"		/* shift=(ofs&7)*8 */
+"	mtsp	%5, %%sr1\n"
+"	depd	%%r0,63,3,%4\n"
+"1:	ldd	0(%%sr1,%4),%0\n"
+"2:	ldd	8(%%sr1,%4),%3\n"
+"	subi	64,%2,%2\n"
+"	mtsar	%2\n"
+"	shrpd	%0,%3,%%sar,%0\n"
  "3:	\n"
  	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b, "%1")
Quoted 8 lines

arch/parisc/kernel/unaligned.c static int emulate_ldd(struct pt_regs *regs, int toreg, int flop) · line 196

Quoted 6 lines
arch/parisc/kernel/unaligned.c +2 -3
  	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b, "%1")
-	: "=r" (val), "+r" (ret)
-	: "0" (val), "r" (saddr), "r" (regs->isr)
-	: "r19", "r20" );
+	: "+r" (val), "+r" (ret), "=&r" (shift), "=&r" (temp1)
+	: "r" (saddr), "r" (regs->isr) );

It looks like something went wrong when this patch was applied.

I think this was my fault when I tried to reshuffle the input vars :-(

Quoted 7 lines

It is now

+" depd,z %4,60,3,%3\n" /* shift=(ofs&7)*8 */
...

(quoted diff) +2 -0
+       : "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
+       : "r" (regs->isr) );
Quoted 5 lines

meaning saddr is now %2, but the depd,z instruction still assumes it is %4. Unfortunately this results in a crash when trying to boot linux-next on parisc64.

The patch below on top of linux-next fixes the problem for me.

I fixed it up with your hunk below in the parisc for-next branch, so it should be fixed in linux-next soon.

THANKS!

Helge

Quoted 16 lines

Guenter

---
iff --git a/arch/parisc/kernel/unaligned.c b/arch/parisc/kernel/unaligned.c index 31974eddedc9..a8e75e5b884a 100644

arch/parisc/kernel/unaligned.c +1 -1
@@ -181,7 +181,7 @@ static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
 
  #ifdef CONFIG_64BIT
         __asm__ __volatile__  (
-"      depd,z  %4,60,3,%3\n"           /* shift=(ofs&7)*8 */
+"      depd,z  %2,60,3,%3\n"           /* shift=(ofs&7)*8 */
  "      mtsp    %5, %%sr1\n"
  "      depd    %%r0,63,3,%2\n"
  "1:    ldd     0(%%sr1,%2),%0\n"