Skip to contentSkip to conversation

powerpc sstep: Extend instruction emulation support

PATCH Superseded Sandipan Das 4 participants 16 messages Sep 3, 2018 – Feb 20, 2019 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover powerpc sstep: Extend instruction emulation support

This series adds emulation support for some additional ISA 3.0 instructions, most of which are now generated by a recent compiler (e.g. gcc-8.x) when building the kernel with CONFIG_POWER9_CPU = y.

PrasannaKumar Muralidharan (1):
powerpc sstep: Add modsw, moduw instruction emulation

Sandipan Das (5):
powerpc sstep: Add maddhd, maddhdu, maddld instruction emulation powerpc sstep: Add darn instruction emulation
powerpc sstep: Add cnttzw, cnttzd instruction emulation powerpc sstep: Add extswsli instruction emulation powerpc sstep: Add modsd, modud instruction emulation

 arch/powerpc/lib/sstep.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 2 deletions(-)
Signature · 1 line
2.14.4
#1 lore raw

PATCH 1/6 powerpc sstep: Add maddhd, maddhdu, maddld instruction emulation

This adds emulation support for the following integer instructions:
* Multiply-Add High Doubleword (maddhd)
* Multiply-Add High Doubleword Unsigned (maddhdu)
* Multiply-Add Low Doubleword (maddld)

---
 arch/powerpc/lib/sstep.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
arch/powerpc/lib/sstep.c +34 -1
index d81568f783e5..b40ec18515bd 100644
@@ -1169,7 +1169,7 @@ static nokprobe_inline int trap_compare(long v1, long v2)
 int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 		  unsigned int instr)
 {
-	unsigned int opcode, ra, rb, rd, spr, u;
+	unsigned int opcode, ra, rb, rc, rd, spr, u;
 	unsigned long int imm;
 	unsigned long int val, val2;
 	unsigned int mb, me, sh;
@@ -1292,6 +1292,7 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 	rd = (instr >> 21) & 0x1f;
 	ra = (instr >> 16) & 0x1f;
 	rb = (instr >> 11) & 0x1f;
+	rc = (instr >> 6) & 0x1f;
 
 	switch (opcode) {
 #ifdef __powerpc64__
@@ -1305,6 +1306,38 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 			goto trap;
 		return 1;
 
+#ifdef __powerpc64__
+	case 4:
+		if (!cpu_has_feature(CPU_FTR_ARCH_300))
+			return -1;
+
+		switch (instr & 0x3f) {
+		case 48:	/* maddhd */
+			asm("maddhd %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+
+		case 49:	/* maddhdu */
+			asm("maddhdu %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+
+		case 51:	/* maddld */
+			asm("maddld %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+		}
+
+		/*
+		 * There are other instructions from ISA 3.0 with the same
+		 * primary opcode which do not have emulation support yet.
+		 */
+		return -1;
+#endif
+
 	case 7:		/* mulli */
 		op->val = regs->gpr[ra] * (short) instr;
 		goto compute_done;
Signature · 1 line
2.14.4
#5 lore raw

PATCH 2/6 powerpc sstep: Add darn instruction emulation

In reply to Sandipan Das

This adds emulation support for the following integer instructions:
* Deliver A Random Number (darn)

---
 arch/powerpc/lib/sstep.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/powerpc/lib/sstep.c +19 -0
index b40ec18515bd..18ac0a26c4fc 100644
@@ -1728,6 +1728,25 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 				(int) regs->gpr[rb];
 			goto arith_done;
 
+		case 755:	/* darn */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			switch (ra & 0x3) {
+			case 0:
+				asm("darn %0,0" : "=r" (op->val));
+				goto compute_done;
+
+			case 1:
+				asm("darn %0,1" : "=r" (op->val));
+				goto compute_done;
+
+			case 2:
+				asm("darn %0,2" : "=r" (op->val));
+				goto compute_done;
+			}
+
+			return -1;
+
 
 /*
  * Logical instructions
Signature · 1 line
2.14.4
#11 lore raw

PATCH 4/6 powerpc sstep: Add extswsli instruction emulation

In reply to Sandipan Das

This adds emulation support for the following integer instructions:
* Extend-Sign Word and Shift Left Immediate (extswsli[.])

---
 arch/powerpc/lib/sstep.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/powerpc/lib/sstep.c +14 -0
index d57cb4e28621..828677df47b2 100644
@@ -1932,6 +1932,20 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 				op->xerval &= ~XER_CA;
 			set_ca32(op, op->xerval & XER_CA);
 			goto logical_done;
+
+		case 890:	/* extswsli with sh_5 = 0 */
+		case 891:	/* extswsli with sh_5 = 1 */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->type = COMPUTE + SETREG;
+			sh = rb | ((instr & 2) << 4);
+			val = (signed int) regs->gpr[rd];
+			if (sh)
+				op->val = ROTATE(val, sh) & MASK64(0, 63 - sh);
+			else
+				op->val = val;
+			goto logical_done;
+
 #endif /* __powerpc64__ */
 
 /*
Signature · 1 line
2.14.4
#8 lore raw

PATCH 3/6 powerpc sstep: Add cnttzw, cnttzd instruction emulation

In reply to Sandipan Das

This adds emulation support for the following integer instructions:
* Count Trailing Zeros Word (cnttzw[.])
* Count Trailing Zeros Doubleword (cnttzd[.])

---
 arch/powerpc/lib/sstep.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/powerpc/lib/sstep.c +14 -0
index 18ac0a26c4fc..d57cb4e28621 100644
@@ -1816,6 +1816,20 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 		case 506:	/* popcntd */
 			do_popcnt(regs, op, regs->gpr[rd], 64);
 			goto logical_done_nocc;
+#endif
+		case 538:	/* cnttzw */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = (unsigned int) regs->gpr[rd];
+			op->val = ( val ? __builtin_ctz(val) : 32 );
+			goto logical_done;
+#ifdef __powerpc64__
+		case 570:	/* cnttzd */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = regs->gpr[rd];
+			op->val = ( val ? __builtin_ctzl(val) : 64 );
+			goto logical_done;
 #endif
 		case 922:	/* extsh */
 			op->val = (signed short) regs->gpr[rd];
Signature · 1 line
2.14.4
#12 lore raw

PATCH 5/6 powerpc sstep: Add modsw, moduw instruction emulation

In reply to Sandipan Das

From: PrasannaKumar Muralidharan <[email protected]>

This adds emulation support for the following integer instructions:
* Modulo Signed Word (modsw)
* Modulo Unsigned Word (moduw)

---
 arch/powerpc/lib/sstep.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/powerpc/lib/sstep.c +14 -0
index 828677df47b2..ddc2de28e00a 100644
@@ -1708,6 +1708,13 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 		case 266:	/* add */
 			op->val = regs->gpr[ra] + regs->gpr[rb];
 			goto arith_done;
+
+		case 267:	/* moduw */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = (unsigned int) regs->gpr[ra] %
+				(unsigned int) regs->gpr[rb];
+			goto compute_done;
 #ifdef __powerpc64__
 		case 457:	/* divdu */
 			op->val = regs->gpr[ra] / regs->gpr[rb];
@@ -1747,6 +1754,13 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 
 			return -1;
 
+		case 779:	/* modsw */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = (int) regs->gpr[ra] %
+				(int) regs->gpr[rb];
+			goto compute_done;
+
 
 /*
  * Logical instructions
Signature · 1 line
2.14.4
#13 lore raw

PATCH 6/6 powerpc sstep: Add modsd, modud instruction emulation

In reply to Sandipan Das

This adds emulation support for the following integer instructions:
* Modulo Signed Doubleword (modsd)
* Modulo Unsigned Doubleword (modud)

---
 arch/powerpc/lib/sstep.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
arch/powerpc/lib/sstep.c +15 -2
index ddc2de28e00a..b3b508d175fb 100644
@@ -1704,7 +1704,13 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 				(int) regs->gpr[rb];
 
 			goto arith_done;
-
+#ifdef __powerpc64__
+		case 265:	/* modud */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = regs->gpr[ra] % regs->gpr[rb];
+			goto compute_done;
+#endif
 		case 266:	/* add */
 			op->val = regs->gpr[ra] + regs->gpr[rb];
 			goto arith_done;
@@ -1753,7 +1759,14 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 			}
 
 			return -1;
-
+#ifdef __powerpc64__
+		case 777:	/* modsd */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = (long int) regs->gpr[ra] %
+				(long int) regs->gpr[rb];
+			goto compute_done;
+#endif
 		case 779:	/* modsw */
 			if (!cpu_has_feature(CPU_FTR_ARCH_300))
 				return -1;
Signature · 1 line
2.14.4
#14 lore raw

Re: [PATCH 6/6] powerpc sstep: Add modsd, modud instruction emulation

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1713

Quoted 7 lines · Sandipan Das
arch/powerpc/lib/sstep.c +7 -0
+#ifdef __powerpc64__
+		case 265:	/* modud */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = regs->gpr[ra] % regs->gpr[rb];
+			goto compute_done;
+#endif

The mod instruction has special cases that aren't handled by this C code, too (divide by 0, or signed division of the most negative number by -1). For the mod intruction the behaviour is undefined in those cases, but you probably should force some specific behaviour. You don't want the kernel to execute a trap instruction, etc. :-)

Segher

#2 lore raw

Re: [PATCH 1/6] powerpc sstep: Add maddhd, maddhdu, maddld instruction emulation

In reply to Sandipan Das

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1319

Quoted 11 lines · Sandipan Das
arch/powerpc/lib/sstep.c +11 -0
+#ifdef __powerpc64__
+	case 4:
+		if (!cpu_has_feature(CPU_FTR_ARCH_300))
+			return -1;
+
+		switch (instr & 0x3f) {
+		case 48:	/* maddhd */
+			asm("maddhd %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;

If running maddhd does not work, will running it in kernel mode work?

I think you should *actually* emulate it.

(Same for the next patch, "darn", but emulation of that is much more interesting).

Segher

#9 lore raw

Re: [PATCH 3/6] powerpc sstep: Add cnttzw, cnttzd instruction emulation

In reply to Sandipan Das

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1832

Quoted 13 lines · Sandipan Das
arch/powerpc/lib/sstep.c +13 -0
+		case 538:	/* cnttzw */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = (unsigned int) regs->gpr[rd];
+			op->val = ( val ? __builtin_ctz(val) : 32 );
+			goto logical_done;
+#ifdef __powerpc64__
+		case 570:	/* cnttzd */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = regs->gpr[rd];
+			op->val = ( val ? __builtin_ctzl(val) : 64 );
+			goto logical_done;

__builtin_ctz(val) is undefined for val == 0.

Segher

#10 lore raw

Re: [PATCH 3/6] powerpc sstep: Add cnttzw, cnttzd instruction emulation

Quoted 16 lines · Segher Boessenkool

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1832

Quoted 13 lines · Sandipan Das
arch/powerpc/lib/sstep.c +13 -0
+		case 538:	/* cnttzw */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = (unsigned int) regs->gpr[rd];
+			op->val = ( val ? __builtin_ctz(val) : 32 );
+			goto logical_done;
+#ifdef __powerpc64__
+		case 570:	/* cnttzd */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			val = regs->gpr[rd];
+			op->val = ( val ? __builtin_ctzl(val) : 64 );
+			goto logical_done;

__builtin_ctz(val) is undefined for val == 0.

Which would be why he only calls it when val != 0, presumably, and uses 64 when val == 0. Apart from idiosyncratic whitespace his code looks correct to me.

Are you saying there is a bug in his code, or that his patch description is incomplete, or what?

Paul.

#3 lore raw

Re: [PATCH 1/6] powerpc sstep: Add maddhd, maddhdu, maddld instruction emulation

In reply to Segher Boessenkool

Hi Segher,

Quoted 15 lines · Segher Boessenkool

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1319

Quoted 11 lines · Sandipan Das
arch/powerpc/lib/sstep.c +11 -0
+#ifdef __powerpc64__
+	case 4:
+		if (!cpu_has_feature(CPU_FTR_ARCH_300))
+			return -1;
+
+		switch (instr & 0x3f) {
+		case 48:	/* maddhd */
+			asm("maddhd %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;

If running maddhd does not work, will running it in kernel mode work?

Not sure what you meant here but one of the scenarios that I'm aware of where this is will be used is if we place a probe at a location having an maddhd instruction. The kernel would first attempt to emulate its behaviour, which in this case is done by executing the same instruction (similar to what is done for mulhd and mulhw) and if that fails, try to execute the instruction natively.

- Sandipan

#15 lore raw

Re: [PATCH 6/6] powerpc sstep: Add modsd, modud instruction emulation

In reply to Segher Boessenkool

Hi Segher,

Quoted 15 lines · Segher Boessenkool

arch/powerpc/lib/sstep.c int analyse_instr(struct instruction_op *op, const struct pt_regs *regs, · line 1713

Quoted 7 lines · Sandipan Das
arch/powerpc/lib/sstep.c +7 -0
+#ifdef __powerpc64__
+		case 265:	/* modud */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			op->val = regs->gpr[ra] % regs->gpr[rb];
+			goto compute_done;
+#endif

The mod instruction has special cases that aren't handled by this C code, too (divide by 0, or signed division of the most negative number by -1). For the mod intruction the behaviour is undefined in those cases, but you probably should force some specific behaviour. You don't want the kernel to execute a trap instruction, etc. :-)

Agreed. In that case, the same would apply to the divw, divwu, divd and divdu instructions as well, right? Cause I don't see these cases being handled for them currently.

Also, if I execute a modulo or division instruction for any of these special cases in a userspace binary, I don't see any exceptions being generated. It's just that the result is undefined (usually same as one of the source operands, I don't remember if it was the dividend or the divisor). So, I'm wondering if this would be necessary.

- Sandipan

#6 lore raw

Re: [PATCH 2/6] powerpc sstep: Add darn instruction emulation

In reply to Sandipan Das
Quoted 2 lines · Sandipan Das

This adds emulation support for the following integer instructions:
* Deliver A Random Number (darn)

This doesn't build with old binutils. We need to support old binutils.

{standard input}:4343: Error: Unrecognized opcode: `darn'

You need to use PPC_DARN().

cheers

Quoted 32 lines
arch/powerpc/lib/sstep.c +19 -0
index b40ec18515bd..18ac0a26c4fc 100644
@@ -1728,6 +1728,25 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 				(int) regs->gpr[rb];
 			goto arith_done;
 
+		case 755:	/* darn */
+			if (!cpu_has_feature(CPU_FTR_ARCH_300))
+				return -1;
+			switch (ra & 0x3) {
+			case 0:
+				asm("darn %0,0" : "=r" (op->val));
+				goto compute_done;
+
+			case 1:
+				asm("darn %0,1" : "=r" (op->val));
+				goto compute_done;
+
+			case 2:
+				asm("darn %0,2" : "=r" (op->val));
+				goto compute_done;
+			}
+
+			return -1;
+
 
 /*
  * Logical instructions
Signature · 1 line
2.14.4
#4 lore raw

Re: [PATCH 1/6] powerpc sstep: Add maddhd, maddhdu, maddld instruction emulation

In reply to Sandipan Das
Quoted 4 lines · Sandipan Das

This adds emulation support for the following integer instructions:
* Multiply-Add High Doubleword (maddhd)
* Multiply-Add High Doubleword Unsigned (maddhdu)
* Multiply-Add Low Doubleword (maddld)

This doesn't build with old binutils.

{standard input}:2089: Error: Unrecognized opcode: maddld' {standard input}:2104: Error: Unrecognized opcode: maddhdu' {standard input}:1141: Error: Unrecognized opcode: `maddhd'

You'll need to add hand built versions, see ppc-opcode.h for examples.

cheers

Quoted 62 lines
arch/powerpc/lib/sstep.c +34 -1
index d81568f783e5..b40ec18515bd 100644
@@ -1169,7 +1169,7 @@ static nokprobe_inline int trap_compare(long v1, long v2)
 int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 		  unsigned int instr)
 {
-	unsigned int opcode, ra, rb, rd, spr, u;
+	unsigned int opcode, ra, rb, rc, rd, spr, u;
 	unsigned long int imm;
 	unsigned long int val, val2;
 	unsigned int mb, me, sh;
@@ -1292,6 +1292,7 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 	rd = (instr >> 21) & 0x1f;
 	ra = (instr >> 16) & 0x1f;
 	rb = (instr >> 11) & 0x1f;
+	rc = (instr >> 6) & 0x1f;
 
 	switch (opcode) {
 #ifdef __powerpc64__
@@ -1305,6 +1306,38 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
 			goto trap;
 		return 1;
 
+#ifdef __powerpc64__
+	case 4:
+		if (!cpu_has_feature(CPU_FTR_ARCH_300))
+			return -1;
+
+		switch (instr & 0x3f) {
+		case 48:	/* maddhd */
+			asm("maddhd %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+
+		case 49:	/* maddhdu */
+			asm("maddhdu %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+
+		case 51:	/* maddld */
+			asm("maddld %0,%1,%2,%3" : "=r" (op->val) :
+			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]),
+			    "r" (regs->gpr[rc]));
+			goto compute_done;
+		}
+
+		/*
+		 * There are other instructions from ISA 3.0 with the same
+		 * primary opcode which do not have emulation support yet.
+		 */
+		return -1;
+#endif
+
 	case 7:		/* mulli */
 		op->val = regs->gpr[ra] * (short) instr;
 		goto compute_done;
Signature · 1 line
2.14.4
#7 lore raw

Re: [PATCH 2/6] powerpc sstep: Add darn instruction emulation

In reply to Michael Ellerman

Hi Michael,

Quoted 7 lines · Michael Ellerman
Quoted 2 lines · Sandipan Das

This adds emulation support for the following integer instructions:
* Deliver A Random Number (darn)

This doesn't build with old binutils. We need to support old binutils.

Quoted 8 lines

{standard input}:4343: Error: Unrecognized opcode: `darn'

You need to use PPC_DARN().

cheers

[...]

Thanks for pointing these out. Will post v2 with the required changes.

- Sandipan