Skip to contentSkip to conversation

powerpc: Emulate most load and store instructions in emulate_step()

RFC PATCH In review Paul Mackerras 6 participants 13 messages May 20, 2010 – Jun 2, 2010 PowerPC

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 powerpc: Emulate most load and store instructions in emulate_step()

This extends the emulate_step() function to handle most of the load and store instructions implemented on current 64-bit server processors. The aim is to handle all the load and store instructions used in the kernel, so this handles the Altivec/VMX lvx and stvx and the VSX lxv2dx and stxv2dx instructions (implemented in POWER7).

The new code can emulate user mode instructions, and checks the effective address for a load or store if the saved state is for user mode. It doesn't handle little-endian mode at present.

For floating-point, Altivec/VMX and VSX instructions, it checks that the saved MSR has the enable bit for the relevant facility set, and if so, assumes that the FP/VMX/VSX registers contain valid state, and does loads or stores directly to/from the FP/VMX/VSX registers, using assembly helpers in ldstfp.S.

---
 arch/powerpc/lib/Makefile |    4 +-
 arch/powerpc/lib/ldstfp.S |  207 +++++++++++++
 arch/powerpc/lib/sstep.c  |  753 +++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 937 insertions(+), 27 deletions(-)
 create mode 100644 arch/powerpc/lib/ldstfp.S
arch/powerpc/lib/Makefile +2 -2
index 3040dac..7581dbf 100644
@@ -18,8 +18,8 @@ obj-$(CONFIG_HAS_IOMEM)	+= devres.o
 
 obj-$(CONFIG_PPC64)	+= copypage_64.o copyuser_64.o \
 			   memcpy_64.o usercopy_64.o mem_64.o string.o
-obj-$(CONFIG_XMON)	+= sstep.o
-obj-$(CONFIG_KPROBES)	+= sstep.o
+obj-$(CONFIG_XMON)	+= sstep.o ldstfp.o
+obj-$(CONFIG_KPROBES)	+= sstep.o ldstfp.o
 
 ifeq ($(CONFIG_PPC64),y)
 obj-$(CONFIG_SMP)	+= locks.o
arch/powerpc/lib/ldstfp.S new file +207 -0

207 lines not on this page · raw message

arch/powerpc/lib/sstep.c +728 -25

841 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#1 lore raw

RFC PATCH 1/1 powerpc: Emulate most load and store instructions in emulate_step()

[resending to hit the list]

arch/powerpc/lib/sstep.c int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr) · line 442

Quoted 8 lines
arch/powerpc/lib/sstep.c +6 -0
+		if (regs->gpr[0] == 0x1ebe &&
+		    cpu_has_feature(CPU_FTR_REAL_LE)) {
+			regs->msr ^= MSR_LE;
+			goto instr_done;
+		}
 		regs->gpr[9] = regs->gpr[13];
+		regs->gpr[10] = MSR_KERNEL;

this hunk didn't seem to be mentioned in the change log

Quoted 1 line

+ static inline unsigned long xform_ea(unsigned int instr, struct pt_regs *regs)

..

arch/powerpc/lib/sstep.c static int __kprobes branch_taken(unsigned int instr, struct pt_regs *regs) · line 122

Quoted 5 lines
arch/powerpc/lib/sstep.c +5 -0
+	if (ra) {
+		ea += regs->gpr[ra];
+		if (instr & 0x40)		/* update forms */
+			regs->gpr[ra] = ea;
+	}

if the instruction faults, we shouldn't update ra

arch/powerpc/lib/sstep.c int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr) · line 889

Quoted 12 lines
arch/powerpc/lib/sstep.c +11 -0
+	case 62:	/* std[u] */
+		val = regs->gpr[rd];
+		switch (instr & 3) {
+		case 0:		/* ld */
+			err = write_mem(val, dsform_ea(instr, regs), 8, regs);
+			goto ldst_done;
+		case 1:		/* ldu */
+			err = write_mem(val, dsform_ea(instr, regs), 8, regs);
+			goto ldst_done;
+		}
+		break;

comments seem to be copied

milton

#2 lore raw

RFC PATCH 1/1 powerpc: Emulate nop too

In reply to Paul Mackerras

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

        case 24:
                rd = (instr >> 21) & 0x1f;
                if (rd != 0)
                        break;
                rb = (instr >> 11) & 0x1f;
                if (rb != 0)
                        break;
                imm = (instr & 0xffff);
                if (imm == 0) {
                        regs->nip += 4;
                        return 1;
                }
                break;

But, the following is more straightforward for this case at least.

---
 arch/powerpc/lib/sstep.c |    6 ++++++
 1 file changed, 6 insertions(+)
arch/powerpc/lib/sstep.c +6 -0
Index: linux-27may/arch/powerpc/lib/sstep.c
===================================================================
@@ -412,6 +412,12 @@ int __kprobes emulate_step(struct pt_reg
 	int err;
 	mm_segment_t oldfs;
 
+	/* ori 0,0,0 is a nop. Emulate that too */
+	if (instr == 0x60000000) {
+		regs->nip += 4;
+		return 1;
+	}
+
 	opcode = instr >> 26;
 	switch (opcode) {
 	case 16:	/* bc */
#3 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

Quoted 4 lines · Ananth N Mavinakayanahalli

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Dare I ask why we need to emulate nop?

- k

Quoted 52 lines

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

       case 24:
               rd = (instr >> 21) & 0x1f;
               if (rd != 0)
                       break;
               rb = (instr >> 11) & 0x1f;
               if (rb != 0)
                       break;
               imm = (instr & 0xffff);
               if (imm == 0) {
                       regs->nip += 4;
                       return 1;
               }
               break;

But, the following is more straightforward for this case at least.

---
arch/powerpc/lib/sstep.c | 6 ++++++

1 file changed, 6 insertions(+)

Index: linux-27may/arch/powerpc/lib/sstep.c
===================================================================

arch/powerpc/lib/sstep.c +6 -0
@@ -412,6 +412,12 @@ int __kprobes emulate_step(struct pt_reg
 	int err;
 	mm_segment_t oldfs;
 
+	/* ori 0,0,0 is a nop. Emulate that too */
+	if (instr == 0x60000000) {
+		regs->nip += 4;
+		return 1;
+	}
+
 	opcode = instr >> 26;
 	switch (opcode) {
 	case 16:	/* bc */

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#4 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

Quoted 9 lines · Kumar Gala
Quoted 4 lines · Ananth N Mavinakayanahalli

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Dare I ask why we need to emulate nop?

We are close to getting userspace probes done. Some userspace apps that've gained 'markers' from DTrace or elsewhere, are being enabled to expose those in through debuginfo on Linux and the underlying instruction there is a nop.

Ananth

#5 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

In reply to Ananth N Mavinakayanahalli
Quoted 2 lines · Ananth N Mavinakayanahalli

While we are at it, can we also add nop to the list of emulated instructions?

I have a patch in development that emulates most of the arithmetic, logical and shift/rotate instructions, including ori.

While you're here (in a virtual sense at least :), could you explain what's going on with the emulate_step() call in resume_execution() in arch/powerpc/kernel/kprobes.c? It looks like, having decided that emulate_step() can't handle the instruction, you single-step the instruction out of line and then call emulate_step again on the same instruction, in resume_execution(). Why on earth is it trying to emulate the instruction when it has already been executed at this point? Is there any reason why we can't just remove the emulate_step call from resume_execution()?

Paul.

#6 lore raw

powerpc: remove resume_execution() in kprobes

Quoted 7 lines · Paul Mackerras
Quoted 2 lines · Ananth N Mavinakayanahalli

While we are at it, can we also add nop to the list of emulated instructions?

I have a patch in development that emulates most of the arithmetic, logical and shift/rotate instructions, including ori.

OK.

Quoted 9 lines

While you're here (in a virtual sense at least :), could you explain what's going on with the emulate_step() call in resume_execution() in arch/powerpc/kernel/kprobes.c? It looks like, having decided that emulate_step() can't handle the instruction, you single-step the instruction out of line and then call emulate_step again on the same instruction, in resume_execution(). Why on earth is it trying to emulate the instruction when it has already been executed at this point? Is there any reason why we can't just remove the emulate_step call from resume_execution()?

You are right. We needed emulate_step() in resume_execution() before we had the code to handle the emulation in kprobe_handler() at the time of the breakpoint it. At the time we needed it mainly to ensure branch targets are reflected correctly in regs->nip if the stepped instruction was a branch.

However, we now don't get to post_kprobe_handler() at all if emulate_step() returned 1 at the time of the breakpoint hit. It suffices if we just fixup the nip here. Patch below. Tested for various instructions that can and can't be emulated...

---
From: Ananth N Mavinakayanahalli <[email protected]>

emulate_step() in kprobe_handler() would've already determined if the probed instruction can be emulated. We single-step in hardware only if the instruction couldn't be emulated. resume_execution() therefore is superfluous -- all we need is to fix up the instruction pointer after single-stepping.

Thanks to Paul Mackerras for catching this.

---
 arch/powerpc/kernel/kprobes.c |   14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
arch/powerpc/kernel/kprobes.c +2 -12
Index: linux-2.6.34/arch/powerpc/kernel/kprobes.c
===================================================================
@@ -375,17 +375,6 @@ static int __kprobes trampoline_probe_ha
  * single-stepped a copy of the instruction.  The address of this
  * copy is p->ainsn.insn.
  */
-static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
-{
-	int ret;
-	unsigned int insn = *p->ainsn.insn;
-
-	regs->nip = (unsigned long)p->addr;
-	ret = emulate_step(regs, insn);
-	if (ret == 0)
-		regs->nip = (unsigned long)p->addr + 4;
-}
-
 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
 {
 	struct kprobe *cur = kprobe_running();
@@ -403,7 +392,8 @@ static int __kprobes post_kprobe_handler
 		cur->post_handler(cur, regs, 0);
 	}
 
-	resume_execution(cur, regs);
+	/* Adjust nip to after the single-stepped instruction */
+	regs->nip = (unsigned long)cur->addr + 4;
 	regs->msr |= kcb->kprobe_saved_msr;
 
 	/*Restore back the original saved kprobes variables and continue. */
#7 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

In reply to Ananth N Mavinakayanahalli
Quoted 13 lines · In message <[email protected]> you wrote:

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

Quoted 7 lines
        case 24:
                rd = (instr >> 21) & 0x1f;
                if (rd != 0)
                        break;
                rb = (instr >> 11) & 0x1f;
                if (rb != 0)
                        break;

Don't we just need rb == rd?

Quoted 37 lines
                imm = (instr & 0xffff);
                if (imm == 0) {
                        regs->nip += 4;
                        return 1;
                }
                break;

But, the following is more straightforward for this case at least.

---
 arch/powerpc/lib/sstep.c |    6 ++++++
 1 file changed, 6 insertions(+)

Index: linux-27may/arch/powerpc/lib/sstep.c
===================================================================

arch/powerpc/lib/sstep.c +6 -0
@@ -412,6 +412,12 @@ int __kprobes emulate_step(struct pt_reg
 	int err;
 	mm_segment_t oldfs;
 
+	/* ori 0,0,0 is a nop. Emulate that too */
+	if (instr == 0x60000000) {
+		regs->nip += 4;
+		return 1;
+	}
+
 	opcode = instr >> 26;
 	switch (opcode) {
 	case 16:	/* bc */

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#8 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

Quoted 24 lines · Michael Neuling
Quoted 13 lines · In message <[email protected]> you wrote:

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

Quoted 7 lines
        case 24:
                rd = (instr >> 21) & 0x1f;
                if (rd != 0)
                        break;
                rb = (instr >> 11) & 0x1f;
                if (rb != 0)
                        break;
Quoted 1 line

Don't we just need rb == rd?

Sure. But for this case, just checking against the opcode seems simple enough.

Ananth

#9 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

Quoted 25 lines · In message <[email protected]> you wrote:
Quoted 24 lines · Michael Neuling
Quoted 20 lines · In message <[email protected]> you wrote:

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

        case 24:
                rd = (instr >> 21) & 0x1f;
                if (rd != 0)
                        break;
                rb = (instr >> 11) & 0x1f;
                if (rb != 0)
                        break;
Quoted 4 lines
Quoted 1 line

Don't we just need rb == rd?

Sure. But for this case, just checking against the opcode seems simple enough.

Simple, sure. You could not emulate anything and remove the code altogether. That would be truly simple. :-P

Why not eliminate as much as possible?

Anyway, sounds like paulus as a better solution.

Mikey

#10 lore raw

Re: [RFC PATCH] powerpc: Emulate nop too

Quoted 28 lines · Michael Neuling
Quoted 25 lines · In message <[email protected]> you wrote:
Quoted 24 lines · Michael Neuling
Quoted 20 lines · In message <[email protected]> you wrote:

Hi Paul,

While we are at it, can we also add nop to the list of emulated instructions?

Ananth
---
From: Ananth N Mavinakayanahalli <[email protected]>

Emulate ori 0,0,0 (nop).

The long winded way is to do:

        case 24:
                rd = (instr >> 21) & 0x1f;
                if (rd != 0)
                        break;
                rb = (instr >> 11) & 0x1f;
                if (rb != 0)
                        break;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Don't we just need rb == rd?

Sure. But for this case, just checking against the opcode seems simple enough.

Simple, sure. You could not emulate anything and remove the code altogether. That would be truly simple. :-P

Ah.. I misunderstood your initial point about rb == rd with imm = 0.

Quoted 3 lines

Why not eliminate as much as possible?

Anyway, sounds like paulus as a better solution.

Right.

Ananth

#11 lore raw
In reply to Paul Mackerras
Quoted 6 lines · Paul Mackerras

This extends the emulate_step() function to handle most of the load and store instructions implemented on current 64-bit server processors. The aim is to handle all the load and store instructions used in the kernel, so this handles the Altivec/VMX lvx and stvx and the VSX lxv2dx and stxv2dx instructions (implemented in POWER7).

Can the emulate_step() function be used on BookIII E processors as well (arch/powerpc/kernel/kprobes.c invokes it irrespective of the host processor though)?

If yes, we can use it with hw_breakpoint_handler() of BookE processors (like what is done on the PPC64 counterpart).

Quoted 10 lines

The new code can emulate user mode instructions, and checks the effective address for a load or store if the saved state is for user mode. It doesn't handle little-endian mode at present.

For floating-point, Altivec/VMX and VSX instructions, it checks that the saved MSR has the enable bit for the relevant facility set, and if so, assumes that the FP/VMX/VSX registers contain valid state, and does loads or stores directly to/from the FP/VMX/VSX registers, using assembly helpers in ldstfp.S.

Thanks,
K.Prasad

#12 lore raw
Quoted 11 lines · K.Prasad
Quoted 6 lines · Paul Mackerras

This extends the emulate_step() function to handle most of the load and store instructions implemented on current 64-bit server processors. The aim is to handle all the load and store instructions used in the kernel, so this handles the Altivec/VMX lvx and stvx and the VSX lxv2dx and stxv2dx instructions (implemented in POWER7).

Can the emulate_step() function be used on BookIII E processors as well (arch/powerpc/kernel/kprobes.c invokes it irrespective of the host processor though)?

I expect it can. I haven't checked what extra things I'd need to add for Book 3E though. I also want to use it on classic 32-bit as well.

Paul.