[1/5] powerpc/lib/sstep: Add cmpb instruction emulation

Submitted by Matt Brown on July 13, 2017, 3:25 a.m.

Details

Message ID 20170713032548.451-1-matthew.brown.dev@gmail.com
State New
Headers show

Commit Message

Matt Brown July 13, 2017, 3:25 a.m.
This patch adds emulation of the cmpb instruction, enabling xmon to
emulate this instruction.

Signed-off-by: Matt Brown <matthew.brown.dev@gmail.com>
---
 arch/powerpc/lib/sstep.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Andrew Donnellan July 13, 2017, 3:51 a.m.
On 13/07/17 13:25, Matt Brown wrote:
> @@ -1049,6 +1066,13 @@ int analyse_instr(struct instruction_op *op, struct pt_regs *regs,
>  			do_cmp_unsigned(regs, val, val2, rd >> 2);
>  			goto instr_done;
>
> +		case 19173952: /* cmpb */

This looks wrong and should never trigger, given that the switch 
statement is comparing against ((instr >> 1) & 0x3ff).

How did you get this value?
Segher Boessenkool July 13, 2017, 6:43 a.m.
On Thu, Jul 13, 2017 at 01:51:30PM +1000, Andrew Donnellan wrote:
> On 13/07/17 13:25, Matt Brown wrote:
> >@@ -1049,6 +1066,13 @@ int analyse_instr(struct instruction_op *op, struct 
> >pt_regs *regs,
> > 			do_cmp_unsigned(regs, val, val2, rd >> 2);
> > 			goto instr_done;
> >
> >+		case 19173952: /* cmpb */
> 
> This looks wrong and should never trigger, given that the switch 
> statement is comparing against ((instr >> 1) & 0x3ff).
> 
> How did you get this value?

The correct number is 508, and 19173952 = 37744*508.  How to get
37744 is a mystery though :-)


Segher
Segher Boessenkool July 13, 2017, 6:46 a.m.
On Thu, Jul 13, 2017 at 01:25:44PM +1000, Matt Brown wrote:
> +static nokprobe_inline void do_cmpb(struct pt_regs *regs, unsigned long v1,
> +				unsigned long v2, int rd)
> +{
> +	unsigned long out_val, mask;
> +	int i;
> +
> +	out_val = 0;
> +	for (i = 0; i < 8; i++) {
> +		mask = 0xff << (i * 8);

0xffUL ?

> +
> +		if ((v1 & mask) == (v2 & mask))
> +			out_val |= mask;
> +	}
> +
> +	regs->gpr[rd] = out_val;
> +}


Segher
Segher Boessenkool July 13, 2017, 7:10 a.m.
On Thu, Jul 13, 2017 at 01:43:53AM -0500, Segher Boessenkool wrote:
> On Thu, Jul 13, 2017 at 01:51:30PM +1000, Andrew Donnellan wrote:
> > On 13/07/17 13:25, Matt Brown wrote:
> > >@@ -1049,6 +1066,13 @@ int analyse_instr(struct instruction_op *op, struct 
> > >pt_regs *regs,
> > > 			do_cmp_unsigned(regs, val, val2, rd >> 2);
> > > 			goto instr_done;
> > >
> > >+		case 19173952: /* cmpb */
> > 
> > This looks wrong and should never trigger, given that the switch 
> > statement is comparing against ((instr >> 1) & 0x3ff).
> > 
> > How did you get this value?
> 
> The correct number is 508, and 19173952 = 37744*508.  How to get
> 37744 is a mystery though :-)

Ah.  Take the binary representation of 508, and interpret that as if
it were octal :-)  Those pesky meddling leading zeroes!


Segher

Patch hide | download patch | download mbox

diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c
index 33117f8..f3e9ba8 100644
--- a/arch/powerpc/lib/sstep.c
+++ b/arch/powerpc/lib/sstep.c
@@ -596,6 +596,23 @@  static nokprobe_inline void do_cmp_unsigned(struct pt_regs *regs, unsigned long
 	regs->ccr = (regs->ccr & ~(0xf << shift)) | (crval << shift);
 }
 
+static nokprobe_inline void do_cmpb(struct pt_regs *regs, unsigned long v1,
+				unsigned long v2, int rd)
+{
+	unsigned long out_val, mask;
+	int i;
+
+	out_val = 0;
+	for (i = 0; i < 8; i++) {
+		mask = 0xff << (i * 8);
+
+		if ((v1 & mask) == (v2 & mask))
+			out_val |= mask;
+	}
+
+	regs->gpr[rd] = out_val;
+}
+
 static nokprobe_inline int trap_compare(long v1, long v2)
 {
 	int ret = 0;
@@ -1049,6 +1066,13 @@  int analyse_instr(struct instruction_op *op, struct pt_regs *regs,
 			do_cmp_unsigned(regs, val, val2, rd >> 2);
 			goto instr_done;
 
+		case 19173952: /* cmpb */
+			val = regs->gpr[rd];
+			val2 = regs->gpr[rb];
+
+			do_cmpb(regs, val, val2, ra);
+			goto instr_done;
+
 /*
  * Arithmetic instructions
  */