Skip to contentSkip to conversation

powerpc: Make sstep.c more generally useful

PATCH New Paul Mackerras 1 participant 4 messages Sep 2, 2014 PowerPC

lore
Share & tools
lore
#0 lore raw

v2 cover powerpc: Make sstep.c more generally useful

This patch series modifies the code in arch/powerpc/lib/sstep.c so that it can be used by KVM in its instruction emulation, thereby reducing the number of different PowerPC instruction emulators that we have in the kernel. It does this by splitting the existing emulate_step() function into two: an analyse_instr() function that decodes an instruction and executes the easy ones, and an emulate_step() function that uses analyse_instr() and then executes the loads and stores. The subsequent two patches add emulation of some extra instructions.

v2: add #ifdef CONFIG_ALTIVEC around do_vec_{load,store} calls.

The patch series is against v3.17-rc1.

 arch/powerpc/include/asm/sstep.h |  62 +++
 arch/powerpc/lib/sstep.c         | 996 +++++++++++++++++++++++++--------------
 2 files changed, 698 insertions(+), 360 deletions(-)
#1 lore raw

PATCH v2 1/3 powerpc: Split out instruction analysis part of emulate_step()

This splits out the instruction analysis part of emulate_step() into a separate analyse_instr() function, which decodes the instruction, but doesn't execute any load or store instructions. It does execute integer instructions and branches which can be executed purely by updating register values in the pt_regs struct. For other instructions, it returns the instruction type and other details in a new instruction_op struct. emulate_step() then uses that information to execute loads, stores, cache operations, mfmsr, mtmsr[d], and (on 64-bit) sc instructions.

The reason for doing this is so that the KVM code can use it instead of having its own separate instruction emulation code. Possibly the alignment interrupt handler could also use this.

---
 arch/powerpc/include/asm/sstep.h |  61 +++
 arch/powerpc/lib/sstep.c         | 897 +++++++++++++++++++++++----------------
 2 files changed, 598 insertions(+), 360 deletions(-)
arch/powerpc/include/asm/sstep.h +61 -0
index f593b0f..1a693b1 100644
@@ -25,3 +25,64 @@ struct pt_regs;
 
 /* Emulate instructions that cause a transfer of control. */
 extern int emulate_step(struct pt_regs *regs, unsigned int instr);
+
+enum instruction_type {
+	COMPUTE,		/* arith/logical/CR op, etc. */
+	LOAD,
+	LOAD_MULTI,
+	LOAD_FP,
+	LOAD_VMX,
+	LOAD_VSX,
+	STORE,
+	STORE_MULTI,
+	STORE_FP,
+	STORE_VMX,
+	STORE_VSX,
+	LARX,
+	STCX,
+	BRANCH,
+	MFSPR,
+	MTSPR,
+	CACHEOP,
+	BARRIER,
+	SYSCALL,
+	MFMSR,
+	MTMSR,
+	RFI,
+	INTERRUPT,
+	UNKNOWN
+};
+
+#define INSTR_TYPE_MASK	0x1f
+
+/* Load/store flags, ORed in with type */
+#define SIGNEXT		0x20
+#define UPDATE		0x40	/* matches bit in opcode 31 instructions */
+#define BYTEREV		0x80
+
+/* Cacheop values, ORed in with type */
+#define CACHEOP_MASK	0x700
+#define DCBST		0
+#define DCBF		0x100
+#define DCBTST		0x200
+#define DCBT		0x300
+
+/* Size field in type word */
+#define SIZE(n)		((n) << 8)
+#define GETSIZE(w)	((w) >> 8)
+
+#define MKOP(t, f, s)	((t) | (f) | SIZE(s))
+
+struct instruction_op {
+	int type;
+	int reg;
+	unsigned long val;
+	/* For LOAD/STORE/LARX/STCX */
+	unsigned long ea;
+	int update_reg;
+	/* For MFSPR */
+	int spr;
+};
+
+extern int analyse_instr(struct instruction_op *op, struct pt_regs *regs,
+			 unsigned int instr);
arch/powerpc/lib/sstep.c +537 -360

1,202 lines not on this page · raw message

Signature · 1 line
2.1.0.rc1
#2 lore raw

PATCH v2 2/3 powerpc: Emulate icbi, mcrf and conditional-trap instructions

In reply to Paul Mackerras

This extends the instruction emulation done by analyse_instr() and emulate_step() to handle a few more instructions that are found in the kernel.

---
 arch/powerpc/include/asm/sstep.h |  1 +
 arch/powerpc/lib/sstep.c         | 60 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
arch/powerpc/include/asm/sstep.h +1 -0
index 1a693b1..d3a42cc 100644
@@ -66,6 +66,7 @@ enum instruction_type {
 #define DCBF		0x100
 #define DCBTST		0x200
 #define DCBT		0x300
+#define ICBI		0x400
 
 /* Size field in type word */
 #define SIZE(n)		((n) << 8)
arch/powerpc/lib/sstep.c +60 -0

102 lines not on this page · raw message

Signature · 1 line
2.1.0.rc1
#3 lore raw

PATCH v2 3/3 powerpc: Implement emulation of string loads and stores

In reply to Paul Mackerras

The size field of the op.type word is now the total number of bytes to be loaded or stored.

---
 arch/powerpc/lib/sstep.c | 59 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 49 insertions(+), 10 deletions(-)
arch/powerpc/lib/sstep.c +49 -10
index 209a506..54651fc 100644
@@ -1433,11 +1433,24 @@ int __kprobes analyse_instr(struct instruction_op *op, struct pt_regs *regs,
 			break;
 
 #endif
+		case 533:	/* lswx */
+			op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
+			break;
 
 		case 534:	/* lwbrx */
 			op->type = MKOP(LOAD, BYTEREV, 4);
 			break;
 
+		case 597:	/* lswi */
+			if (rb == 0)
+				rb = 32;	/* # bytes to load */
+			op->type = MKOP(LOAD_MULTI, 0, rb);
+			op->ea = 0;
+			if (ra)
+				op->ea = truncate_if_32bit(regs->msr,
+							   regs->gpr[ra]);
+			break;
+
 #ifdef CONFIG_PPC_FPU
 		case 535:	/* lfsx */
 		case 567:	/* lfsux */
@@ -1475,11 +1488,25 @@ int __kprobes analyse_instr(struct instruction_op *op, struct pt_regs *regs,
 			break;
 
 #endif
+		case 661:	/* stswx */
+			op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
+			break;
+
 		case 662:	/* stwbrx */
 			op->type = MKOP(STORE, BYTEREV, 4);
 			op->val = byterev_4(regs->gpr[rd]);
 			break;
 
+		case 725:
+			if (rb == 0)
+				rb = 32;	/* # bytes to store */
+			op->type = MKOP(STORE_MULTI, 0, rb);
+			op->ea = 0;
+			if (ra)
+				op->ea = truncate_if_32bit(regs->msr,
+							   regs->gpr[ra]);
+			break;
+
 		case 790:	/* lhbrx */
 			op->type = MKOP(LOAD, BYTEREV, 2);
 			break;
@@ -1553,15 +1580,14 @@ int __kprobes analyse_instr(struct instruction_op *op, struct pt_regs *regs,
 		break;
 
 	case 46:	/* lmw */
-		ra = (instr >> 16) & 0x1f;
 		if (ra >= rd)
 			break;		/* invalid form, ra in range to load */
-		op->type = MKOP(LOAD_MULTI, 0, 4);
+		op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
 		op->ea = dform_ea(instr, regs);
 		break;
 
 	case 47:	/* stmw */
-		op->type = MKOP(STORE_MULTI, 0, 4);
+		op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
 		op->ea = dform_ea(instr, regs);
 		break;
 
@@ -1744,7 +1770,7 @@ int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr)
 	int r, err, size;
 	unsigned long val;
 	unsigned int cr;
-	int rd;
+	int i, rd, nb;
 
 	r = analyse_instr(&op, regs, instr);
 	if (r != 0)
@@ -1866,12 +1892,18 @@ int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr)
 		if (regs->msr & MSR_LE)
 			return 0;
 		rd = op.reg;
-		do {
-			err = read_mem(&regs->gpr[rd], op.ea, 4, regs);
+		for (i = 0; i < size; i += 4) {
+			nb = size - i;
+			if (nb > 4)
+				nb = 4;
+			err = read_mem(&regs->gpr[rd], op.ea, nb, regs);
 			if (err)
 				return 0;
+			if (nb < 4)	/* left-justify last bytes */
+				regs->gpr[rd] <<= 32 - 8 * nb;
 			op.ea += 4;
-		} while (++rd < 32);
+			++rd;
+		}
 		goto instr_done;
 
 	case STORE:
@@ -1914,12 +1946,19 @@ int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr)
 		if (regs->msr & MSR_LE)
 			return 0;
 		rd = op.reg;
-		do {
-			err = write_mem(regs->gpr[rd], op.ea, 4, regs);
+		for (i = 0; i < size; i += 4) {
+			val = regs->gpr[rd];
+			nb = size - i;
+			if (nb > 4)
+				nb = 4;
+			else
+				val >>= 32 - 8 * nb;
+			err = write_mem(val, op.ea, nb, regs);
 			if (err)
 				return 0;
 			op.ea += 4;
-		} while (++rd < 32);
+			++rd;
+		}
 		goto instr_done;
 
 	case MFMSR:
Signature · 1 line
2.1.0.rc1