powerpc: Remove system call emulation
v2 cover powerpc: Remove system call emulation
Since v1, the main change is to use helpers to decode primary/extended opcode and the addition of macros for some of the used opcodes.
- Naveen
Naveen N. Rao (2):
powerpc: Sort and de-dup primary opcodes in ppc-opcode.h
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/ppc-opcode.h | 87 +++++++++++++++------------ arch/powerpc/include/asm/probes.h | 36 +++++++++++ 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 ++-- 7 files changed, 107 insertions(+), 92 deletions(-)
base-commit: e8833c5edc5903f8c8c4fa3dd4f34d6b813c87c8
Signature · 1 line
2.35.1
PATCH v2 1/3 powerpc: Sort and de-dup primary opcodes in ppc-opcode.h
Some of the primary opcodes are duplicated. Remove those, and sort the rest of the primary opcodes to make it easy to read.
--- arch/powerpc/include/asm/ppc-opcode.h | 69 ++++++++++++--------------- 1 file changed, 31 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +31 -38
@@ -127,8 +127,37 @@/* opcode and xopcode for instructions */ -#define OP_TRAP 3 -#define OP_TRAP_64 2 +#define OP_PREFIX 1 +#define OP_TRAP_64 2 +#define OP_TRAP 3 +#define OP_31 31 +#define OP_LWZ 32 +#define OP_LWZU 33 +#define OP_LBZ 34 +#define OP_LBZU 35 +#define OP_STW 36 +#define OP_STWU 37 +#define OP_STB 38 +#define OP_STBU 39 +#define OP_LHZ 40 +#define OP_LHZU 41 +#define OP_LHA 42 +#define OP_LHAU 43 +#define OP_STH 44 +#define OP_STHU 45 +#define OP_LMW 46 +#define OP_STMW 47 +#define OP_LFS 48 +#define OP_LFSU 49 +#define OP_LFD 50 +#define OP_LFDU 51 +#define OP_STFS 52 +#define OP_STFSU 53 +#define OP_STFD 54 +#define OP_STFDU 55 +#define OP_LQ 56 +#define OP_LD 58 +#define OP_STD 62#define OP_31_XOP_TRAP 4#define OP_31_XOP_LDX 21
@@ -208,42 +237,6 @@/* VMX Vector Store Instructions */ #define OP_31_XOP_STVX 231 -/* Prefixed Instructions */ -#define OP_PREFIX 1 - -#define OP_31 31 -#define OP_LWZ 32 -#define OP_STFS 52 -#define OP_STFSU 53 -#define OP_STFD 54 -#define OP_STFDU 55 -#define OP_LD 58 -#define OP_LWZU 33 -#define OP_LBZ 34 -#define OP_LBZU 35 -#define OP_STW 36 -#define OP_STWU 37 -#define OP_STD 62 -#define OP_STB 38 -#define OP_STBU 39 -#define OP_LHZ 40 -#define OP_LHZU 41 -#define OP_LHA 42 -#define OP_LHAU 43 -#define OP_STH 44 -#define OP_STHU 45 -#define OP_LMW 46 -#define OP_STMW 47 -#define OP_LFS 48 -#define OP_LFSU 49 -#define OP_LFD 50 -#define OP_LFDU 51 -#define OP_STFS 52 -#define OP_STFSU 53 -#define OP_STFD 54 -#define OP_STFDU 55 -#define OP_LQ 56 -/* sorted alphabetically */ #define PPC_INST_BCCTR_FLUSH 0x4c400420#define PPC_INST_COPY 0x7c20060c
Signature · 1 line
2.35.1
PATCH v2 2/3 powerpc: Reject probes on instructions that can't be single stepped
In reply to Naveen N. RaoPer 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/ppc-opcode.h | 18 ++++++++++++++ arch/powerpc/include/asm/probes.h | 36 +++++++++++++++++++++++++++ arch/powerpc/kernel/kprobes.c | 4 +-- arch/powerpc/kernel/uprobes.c | 5 ++++ arch/powerpc/xmon/xmon.c | 11 ++++---- 5 files changed, 66 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +18 -0
@@ -130,6 +130,8 @@#define OP_PREFIX 1#define OP_TRAP_64 2#define OP_TRAP 3 +#define OP_SC 17 +#define OP_19 19#define OP_31 31#define OP_LWZ 32#define OP_LWZU 33
@@ -159,6 +161,20 @@#define OP_LD 58#define OP_STD 62 +#define OP_19_XOP_RFID 18 +#define OP_19_XOP_RFMCI 38 +#define OP_19_XOP_RFDI 39 +#define OP_19_XOP_RFI 50 +#define OP_19_XOP_RFCI 51 +#define OP_19_XOP_RFSCV 82 +#define OP_19_XOP_HRFID 274 +#define OP_19_XOP_URFID 306 +#define OP_19_XOP_STOP 370 +#define OP_19_XOP_DOZE 402 +#define OP_19_XOP_NAP 434 +#define OP_19_XOP_SLEEP 466 +#define OP_19_XOP_RVWINKLE 498 +#define OP_31_XOP_TRAP 4#define OP_31_XOP_LDX 21#define OP_31_XOP_LWZX 23
@@ -179,6 +195,8 @@#define OP_31_XOP_LHZUX 311#define OP_31_XOP_MSGSNDP 142#define OP_31_XOP_MSGCLRP 174 +#define OP_31_XOP_MTMSR 146 +#define OP_31_XOP_MTMSRD 178#define OP_31_XOP_TLBIE 306#define OP_31_XOP_MFSPR 339#define OP_31_XOP_LWAX 341
arch/powerpc/include/asm/probes.h +36 -0
@@ -8,6 +8,7 @@* Copyright IBM Corporation, 2012 */ #include <linux/types.h> +#include <asm/disassemble.h> typedef u32 ppc_opcode_t ;#define BREAKPOINT_INSTRUCTION 0x7fe00008/* trap */
@@ -31,6 +32,41 @@ typedef u32 ppc_opcode_t;#define MSR_SINGLESTEP (MSR_SE)#endif +static inline bool can_single_step (u32 inst) +{ +switch (get_op (inst)) { +case OP_TRAP_64 :return false; +case OP_TRAP :return false; +case OP_SC :return false; +case OP_19 : +switch (get_xop (inst)) { +case OP_19_XOP_RFID :return false; +case OP_19_XOP_RFMCI :return false; +case OP_19_XOP_RFDI :return false; +case OP_19_XOP_RFI :return false; +case OP_19_XOP_RFCI :return false; +case OP_19_XOP_RFSCV :return false; +case OP_19_XOP_HRFID :return false; +case OP_19_XOP_URFID :return false; +case OP_19_XOP_STOP :return false; +case OP_19_XOP_DOZE :return false; +case OP_19_XOP_NAP :return false; +case OP_19_XOP_SLEEP :return false; +case OP_19_XOP_RVWINKLE :return false; + } +break ; +case OP_31 : +switch (get_xop (inst)) { +case OP_31_XOP_TRAP :return false; +case OP_31_XOP_TRAP_64 :return false; +case OP_31_XOP_MTMSR :return false; +case OP_31_XOP_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
@@ -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
@@ -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
@@ -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
PATCH v2 3/3 powerpc/64: remove system call instruction emulation
In reply to Naveen N. RaoFrom: Nicholas Piggin <[email protected]>
emulate_step() instruction emulation including sc instruction emulation initially appeared in xmon. It 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 were any in-tree users. 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 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.
Furthermore, it is not possible to single step a system call instruction since it causes an interrupt. As such, a separate patch disables probing on system call instructions.
This patch removes system call emulation and disables stepping system calls.
--- 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
@@ -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
Michael Ellerman
AppliedQuoted 8 lines · Naveen N. Rao
Since v1, the main change is to use helpers to decode primary/extended opcode and the addition of macros for some of the used opcodes.
- Naveen
[...]
Applied to powerpc/next.
[1/3] powerpc: Sort and de-dup primary opcodes in ppc-opcode.h
https://git.kernel.org/powerpc/c/f31c618373f2051a32e30002d8eacad7bbbd3885 [2/3] powerpc: Reject probes on instructions that can't be single stepped https://git.kernel.org/powerpc/c/54cdacd7d3b3c1a8dc10965f56c8b5eb8eda1a33 [3/3] powerpc/64: remove system call instruction emulation https://git.kernel.org/powerpc/c/a553476c44fb6bd3dc3a7e5efef8f130f0f34850
cheers