Skip to contentSkip to conversation

powerpc: Remove system call emulation

PATCH Superseded Naveen N. Rao 4 participants 8 messages Mar 23, 2022 – Mar 30, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover powerpc: Remove system call emulation

This is an update to the series posted by Nick here:
http://lkml.kernel.org/r/[email protected]

The first patch disables probes and breakpoints on instructions that can't be single stepped, including sc and scv. The second patch removes system call emulation for powerpc64.

- Naveen

Naveen N. Rao (1):
powerpc: Reject probes on instructions that can't be single stepped

Nicholas Piggin (1):
powerpc/64: remove system call instruction emulation

 arch/powerpc/include/asm/probes.h  | 55 ++++++++++++++++++++++++++++++
 arch/powerpc/kernel/interrupt_64.S | 10 ------
 arch/powerpc/kernel/kprobes.c      |  4 +--
 arch/powerpc/kernel/uprobes.c      |  5 +++
 arch/powerpc/lib/sstep.c           | 46 ++++++-------------------
 arch/powerpc/xmon/xmon.c           | 11 +++---
 6 files changed, 77 insertions(+), 54 deletions(-)

base-commit: e8833c5edc5903f8c8c4fa3dd4f34d6b813c87c8

Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 powerpc: Reject probes on instructions that can't be single stepped

Per the ISA, a Trace interrupt is not generated for:
- [h|u]rfi[d]
- rfscv
- sc, scv, and Trap instructions that trap
- Power-Saving Mode instructions
- other instructions that cause interrupts (other than Trace interrupts)
- the first instructions of any interrupt handler (applies to Branch and Single Step tracing; CIABR matches may still occur)
- instructions that are emulated by software

Add a helper to check for instructions belonging to the first four categories above and to reject kprobes, uprobes and xmon breakpoints on such instructions. We reject probing on instructions belonging to these categories across all ISA versions and across both BookS and BookE.

For trap instructions, we can't know in advance if they can cause a trap, and there is no good reason to allow probing on those. Also, uprobes already refuses to probe trap instructions and kprobes does not allow probes on trap instructions used for kernel warnings and bugs. As such, stop allowing any type of probes/breakpoints on trap instruction across uprobes, kprobes and xmon.

For some of the fp/altivec instructions that can generate an interrupt and which we emulate in the kernel (altivec assist, for example), we check and turn off single stepping in emulate_single_step().

Instructions generating a DSI are restarted and single stepping normally completes once the instruction is completed.

In uprobes, if a single stepped instruction results in a non-fatal signal to be delivered to the task, such signals are "delayed" until after the instruction completes. For fatal signals, single stepping is cancelled and the instruction restarted in-place so that core dump captures proper addresses.

In kprobes, we do not allow probes on instructions having an extable entry and we also do not allow probing interrupt vectors.

---
 arch/powerpc/include/asm/probes.h | 55 +++++++++++++++++++++++++++++++
 arch/powerpc/kernel/kprobes.c     |  4 +--
 arch/powerpc/kernel/uprobes.c     |  5 +++
 arch/powerpc/xmon/xmon.c          | 11 +++----
 4 files changed, 67 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/probes.h +55 -0
index c5d984700d241a..21ab5b6256b590 100644
@@ -31,6 +31,61 @@ typedef u32 ppc_opcode_t;
 #define MSR_SINGLESTEP	(MSR_SE)
 #endif
 
+static inline bool can_single_step(u32 inst)
+{
+	switch (inst >> 26) {
+	case 2:		/* tdi */
+		return false;
+	case 3:		/* twi */
+		return false;
+	case 17:	/* sc and scv */
+		return false;
+	case 19:
+		switch ((inst >> 1) & 0x3ff) {
+		case 18:	/* rfid */
+			return false;
+		case 38:	/* rfmci */
+			return false;
+		case 39:	/* rfdi */
+			return false;
+		case 50:	/* rfi */
+			return false;
+		case 51:	/* rfci */
+			return false;
+		case 82:	/* rfscv */
+			return false;
+		case 274:	/* hrfid */
+			return false;
+		case 306:	/* urfid */
+			return false;
+		case 370:	/* stop */
+			return false;
+		case 402:	/* doze */
+			return false;
+		case 434:	/* nap */
+			return false;
+		case 466:	/* sleep */
+			return false;
+		case 498:	/* rvwinkle */
+			return false;
+		}
+		break;
+	case 31:
+		switch ((inst >> 1) & 0x3ff) {
+		case 4:		/* tw */
+			return false;
+		case 68:	/* td */
+			return false;
+		case 146:	/* mtmsr */
+			return false;
+		case 178:	/* mtmsrd */
+			return false;
+		}
+		break;
+	}
+	return true;
+}
+
 /* Enable single stepping for the current task */
 static inline void enable_single_step(struct pt_regs *regs)
 {
arch/powerpc/kernel/kprobes.c +2 -2
index 9a492fdec1dfbe..0936a6c8c256b9 100644
@@ -129,8 +129,8 @@ int arch_prepare_kprobe(struct kprobe *p)
 	if ((unsigned long)p->addr & 0x03) {
 		printk("Attempt to register kprobe at an unaligned address\n");
 		ret = -EINVAL;
-	} else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
-		printk("Cannot register a kprobe on mtmsr[d]/rfi[d]\n");
+	} else if (!can_single_step(ppc_inst_val(insn))) {
+		printk("Cannot register a kprobe on instructions that can't be single stepped\n");
 		ret = -EINVAL;
 	} else if ((unsigned long)p->addr & ~PAGE_MASK &&
 		   ppc_inst_prefixed(ppc_inst_read(p->addr - 1))) {
arch/powerpc/kernel/uprobes.c +5 -0
index c6975467d9ffdc..95a41ae9dfa755 100644
@@ -48,6 +48,11 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 		return -EINVAL;
 	}
 
+	if (!can_single_step(ppc_inst_val(ppc_inst_read(auprobe->insn)))) {
+		pr_info_ratelimited("Cannot register a uprobe on instructions that can't be single stepped\n");
+		return -ENOTSUPP;
+	}
+
 	return 0;
 }
 
arch/powerpc/xmon/xmon.c +5 -6
index fd72753e8ad502..a92c5739d954e2 100644
@@ -921,9 +921,9 @@ static void insert_bpts(void)
 			bp->enabled = 0;
 			continue;
 		}
-		if (IS_MTMSRD(instr) || IS_RFID(instr)) {
-			printf("Breakpoint at %lx is on an mtmsrd or rfid "
-			       "instruction, disabling it\n", bp->address);
+		if (!can_single_step(ppc_inst_val(instr))) {
+			printf("Breakpoint at %lx is on an instruction that can't be single stepped, disabling it\n",
+					bp->address);
 			bp->enabled = 0;
 			continue;
 		}
@@ -1470,9 +1470,8 @@ static long check_bp_loc(unsigned long addr)
 		printf("Can't read instruction at address %lx\n", addr);
 		return 0;
 	}
-	if (IS_MTMSRD(instr) || IS_RFID(instr)) {
-		printf("Breakpoints may not be placed on mtmsrd or rfid "
-		       "instructions\n");
+	if (!can_single_step(ppc_inst_val(instr))) {
+		printf("Breakpoints may not be placed on instructions that can't be single stepped\n");
 		return 0;
 	}
 	return 1;
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 1/2] powerpc: Reject probes on instructions that can't be single stepped

Hi, Naveen.

Some comments below.

Quoted 51 lines · Naveen N. Rao

Per the ISA, a Trace interrupt is not generated for:
- [h|u]rfi[d]
- rfscv
- sc, scv, and Trap instructions that trap
- Power-Saving Mode instructions
- other instructions that cause interrupts (other than Trace interrupts)
- the first instructions of any interrupt handler (applies to Branch and Single Step tracing; CIABR matches may still occur)
- instructions that are emulated by software

Add a helper to check for instructions belonging to the first four categories above and to reject kprobes, uprobes and xmon breakpoints on such instructions. We reject probing on instructions belonging to these categories across all ISA versions and across both BookS and BookE.

For trap instructions, we can't know in advance if they can cause a trap, and there is no good reason to allow probing on those. Also, uprobes already refuses to probe trap instructions and kprobes does not allow probes on trap instructions used for kernel warnings and bugs. As such, stop allowing any type of probes/breakpoints on trap instruction across uprobes, kprobes and xmon.

For some of the fp/altivec instructions that can generate an interrupt and which we emulate in the kernel (altivec assist, for example), we check and turn off single stepping in emulate_single_step().

Instructions generating a DSI are restarted and single stepping normally completes once the instruction is completed.

In uprobes, if a single stepped instruction results in a non-fatal signal to be delivered to the task, such signals are "delayed" until after the instruction completes. For fatal signals, single stepping is cancelled and the instruction restarted in-place so that core dump captures proper addresses.

In kprobes, we do not allow probes on instructions having an extable entry and we also do not allow probing interrupt vectors.

---
  arch/powerpc/include/asm/probes.h | 55 +++++++++++++++++++++++++++++++
  arch/powerpc/kernel/kprobes.c     |  4 +--
  arch/powerpc/kernel/uprobes.c     |  5 +++
  arch/powerpc/xmon/xmon.c          | 11 +++----
  4 files changed, 67 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/probes.h +0 -0
index c5d984700d241a..21ab5b6256b590 100644

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 36

Quoted 6 lines
arch/powerpc/include/asm/probes.h +3 -0
@@ -31,6 +31,61 @@ typedef u32 ppc_opcode_t;
  #define MSR_SINGLESTEP	(MSR_SE)
  #endif
 
+static inline bool can_single_step(u32 inst)
+{
+	switch (inst >> 26) {

Can't ppc_inst_primary_opcode() be used instead?

Quoted 42 lines
arch/powerpc/include/asm/probes.h +42 -0
+	case 2:		/* tdi */
+		return false;
+	case 3:		/* twi */
+		return false;
+	case 17:	/* sc and scv */
+		return false;
+	case 19:
+		switch ((inst >> 1) & 0x3ff) {
+		case 18:	/* rfid */
+			return false;
+		case 38:	/* rfmci */
+			return false;
+		case 39:	/* rfdi */
+			return false;
+		case 50:	/* rfi */
+			return false;
+		case 51:	/* rfci */
+			return false;
+		case 82:	/* rfscv */
+			return false;
+		case 274:	/* hrfid */
+			return false;
+		case 306:	/* urfid */
+			return false;
+		case 370:	/* stop */
+			return false;
+		case 402:	/* doze */
+			return false;
+		case 434:	/* nap */
+			return false;
+		case 466:	/* sleep */
+			return false;
+		case 498:	/* rvwinkle */
+			return false;
+		}
+		break;
+	case 31:
+		switch ((inst >> 1) & 0x3ff) {
+		case 4:		/* tw */
+			return false;
+		case 68:	/* td */
+			return false;

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 88

Quoted 10 lines
arch/powerpc/include/asm/probes.h +10 -0
+		case 146:	/* mtmsr */
+			return false;
+		case 178:	/* mtmsrd */
+			return false;
+		}
+		break;
+	}
+	return true;
+}
+

Can't OP_* definitions from ppc-opcode.h be used for all of these switch-case statements?

Quoted 53 lines
arch/powerpc/include/asm/probes.h +0 -0
  /* Enable single stepping for the current task */
  static inline void enable_single_step(struct pt_regs *regs)
  {
arch/powerpc/kernel/kprobes.c +2 -2
index 9a492fdec1dfbe..0936a6c8c256b9 100644
@@ -129,8 +129,8 @@ int arch_prepare_kprobe(struct kprobe *p)
  	if ((unsigned long)p->addr & 0x03) {
  		printk("Attempt to register kprobe at an unaligned address\n");
  		ret = -EINVAL;
-	} else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
-		printk("Cannot register a kprobe on mtmsr[d]/rfi[d]\n");
+	} else if (!can_single_step(ppc_inst_val(insn))) {
+		printk("Cannot register a kprobe on instructions that can't be single stepped\n");
  		ret = -EINVAL;
  	} else if ((unsigned long)p->addr & ~PAGE_MASK &&
  		   ppc_inst_prefixed(ppc_inst_read(p->addr - 1))) {
arch/powerpc/kernel/uprobes.c +5 -0
index c6975467d9ffdc..95a41ae9dfa755 100644
@@ -48,6 +48,11 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
  		return -EINVAL;
  	}
 
+	if (!can_single_step(ppc_inst_val(ppc_inst_read(auprobe->insn)))) {
+		pr_info_ratelimited("Cannot register a uprobe on instructions that can't be single stepped\n");
+		return -ENOTSUPP;
+	}
+
  	return 0;
  }
 
arch/powerpc/xmon/xmon.c +3 -3
index fd72753e8ad502..a92c5739d954e2 100644
@@ -921,9 +921,9 @@ static void insert_bpts(void)
  			bp->enabled = 0;
  			continue;
  		}
-		if (IS_MTMSRD(instr) || IS_RFID(instr)) {
-			printf("Breakpoint at %lx is on an mtmsrd or rfid "
-			       "instruction, disabling it\n", bp->address);
+		if (!can_single_step(ppc_inst_val(instr))) {
+			printf("Breakpoint at %lx is on an instruction that can't be single stepped, disabling it\n",
+					bp->address);
  			bp->enabled = 0;
  			continue;
  		}
@@ -1470,9 +1470,8 @@ static long check_bp_loc(unsigned long addr)
  		printf("Can't read instruction at address %lx\n", addr);

arch/powerpc/xmon/xmon.c static long check_bp_loc(unsigned long addr) · line 1477

Quoted 10 lines
arch/powerpc/xmon/xmon.c +2 -3
  		return 0;
  	}
-	if (IS_MTMSRD(instr) || IS_RFID(instr)) {
-		printf("Breakpoints may not be placed on mtmsrd or rfid "
-		       "instructions\n");
+	if (!can_single_step(ppc_inst_val(instr))) {
+		printf("Breakpoints may not be placed on instructions that can't be single stepped\n");
  		return 0;
  	}
  	return 1;

Cheers!

Signature · 1 line
Murilo
#3 lore raw

Re: [PATCH 1/2] powerpc: Reject probes on instructions that can't be single stepped

Quoted 1 line · Murilo Opsfelder Araújo

On 3/23/22 08:51, Naveen N. Rao wrote:

...

Quoted 10 lines
Quoted 10 lines
arch/powerpc/include/asm/probes.h +10 -0
+	case 31:
+		switch ((inst >> 1) & 0x3ff) {
+		case 4:		/* tw */
+			return false;
+		case 68:	/* td */
+			return false;
+		case 146:	/* mtmsr */
+			return false;
+		case 178:	/* mtmsrd */
+			return false;
Quoted 8 lines

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 88

Quoted 6 lines
arch/powerpc/include/asm/probes.h +6 -0
+		}
+		break;
+	}
+	return true;
+}
+

Can't OP_* definitions from ppc-opcode.h be used for all of these switch-case statements?

Yes please. And add any that are missing.

cheers

#4 lore raw

Re: [PATCH 1/2] powerpc: Reject probes on instructions that can't be single stepped

Quoted 7 lines · Michael Ellerman
Quoted 6 lines · Murilo Opsfelder Araújo

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 36

Quoted 3 lines · Naveen N. Rao
arch/powerpc/include/asm/probes.h +3 -0
+static inline bool can_single_step(u32 inst)
+{
+	switch (inst >> 26) {

Can't ppc_inst_primary_opcode() be used instead?

I didn't want to add a dependency on inst.h. But I guess I can very well move this out of the header into some .c file. I will see if I can make that work.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
arch/powerpc/include/asm/probes.h +10 -0
+	case 31:
+		switch ((inst >> 1) & 0x3ff) {
+		case 4:		/* tw */
+			return false;
+		case 68:	/* td */
+			return false;
+		case 146:	/* mtmsr */
+			return false;
+		case 178:	/* mtmsrd */
+			return false;
Quoted 8 lines

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 88

Quoted 6 lines
arch/powerpc/include/asm/probes.h +6 -0
+		}
+		break;
+	}
+	return true;
+}
+

Can't OP_* definitions from ppc-opcode.h be used for all of these switch-case statements?

Quoted 1 line

Yes please. And add any that are missing.

We only have OP_31 from the above list now. I'll add the rest.

Thanks,
Naveen

#5 lore raw

Re: [PATCH 1/2] powerpc: Reject probes on instructions that can't be single stepped

Le 28/03/2022 à 19:20, Naveen N. Rao a écrit :

Quoted 9 lines
Quoted 7 lines · Michael Ellerman
Quoted 6 lines · Murilo Opsfelder Araújo

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 36

Quoted 3 lines · Naveen N. Rao
arch/powerpc/include/asm/probes.h +3 -0
+static inline bool can_single_step(u32 inst)
+{
+    switch (inst >> 26) {

Can't ppc_inst_primary_opcode() be used instead?

Quoted 3 lines

I didn't want to add a dependency on inst.h. But I guess I can very well move this out of the header into some .c file. I will see if I can make that work.

Maybe use get_op() from asm/disassemble.h ?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+    case 31:
+        switch ((inst >> 1) & 0x3ff) {

For that one you have get_xop() in asm/disassemble.h

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+        case 4:        /* tw */

OP_31_XOP_TRAP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+            return false;
+        case 68:    /* td */

OP_31_XOP_TRAP_64

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 11 lines

+            return false;
+        case 146:    /* mtmsr */
+            return false;
+        case 178:    /* mtmsrd */
+            return false;
+        }
+        break;
+    }
+    return true;

(quoted diff) +2 -0
+}
+

Can't OP_* definitions from ppc-opcode.h be used for all of these switch-case statements?

Quoted 3 lines
Quoted 1 line

Yes please. And add any that are missing.

We only have OP_31 from the above list now. I'll add the rest.

Isn't there also OP_TRAP and OP_TRAP_64 ?

Christophe

#6 lore raw

Re: [PATCH 1/2] powerpc: Reject probes on instructions that can't be single stepped

Quoted 14 lines · Christophe Leroy

Le 28/03/2022 à 19:20, Naveen N. Rao a écrit :

Quoted 10 lines
Quoted 7 lines · Michael Ellerman
Quoted 6 lines · Murilo Opsfelder Araújo

arch/powerpc/include/asm/probes.h typedef u32 ppc_opcode_t; · line 36

Quoted 3 lines · Naveen N. Rao
arch/powerpc/include/asm/probes.h +3 -0
+static inline bool can_single_step(u32 inst)
+{
+    switch (inst >> 26) {

Can't ppc_inst_primary_opcode() be used instead?

I didn't want to add a dependency on inst.h. But I guess I can very well move this out of the header into some .c file. I will see if I can make that work.

Quoted 7 lines

Maybe use get_op() from asm/disassemble.h ?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+    case 31:
+        switch ((inst >> 1) & 0x3ff) {

For that one you have get_xop() in asm/disassemble.h

Nice! I will use those.

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+        case 4:        /* tw */

OP_31_XOP_TRAP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+            return false;
+        case 68:    /* td */

OP_31_XOP_TRAP_64

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

Review

Quoted 11 lines

+            return false;
+        case 146:    /* mtmsr */
+            return false;
+        case 178:    /* mtmsrd */
+            return false;
+        }
+        break;
+    }
+    return true;

(quoted diff) +2 -0
+}
+

Can't OP_* definitions from ppc-opcode.h be used for all of these switch-case statements?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes please. And add any that are missing.

We only have OP_31 from the above list now. I'll add the rest.

Isn't there also OP_TRAP and OP_TRAP_64 ?

Ah, the list clearly isn't sorted, and there are some duplicates there :)

Thanks,
Naveen

#7 lore raw

PATCH 2/2 powerpc/64: remove system call instruction emulation

In reply to Naveen N. Rao

From: Nicholas Piggin <[email protected]>

emulate_step instruction emulation including sc instruction emulation initially appeared in xmon. It then emulation code was then moved into sstep.c where kprobes could use it too, and later hw_breakpoint and uprobes started to use it.

Until uprobes, the only instruction emulation users were for kernel mode instructions.

- xmon only steps / breaks on kernel addresses.
- kprobes is kernel only.
- hw_breakpoint only emulates kernel instructions, single steps user.

At one point there was support for the kernel to execute sc instructions, although that is long removed and it's not clear whether there was any in-tree code. So system call emulation is not required by the above users.

uprobes uses emulate_step and it appears possible to emulate sc instruction in userspace. Userspace system call emulation is broken and it's not clear it ever worked well.

The big complication is that userspace takes an interrupt to the kernel to emulate the instruction. The user->kernel interrupt sets up registers and interrupt stack frame expecting to return to userspace, then system call instruction emulation re-directs that stack frame to the kernel, early in the system call interrupt handler. This means the the interrupt return code takes the kernel->kernel restore path, which does not restore everything as the system call interrupt handler would expect coming from userspace. regs->iamr appears to get lost for example, because the kernel->kernel return does not restore the user iamr. Accounting such as irqflags tracing and CPU accounting does not get flipped back to user mode as the system call handler expects, so those appear to enter the kernel twice without returning to userspace.

These things may be individually fixable with various complication, but it is a big complexity for unclear real benefit.

This patch removes system call emulation and disables stepping system calls (because they don't work with trace interrupts, as commented).

[also get rid of '#ifdef CONFIG_PPC64']
---
 arch/powerpc/kernel/interrupt_64.S | 10 -------
 arch/powerpc/lib/sstep.c           | 46 +++++++-----------------------
 2 files changed, 10 insertions(+), 46 deletions(-)
arch/powerpc/kernel/interrupt_64.S +0 -10
index 7bab2d7de372e0..6471034c790973 100644
@@ -219,16 +219,6 @@ system_call_vectored common 0x3000
  */
 system_call_vectored sigill 0x7ff0
 
-
-/*
- * Entered via kernel return set up by kernel/sstep.c, must match entry regs
- */
-	.globl system_call_vectored_emulate
-system_call_vectored_emulate:
-_ASM_NOKPROBE_SYMBOL(system_call_vectored_emulate)
-	li	r10,IRQS_ALL_DISABLED
-	stb	r10,PACAIRQSOFTMASK(r13)
-	b	system_call_vectored_common
 #endif /* CONFIG_PPC_BOOK3S */
 
 	.balign IFETCH_ALIGN_BYTES
arch/powerpc/lib/sstep.c +10 -36

76 lines not on this page · raw message

Signature · 1 line
2.35.1