powerpc/64s: scv support
Subsystems (3)
Nicholas Piggin
reviewercover powerpc/64s: scv support
Another round of scv, which is getting closer to done. ABI and compatibility / feature testing still not set in stone, but some good discussion among the various libcs etc. and it's close enough that changes should just be small tweaks to clobbers etc. Posting now because there is some interest to prototype userspace support which we should do before fixing the ABI.
This relies on some of the signal handling and kuap patches I already posted, so tree is here:
https://github.com/npiggin/linux/commits/next-test
I have qemu scv support apatches I need to resend, but they're not merged yet. POWER9 system simulator should support it, but I have not tested the public version:
https://www14.software.ibm.com/support/customercare/sas/f/pwrfs/pwr9/home.html
Thanks,
Nick
Nicholas Piggin (2):
powerpc/64s/exception: treat NIA below __end_interrupts as soft-masked powerpc/64s: system call support for scv/rfscv instructions
Documentation/powerpc/syscall64-abi.rst | 42 ++++-- arch/powerpc/include/asm/asm-prototypes.h | 2 +- arch/powerpc/include/asm/exception-64s.h | 6 + arch/powerpc/include/asm/head-64.h | 2 +- arch/powerpc/include/asm/ppc-opcode.h | 2 + arch/powerpc/include/asm/ppc_asm.h | 2 + arch/powerpc/include/asm/processor.h | 2 +- arch/powerpc/include/asm/ptrace.h | 8 +- arch/powerpc/include/asm/setup.h | 4 +- arch/powerpc/include/asm/sstep.h | 1 + arch/powerpc/include/asm/vdso.h | 1 + arch/powerpc/kernel/cpu_setup_power.S | 2 +- arch/powerpc/kernel/cputable.c | 3 +- arch/powerpc/kernel/dt_cpu_ftrs.c | 1 + arch/powerpc/kernel/entry_64.S | 158 +++++++++++++++++++++- arch/powerpc/kernel/exceptions-64s.S | 150 +++++++++++++++++++- arch/powerpc/kernel/process.c | 10 +- arch/powerpc/kernel/setup_64.c | 5 +- arch/powerpc/kernel/signal.c | 19 ++- arch/powerpc/kernel/signal_64.c | 28 +++- arch/powerpc/kernel/syscall_64.c | 32 +++-- arch/powerpc/kernel/vdso.c | 2 + arch/powerpc/kernel/vdso64/sigtramp.S | 34 ++++- arch/powerpc/kernel/vdso64/vdso64.lds.S | 1 + arch/powerpc/lib/sstep.c | 14 ++ arch/powerpc/perf/callchain_64.c | 9 +- arch/powerpc/platforms/pseries/setup.c | 8 +- arch/powerpc/xmon/xmon.c | 1 + 28 files changed, 492 insertions(+), 57 deletions(-)
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerRFC PATCH 1/2 powerpc/64s/exception: treat NIA below __end_interrupts as soft-masked
The scv instruction causes an interrupt which can enter the kernel with MSR[EE]=1, thus allowing interrupts to hit at any time. These must not be taken as normal interrupts, because they come from MSR[PR]=0 context, and yet the kernel stack is not yet set up and r13 is not set to the PACA).
Treat this as a soft-masked interrupt regardless of the soft masked state. This does not affect behaviour yet, because currently all interrupts are taken with MSR[EE]=0.
--- arch/powerpc/kernel/exceptions-64s.S | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +24 -3
@@ -508,8 +508,24 @@ DEFINE_FIXED_SYMBOL(\name\()_common_real) .macro __GEN_COMMON_BODY name .if IMASK + .if ! ISTACK + .error "No support for masked interrupt to use custom stack" + .endif + + /* If coming from user, skip soft-mask tests. */ + andi. r10,r12,MSR_PR + bne 2f + + /* Kernel code running below __end_interrupts is implicitly + * soft-masked */ + LOAD_HANDLER(r10, __end_interrupts) + cmpd r11,r10 + li r10,IMASK + blt- 1f + + /* Test the soft mask state against our interrupt's bit */ lbz r10,PACAIRQSOFTMASK(r13) - andi. r10,r10,IMASK +1: andi. r10,r10,IMASK /* Associate vector numbers with bits in paca->irq_happened */ .if IVEC == 0x500 || IVEC == 0xea0 li r10,PACA_IRQ_EE
@@ -540,7 +556,7 @@ DEFINE_FIXED_SYMBOL(\name\()_common_real) .if ISTACK andi. r10,r12,MSR_PR /* See if coming from user */ - mr r10,r1 /* Save r1 */ +2: mr r10,r1 /* Save r1 */ subi r1,r1,INT_FRAME_SIZE /* alloc frame on kernel stack */ beq- 100f ld r1,PACAKSAVE(r13) /* kernel stack to use */
@@ -2811,7 +2827,8 @@ masked_interrupt: ld r10,PACA_EXGEN+EX_R10(r13) ld r11,PACA_EXGEN+EX_R11(r13) ld r12,PACA_EXGEN+EX_R12(r13) - /* returns to kernel where r13 must be set up, so don't restore it */ + ld r13,PACA_EXGEN+EX_R13(r13) + /* May return to masked low address where r13 is not set up */ .if \hsrr HRFI_TO_KERNEL .else
@@ -2970,6 +2987,10 @@ EXC_COMMON_BEGIN(ppc64_runlatch_on_trampoline) USE_FIXED_SECTION(virt_trampolines) /* + * All code below __end_interrupts is treated as soft-masked. If + * any code runs here with MSR[EE]=1, it must then cope with pending + * soft interrupt being raised (i.e., by ensuring it is replayed). + * * The __end_interrupts marker must be past the out-of-line (OOL) * handlers, so that they are copied to real address 0x100 when running * a relocatable kernel. This ensures they can be reached from the short
Signature · 1 line
2.23.0
Nicholas Piggin
reviewerRFC PATCH 2/2 powerpc/64s: system call support for scv/rfscv instructions
In reply to Nicholas PigginAdd support for the scv instruction on POWER9 and later CPUs.
For now this implements the zeroth scv vector 'scv 0', as identical to 'sc' system calls, with the exception that lr is not preserved, and it is 64-bit only. There may yet be changes made to this ABI, so it's for testing only.
rfscv is implemented to return from scv type system calls. It can not be used to return from sc system calls because those are defined to preserve lr.
In a comparison of getpid syscall, the test program had scv taking about 3 more cycles in user mode (92 vs 89 for sc), due to lr handling. getpid syscall throughput on POWER9 is improved by 33%, mostly due to reducing mtmsr and mtspr.
--- Documentation/powerpc/syscall64-abi.rst | 42 ++++-- arch/powerpc/include/asm/asm-prototypes.h | 2 +- arch/powerpc/include/asm/exception-64s.h | 6 + arch/powerpc/include/asm/head-64.h | 2 +- arch/powerpc/include/asm/ppc-opcode.h | 2 + arch/powerpc/include/asm/ppc_asm.h | 2 + arch/powerpc/include/asm/processor.h | 2 +- arch/powerpc/include/asm/ptrace.h | 8 +- arch/powerpc/include/asm/setup.h | 4 +- arch/powerpc/include/asm/sstep.h | 1 + arch/powerpc/include/asm/vdso.h | 1 + arch/powerpc/kernel/cpu_setup_power.S | 2 +- arch/powerpc/kernel/cputable.c | 3 +- arch/powerpc/kernel/dt_cpu_ftrs.c | 1 + arch/powerpc/kernel/entry_64.S | 158 +++++++++++++++++++++- arch/powerpc/kernel/exceptions-64s.S | 123 ++++++++++++++++- arch/powerpc/kernel/process.c | 10 +- arch/powerpc/kernel/setup_64.c | 5 +- arch/powerpc/kernel/signal.c | 19 ++- arch/powerpc/kernel/signal_64.c | 28 +++- arch/powerpc/kernel/syscall_64.c | 32 +++-- arch/powerpc/kernel/vdso.c | 2 + arch/powerpc/kernel/vdso64/sigtramp.S | 34 ++++- arch/powerpc/kernel/vdso64/vdso64.lds.S | 1 + arch/powerpc/lib/sstep.c | 14 ++ arch/powerpc/perf/callchain_64.c | 9 +- arch/powerpc/platforms/pseries/setup.c | 8 +- arch/powerpc/xmon/xmon.c | 1 + 28 files changed, 468 insertions(+), 54 deletions(-)
Documentation/powerpc/syscall64-abi.rst +30 -12
@@ -5,6 +5,15 @@ Power Architecture 64-bit Linux system call ABI syscall ======= +Invocation +---------- +The syscall is made with the sc instruction, and returns with execution +continuing at the instruction following the sc instruction. + +If PPC_FEATURE2_SCV appears in the AT_HWCAP2 ELF auxiliary vector, the +scv 0 instruction is an alternative that may provide better performance, +with some differences to calling sequence. + syscall calling sequence\ [1]_ matches the Power Architecture 64-bit ELF ABI specification C function calling sequence, including register preservation rules, with the following differences.
@@ -12,16 +21,23 @@ rules, with the following differences. .. [1] Some syscalls (typically low-level management functions) may have different calling sequences (e.g., rt_sigreturn). -Parameters and return value ---------------------------- +Parameters +---------- The system call number is specified in r0. There is a maximum of 6 integer parameters to a syscall, passed in r3-r8. -Both a return value and a return error code are returned. cr0.SO is the return -error code, and r3 is the return value or error code. When cr0.SO is clear, -the syscall succeeded and r3 is the return value. When cr0.SO is set, the -syscall failed and r3 is the error code that generally corresponds to errno. +Return value +------------ +- For the sc instruction, both a return value and a return error code are + returned. cr0.SO is the return error code, and r3 is the return value or + error code. When cr0.SO is clear, the syscall succeeded and r3 is the return + value. When cr0.SO is set, the syscall failed and r3 is the error code that + generally corresponds to errno. + +- For the scv 0 instruction, there is a return value indicates failure if it + is >= -MAX_ERRNO (-4095) as an unsigned comparison, in which case it is the + negated return error code. Otherwise it is the successful return value. Stack -----
@@ -34,22 +50,23 @@ Register preservation rules match the ELF ABI calling sequence with the following differences: =========== ============= ======================================== +--- For the sc instruction --- r0 Volatile (System call number.) r3 Volatile (Parameter 1, and return value.) r4-r8 Volatile (Parameters 2-6.) -cr0 Volatile (cr0.SO is the return error condition) +cr0 Volatile (cr0.SO is the return error condition.) cr1, cr5-7 Nonvolatile lr Nonvolatile + +--- For the scv 0 instruction --- +r0 Volatile (System call number.) +r3 Volatile (Parameter 1, and return value.) +r4-r8 Volatile (Parameters 2-6.) =========== ============= ======================================== All floating point and vector data registers as well as control and status registers are nonvolatile. -Invocation ----------- -The syscall is performed with the sc instruction, and returns with execution -continuing at the instruction following the sc instruction. - Transactional Memory -------------------- Syscall behavior can change if the processor is in transactional or suspended
@@ -75,6 +92,7 @@ auxiliary vector. returning to the caller. This case is not well defined or supported, so this behavior should not be relied upon. +scv 0 syscalls will always behave as PPC_FEATURE2_HTM_NOSC. vsyscall ========
arch/powerpc/include/asm/asm-prototypes.h +1 -1
@@ -98,7 +98,7 @@ unsigned long __init early_init(unsigned long dt_ptr);void __init machine_init (u64 dt_ptr);#endif long system_call_exception (long r3,long r4,long r5,long r6,long r7,long r8,unsigned long r0,struct pt_regs *regs); -notrace unsigned long syscall_exit_prepare (unsigned long r3,struct pt_regs *regs); +notrace unsigned long syscall_exit_prepare (unsigned long r3,struct pt_regs *regs,long scv);notrace unsigned long interrupt_exit_user_prepare (struct pt_regs *regs,unsigned long msr);notrace unsigned long interrupt_exit_kernel_prepare (struct pt_regs *regs,unsigned long msr);
arch/powerpc/include/asm/exception-64s.h +6 -0
@@ -123,6 +123,12 @@hrfid ; \ b hrfi_flush_fallback +#define RFSCV_TO_USER \ + STF_EXIT_BARRIER_SLOT; \ + RFI_FLUSH_SLOT; \ + RFSCV; \ + b rfscv_flush_fallback +#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_EXCEPTION_H */
arch/powerpc/include/asm/head-64.h +1 -1
@@ -128,7 +128,7 @@ end_##sname: .if ((start) % (size) != 0); \ .error "Fixed section exception vector misalignment"; \ .endif; \ - .if ((size) != 0x20) && ((size) != 0x80) && ((size) != 0x100); \ + .if ((size) != 0x20) && ((size) != 0x80) && ((size) != 0x100) && ((size) != 0x1000); \ .error "Fixed section exception vector bad size"; \ .endif; \ .if (start) < sname##_start; \
arch/powerpc/include/asm/ppc-opcode.h +2 -0
@@ -254,6 +254,7 @@#define PPC_INST_MFVSRD 0x7c000066#define PPC_INST_MTVSRD 0x7c000166#define PPC_INST_SC 0x44000002 +#define PPC_INST_SCV 0x44000001#define PPC_INST_SLBFEE 0x7c0007a7#define PPC_INST_SLBIA 0x7c0003e4
@@ -409,6 +410,7 @@#define __PPC_CT (t) (((t) & 0x0f) << 21)#define __PPC_SPR (r) ((((r) & 0x1f) << 16) | ((((r) >> 5) & 0x1f) << 11))#define __PPC_RC21 (0x1 << 10) +#define __PPC_LEV (l) (((l) & 0x7f) << 5)/* * Both low and high 16 bits are added as SIGNED additions, so if low 16 bits
arch/powerpc/include/asm/ppc_asm.h +2 -0
@@ -755,6 +755,8 @@ END_FTR_SECTION_NESTED(CPU_FTR_CELL_TB_BUG, CPU_FTR_CELL_TB_BUG, 96)#define N_SLINE 68#define N_SO 100 +#define RFSCV .long 0x4c0000a4 +/* * Create an endian fixup trampoline *
arch/powerpc/include/asm/processor.h +1 -1
@@ -304,7 +304,7 @@ struct thread_struct { .regs = (struct pt_regs *)INIT_SP - 1, /* XXX bogus, I think */ \ .addr_limit = KERNEL_DS, \ .fpexc_mode = 0, \ - .fscr = FSCR_TAR | FSCR_EBB \ + .fscr = FSCR_TAR | FSCR_EBB | FSCR_SCV \ }#endif
arch/powerpc/include/asm/ptrace.h +6 -2
@@ -180,19 +180,21 @@ extern int ptrace_put_reg(struct task_struct *task, int regno,#define current_pt_regs () \ ((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1) -#define TRAP_IS_SYSCALL (regs) (TRAP(regs) == 0xc00) -#ifdef __powerpc64__#ifdef CONFIG_PPC_BOOK3S #define TRAP (regs) ((regs)->trap & ~0x10)#define SET_TRAP (regs, val) ((regs)->trap = ((regs)->trap & 0x10) | ((val) & ~0x10))#define FULL_REGS (regs) true#define SET_FULL_REGS (regs) do { } while (0) +#define TRAP_IS_SYSCALL (regs) (TRAP(regs) == 0xc00 || TRAP(regs) == 0x3000) +#define TRAP_IS_SCV (regs) (TRAP(regs) == 0x3000)#else #define TRAP (regs) ((regs)->trap & ~0x11)#define SET_TRAP (regs, val) ((regs)->trap = ((regs)->trap & 0x11) | ((val) & ~0x11))#define FULL_REGS (regs) (((regs)->trap & 1) == 0)#define SET_FULL_REGS (regs) ((regs)->trap |= 1) +#define TRAP_IS_SYSCALL (regs) (TRAP(regs) == 0xc00) +#define TRAP_IS_SCV (regs) false#endif #define CHECK_FULL_REGS (regs) BUG_ON(!FULL_REGS(regs))#define NV_REG_POISON 0xdeadbeefdeadbeefUL
@@ -211,6 +213,8 @@ extern int ptrace_put_reg(struct task_struct *task, int regno,#define IS_CRITICAL_EXC (regs) (((regs)->trap & 2) != 0)#define IS_MCHECK_EXC (regs) (((regs)->trap & 4) != 0)#define IS_DEBUG_EXC (regs) (((regs)->trap & 8) != 0) +#define TRAP_IS_SYSCALL (regs) (TRAP(regs) == 0xc00) +#define TRAP_IS_SCV (regs) false#define NV_REG_POISON 0xdeadbeef#define CHECK_FULL_REGS (regs) \ do { \
arch/powerpc/include/asm/setup.h +2 -2
@@ -30,12 +30,12 @@ void setup_panic(void);#define ARCH_PANIC_TIMEOUT 180#ifdef CONFIG_PPC_PSERIES -extern void pseries_enable_reloc_on_exc (void ); +extern bool pseries_enable_reloc_on_exc (void );extern void pseries_disable_reloc_on_exc (void );extern void pseries_big_endian_exceptions (void );extern void pseries_little_endian_exceptions (void );#else -static inline void pseries_enable_reloc_on_exc (void ) {} +static inline bool pseries_enable_reloc_on_exc (void ) {return false; }static inline void pseries_disable_reloc_on_exc (void ) {}static inline void pseries_big_endian_exceptions (void ) {}static inline void pseries_little_endian_exceptions (void ) {}
arch/powerpc/include/asm/sstep.h +1 -0
@@ -39,6 +39,7 @@ enum instruction_type {CACHEOP ,BARRIER ,SYSCALL , +SYSCALL_VECTORED_0 ,MFMSR ,MTMSR , RFI,
arch/powerpc/include/asm/vdso.h +1 -0
@@ -17,6 +17,7 @@/* Offsets relative to thread->vdso_base */ extern unsigned long vdso64_rt_sigtramp; +extern unsigned long vdso64_rt_sigtramp_scv;extern unsigned long vdso32_sigtramp;extern unsigned long vdso32_rt_sigtramp;
arch/powerpc/kernel/cpu_setup_power.S +1 -1
@@ -184,7 +184,7 @@ __init_LPCR_ISA300: __init_FSCR: mfspr r3,SPRN_FSCR - ori r3,r3,FSCR_TAR|FSCR_DSCR|FSCR_EBB + ori r3,r3,FSCR_SCV|FSCR_TAR|FSCR_DSCR|FSCR_EBB mtspr SPRN_FSCR,r3 blr
arch/powerpc/kernel/cputable.c +2 -1
@@ -118,7 +118,8 @@ extern void __restore_cpu_e6500(void);#define COMMON_USER2_POWER9 (COMMON_USER2_POWER8 | \ PPC_FEATURE2_ARCH_3_00 | \ PPC_FEATURE2_HAS_IEEE128 | \ - PPC_FEATURE2_DARN ) + PPC_FEATURE2_DARN | \ + PPC_FEATURE2_SCV)#ifdef CONFIG_PPC_BOOK3E_64 #define COMMON_USER_BOOKE (COMMON_USER_PPC64 | PPC_FEATURE_BOOKE)
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -0
@@ -565,6 +565,7 @@ static struct dt_cpu_feature_match __initdata {"little-endian", feat_enable_le, CPU_FTR_REAL_LE}, {"smt", feat_enable_smt, 0}, {"interrupt-facilities", feat_enable, 0}, + {"system-call-vectored", feat_enable, 0}, {"timer-facilities", feat_enable, 0}, {"timer-facilities-v3", feat_enable, 0}, {"debug-facilities", feat_enable, 0},
arch/powerpc/kernel/entry_64.S +156 -2
187 lines not on this page · raw message
arch/powerpc/kernel/exceptions-64s.S +119 -4
153 lines not on this page · raw message
arch/powerpc/kernel/process.c +8 -2
@@ -1606,6 +1606,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long usp, {struct pt_regs *childregs, *kregs;extern void ret_from_fork (void ); +extern void ret_from_fork_scv (void );extern void ret_from_kernel_thread (void );void (*f)(void );unsigned long sp = (unsigned long )task_stack_page (p) +THREAD_SIZE ;
@@ -1639,7 +1640,9 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long usp,if (usp) childregs->gpr[1 ] = usp; p->thread.regs = childregs; - childregs->gpr[3 ] =0 ;/* Result from fork() */ +/* 64s sets this in ret_from_fork */ +if (!IS_ENABLED (CONFIG_PPC_BOOK3S_64 )) + childregs->gpr[3 ] =0 ;/* Result from fork() */ if (clone_flags &CLONE_SETTLS ) {if (!is_32bit_task ()) childregs->gpr[13 ] = tls;
@@ -1647,7 +1650,10 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long usp, childregs->gpr[2 ] = tls; } - f = ret_from_fork; +if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) &&TRAP (regs) ==0x3000 ) + f = ret_from_fork_scv; +else + f = ret_from_fork; } childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX ); sp -=STACK_FRAME_OVERHEAD ;
arch/powerpc/kernel/setup_64.c +4 -1
@@ -196,7 +196,10 @@ static void __init configure_exceptions(void)/* Under a PAPR hypervisor, we need hypercalls */ if (firmware_has_feature (FW_FEATURE_SET_MODE )) {/* Enable AIL if possible */ -pseries_enable_reloc_on_exc (); +if (!pseries_enable_reloc_on_exc ()) { + init_task.thread.fscr &= ~FSCR_SCV ; + cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_SCV ; + }/* * Tell the hypervisor that we want our exceptions to
arch/powerpc/kernel/signal.c +15 -4
@@ -205,8 +205,14 @@ static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,return ;/* error signalled ? */ -if (!(regs->ccr &0x10000000 )) +if (TRAP_IS_SCV (regs)) { +/* 32-bit compat mode sign extend? */ +if (!IS_ERR_VALUE (ret)) +return ; + ret = -ret; + }else if (!(regs->ccr &0x10000000 )) {return ; + }switch (ret) {case ERESTART_RESTARTBLOCK :
@@ -239,9 +245,14 @@ static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka, regs->nip -=4 ; regs->result =0 ; }else { - regs->result = -EINTR ; - regs->gpr[3 ] =EINTR ; - regs->ccr |=0x10000000 ; +if (TRAP_IS_SCV (regs)) { + regs->result = -EINTR ; + regs->gpr[3 ] = -EINTR ; + }else { + regs->result = -EINTR ; + regs->gpr[3 ] =EINTR ; + regs->ccr |=0x10000000 ; + } } }
arch/powerpc/kernel/signal_64.c +23 -5
@@ -596,7 +596,7 @@ static long restore_tm_sigcontexts(struct task_struct *tsk,/* * Setup the trampoline code on the stack */ -static long setup_trampoline (unsigned int syscall,unsigned int __user *tramp) +static long setup_trampoline (unsigned int syscall,unsigned int __user *tramp,bool use_scv) {int i;long err =0 ;
@@ -608,8 +608,13 @@ static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp) (__SIGNAL_FRAMESIZE & 0xffff), &tramp[1]);/* li r0, __NR_[rt_]sigreturn| */ err |=__put_user (PPC_INST_ADDI | (syscall &0xffff ), &tramp[2 ]); -/* sc */ - err |=__put_user (PPC_INST_SC , &tramp[3 ]); +if (use_scv) { +/* scv 0 */ + err |=__put_user (PPC_INST_SCV |__PPC_LEV (0 ), &tramp[3 ]); + }else { +/* sc 0 */ + err |=__put_user (PPC_INST_SC |__PPC_LEV (0 ), &tramp[3 ]); + }/* Minimal traceback info */ for (i=TRAMP_TRACEBACK ; i <TRAMP_SIZE ;i++)
@@ -819,6 +824,7 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,unsigned long newsp =0 ;long err =0 ;struct pt_regs *regs = tsk->thread.regs; +bool use_scv;#ifdef CONFIG_PPC_TRANSACTIONAL_MEM /* Save the thread's msr before get_tm_stackpointer() changes it */ unsigned long msr = regs->msr;
@@ -865,11 +871,23 @@ int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,/* Make sure signal handler doesn't get spurious FP exceptions */ tsk->thread.fp_state.fpscr =0 ; +/* +* Use SCV if possible for speed, though a SCV capable process that +* called into the kernel with an sc syscall must use sc for the +* trampoline so it restarts its system call properly. +*/ + use_scv = !!(current->thread.fscr &FSCR_SCV ); +if (use_scv &&TRAP_IS_SYSCALL (regs) && !TRAP_IS_SCV (regs)) + use_scv = false; +/* Set up to return from userspace. */ if (vdso64_rt_sigtramp && tsk->mm->context.vdso_base) { - regs->nip = tsk->mm->context.vdso_base + vdso64_rt_sigtramp; +if (use_scv) + regs->nip = tsk->mm->context.vdso_base + vdso64_rt_sigtramp_scv; +else + regs->nip = tsk->mm->context.vdso_base + vdso64_rt_sigtramp; }else { - err |=setup_trampoline (__NR_rt_sigreturn, &frame->tramp[0 ]); + err |=setup_trampoline (__NR_rt_sigreturn, &frame->tramp[0 ], use_scv);if (err) goto badframe; regs->nip = (unsigned long ) &frame->tramp[0 ];
arch/powerpc/kernel/syscall_64.c +24 -8
@@ -60,6 +60,11 @@ notrace long system_call_exception(long r3, long r4, long r5,local_irq_enable ();if (unlikely (current_thread_info ()->flags & _TIF_SYSCALL_DOTRACE)) { +if (unlikely (regs->trap ==0x7ff0 )) { +/* Unsupported scv vector */ +_exception (SIGILL , regs,ILL_ILLOPC , regs->nip); +return regs->gpr[3 ]; + }/* * We use the return value of do_syscall_trace_enter() as the * syscall number. If the syscall was rejected for any reason
@@ -78,6 +83,11 @@ notrace long system_call_exception(long r3, long r4, long r5, r8 = regs->gpr[8 ]; }else if (unlikely (r0 >= NR_syscalls)) { +if (unlikely (regs->trap ==0x7ff0 )) { +/* Unsupported scv vector */ +_exception (SIGILL , regs,ILL_ILLOPC , regs->nip); +return regs->gpr[3 ]; + }return -ENOSYS ; }
@@ -105,16 +115,20 @@ notrace long system_call_exception(long r3, long r4, long r5,* local irqs must be disabled. Returns false if the caller must re-enable * them, check for new work, and try again. */ -static notrace inline bool prep_irq_for_enabled_exit(void) +static notrace inline bool prep_irq_for_enabled_exit(bool clear_ri) {/* This must be done with RI=1 because tracing may touch vmaps */ trace_hardirqs_on ();/* This pattern matches prep_irq_for_idle */ -__hard_EE_RI_disable (); +if (clear_ri) +__hard_EE_RI_disable (); +else +__hard_irq_disable ();if (unlikely (lazy_irq_pending ())) {/* Took an interrupt, may have more exit work to do. */ -__hard_RI_enable (); +if (clear_ri) +__hard_RI_enable ();trace_hardirqs_off (); local_paca->irq_happened |=PACA_IRQ_HARD_DIS ;
@@ -136,7 +150,8 @@ static notrace inline bool prep_irq_for_enabled_exit(void)* because RI=0 and soft mask state is "unreconciled", so it is marked notrace. */ notrace unsigned long syscall_exit_prepare (unsigned long r3, -struct pt_regs *regs) +struct pt_regs *regs, +long scv) {unsigned long *ti_flagsp = ¤t_thread_info ()->flags;unsigned long ti_flags;
@@ -151,7 +166,7 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, ti_flags = *ti_flagsp; -if (unlikely (r3 >= (unsigned long )-MAX_ERRNO )) { +if (unlikely (r3 >= (unsigned long )-MAX_ERRNO ) && !scv) {if (likely (!(ti_flags & (_TIF_NOERROR | _TIF_RESTOREALL)))) { r3 = -r3; regs->ccr |=0x10000000 ;/* Set SO bit in CR */
@@ -211,7 +226,8 @@ notrace unsigned long syscall_exit_prepare(unsigned long r3, } } -if (unlikely (!prep_irq_for_enabled_exit ())) { +/* scv need not set RI=0 because SRRs are not used */ +if (unlikely (!prep_irq_for_enabled_exit (!scv))) {local_irq_enable (); goto again; }
@@ -282,7 +298,7 @@ notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs, unsigned } } -if (unlikely (!prep_irq_for_enabled_exit ())) { +if (unlikely (!prep_irq_for_enabled_exit (true))) {local_irq_enable ();local_irq_disable (); goto again;
@@ -345,7 +361,7 @@ notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs, unsign } } -if (unlikely (!prep_irq_for_enabled_exit ())) { +if (unlikely (!prep_irq_for_enabled_exit (true))) {/* * Can't local_irq_restore to replay if we were in * interrupt context. Must replay directly.
arch/powerpc/kernel/vdso.c +2 -0
@@ -61,6 +61,7 @@ static void *vdso64_kbase = &vdso64_start;static unsigned int vdso64_pages;static struct page **vdso64_pagelist;unsigned long vdso64_rt_sigtramp; +unsigned long vdso64_rt_sigtramp_scv;#endif /* CONFIG_PPC64 */ static int vdso_ready;
@@ -477,6 +478,7 @@ static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,#ifdef CONFIG_PPC64 vdso64_rt_sigtramp =find_function64 (v64,"__kernel_sigtramp_rt64" ); + vdso64_rt_sigtramp_scv =find_function64 (v64,"__kernel_sigtramp_rt64_scv" );#endif vdso32_sigtramp =find_function32 (v32,"__kernel_sigtramp32" ); vdso32_rt_sigtramp =find_function32 (v32,"__kernel_sigtramp_rt32" );
arch/powerpc/kernel/vdso64/sigtramp.S +33 -1
@@ -15,6 +15,17 @@ .text + .balign 8 + .balign IFETCH_ALIGN_BYTES +V_FUNCTION_BEGIN(__kernel_sigtramp_rt64_scv) +.Lsigrt_scv_start: + bctrl /* call the handler */ + addi r1, r1, __SIGNAL_FRAMESIZE + li r0,__NR_rt_sigreturn + scv 0 +.Lsigrt_scv_end: +V_FUNCTION_END(__kernel_sigtramp_rt64_scv) + .balign 8 .balign IFETCH_ALIGN_BYTES V_FUNCTION_BEGIN(__kernel_sigtramp_rt64)
@@ -285,6 +296,27 @@ V_FUNCTION_END(__kernel_sigtramp_rt64) .long .Lfde0_end - .Lfde0_start .Lfde0_start: .long .Lfde0_start - .Lcie /* CIE pointer. */ + .quad .Lsigrt_scv_start - . /* PC start, length */ + .quad .Lsigrt_scv_end - .Lsigrt_scv_start + .uleb128 0 /* Augmentation */ + EH_FRAME_GEN + EH_FRAME_FP + EH_FRAME_VMX +# Do we really need to describe the frame at this point? ie. will +# we ever have some call chain that returns somewhere past the addi? +# I don't think so, since gcc doesn't support async signals. +# .byte 0x41 /* DW_CFA_advance_loc 1*4 */ +#undef PTREGS +#define PTREGS 168+56 +# EH_FRAME_GEN +# EH_FRAME_FP +# EH_FRAME_VMX + .balign 8 +.Lfde0_end: + .long .Lfde0_end - .Lfde0_start + +.Lfde1_start: + .long .Lfde1_start - .Lcie /* CIE pointer. */ .quad .Lsigrt_start - . /* PC start, length */ .quad .Lsigrt_end - .Lsigrt_start .uleb128 0 /* Augmentation */
@@ -301,4 +333,4 @@ V_FUNCTION_END(__kernel_sigtramp_rt64) # EH_FRAME_FP # EH_FRAME_VMX .balign 8 -.Lfde0_end: +.Lfde1_end:
arch/powerpc/kernel/vdso64/vdso64.lds.S +1 -0
@@ -150,6 +150,7 @@ VERSION __kernel_get_tbfreq; __kernel_sync_dicache; __kernel_sync_dicache_p5; + __kernel_sigtramp_rt64_scv; __kernel_sigtramp_rt64; __kernel_getcpu; __kernel_time;
arch/powerpc/lib/sstep.c +14 -0
@@ -15,6 +15,7 @@#include <asm/cputable.h> extern char system_call_common[]; +extern char system_call_vectored_emulate[];#ifdef CONFIG_PPC64 /* Bits in SRR1 that are copied from MSR */
@@ -1190,6 +1191,9 @@ int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,case 17 :/* sc */ if ((instr &0xfe2 ) ==2 ) op->type =SYSCALL ; +else if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) && + (instr &0xfe3 ) ==1 ) + op->type =SYSCALL_VECTORED_0 ;else op->type =UNKNOWN ;return 0 ;
@@ -3194,6 +3198,16 @@ int emulate_step(struct pt_regs *regs, unsigned int instr) regs->msr =MSR_KERNEL ;return 1 ; +case SYSCALL_VECTORED_0 :/* scv 0 */ + regs->gpr[9 ] = regs->gpr[13 ]; + regs->gpr[10 ] =MSR_KERNEL ; + regs->gpr[11 ] = regs->nip +4 ; + regs->gpr[12 ] = regs->msr &MSR_MASK ; + regs->gpr[13 ] = (unsigned long )get_paca (); + regs->nip = (unsigned long ) &system_call_vectored_emulate; + regs->msr =MSR_KERNEL ; +return 1 ; +case RFI :return -1 ;#endif
arch/powerpc/perf/callchain_64.c +6 -3
@@ -95,9 +95,12 @@ static int is_sigreturn_64_address(unsigned long nip, unsigned long fp) {if (nip == fp + offsetof(struct signal_frame_64 , tramp))return 1 ; -if (vdso64_rt_sigtramp && current->mm->context.vdso_base && - nip == current->mm->context.vdso_base + vdso64_rt_sigtramp) -return 1 ; +if (vdso64_rt_sigtramp && current->mm->context.vdso_base) { +if (nip == current->mm->context.vdso_base + vdso64_rt_sigtramp) +return 1 ; +if (nip == current->mm->context.vdso_base + vdso64_rt_sigtramp_scv) +return 1 ; + }return 0 ; }
arch/powerpc/platforms/pseries/setup.c +5 -3
@@ -349,7 +349,7 @@ static void pseries_lpar_idle(void)* to ever be a problem in practice we can move this into a kernel thread to * finish off the process later in boot. */ -void pseries_enable_reloc_on_exc (void ) +bool pseries_enable_reloc_on_exc (void ) {long rc;unsigned int delay, total_delay =0 ;
@@ -360,11 +360,13 @@ void pseries_enable_reloc_on_exc(void)if (rc ==H_P2 ) {pr_info ("Relocation on exceptions not" " supported\n" ); +return false; }else if (rc !=H_SUCCESS ) {pr_warn ("Unable to enable relocation" " on exceptions: %ld\n" , rc); +return false; } -break ; +return true; } delay =get_longbusy_msecs (rc);
@@ -373,7 +375,7 @@ void pseries_enable_reloc_on_exc(void)pr_warn ("Warning: Giving up waiting to enable " "relocation on exceptions (%u msec)!\n" , total_delay); -return ; +return false; }mdelay (delay);
arch/powerpc/xmon/xmon.c +1 -0
@@ -1540,6 +1540,7 @@ const char *getvecname(unsigned long vec)case 0x1300 : ret ="(Instruction Breakpoint)" ;break ;case 0x1500 : ret ="(Denormalisation)" ;break ;case 0x1700 : ret ="(Altivec Assist)" ;break ; +case 0x3000 : ret ="(System Call Vectored)" ;break ;default : ret ="" ; }return ret;
Signature · 1 line
2.23.0
Re: [RFC PATCH 2/2] powerpc/64s: system call support for scv/rfscv instructions
Hi!
Quoted 1 line · Nicholas Piggin
Add support for the scv instruction on POWER9 and later CPUs.
Looks good to me in general :-)
Quoted 4 lines
For now this implements the zeroth scv vector 'scv 0', as identical to 'sc' system calls, with the exception that lr is not preserved, and it is 64-bit only. There may yet be changes made to this ABI, so it's for testing only.
What does it do with SF=0? I don't see how it is obviously not a security hole currently (but I didn't look too closely).
Segher
Nicholas Piggin
reviewerRe: [RFC PATCH 2/2] powerpc/64s: system call support for scv/rfscv instructions
Excerpts from Segher Boessenkool's message of May 6, 2020 8:11 am:
Quoted 6 lines
Hi!
Quoted 1 line · Nicholas Piggin
Add support for the scv instruction on POWER9 and later CPUs.
Looks good to me in general :-)
Thanks for taking a look.
Quoted 7 lines
Quoted 4 lines
For now this implements the zeroth scv vector 'scv 0', as identical to 'sc' system calls, with the exception that lr is not preserved, and it is 64-bit only. There may yet be changes made to this ABI, so it's for testing only.
What does it do with SF=0? I don't see how it is obviously not a security hole currently (but I didn't look too closely).
Oh that's an outdated comment, I since decided better to keep all the code common and handle 32-bit compat the same way as existing sc syscall.
Thanks,
Nick
Re: [RFC PATCH 2/2] powerpc/64s: system call support for scv/rfscv instructions
In reply to Nicholas PigginHi Nicholas,
Small comment below:
Quoted 19 lines · Nicholas Piggin
Add support for the scv instruction on POWER9 and later CPUs.
For now this implements the zeroth scv vector 'scv 0', as identical to 'sc' system calls, with the exception that lr is not preserved, and it is 64-bit only. There may yet be changes made to this ABI, so it's for testing only.
rfscv is implemented to return from scv type system calls. It can not be used to return from sc system calls because those are defined to preserve lr.
In a comparison of getpid syscall, the test program had scv taking about 3 more cycles in user mode (92 vs 89 for sc), due to lr handling. getpid syscall throughput on POWER9 is improved by 33%, mostly due to reducing mtmsr and mtspr.
Signed-off-by Nicholas Piggin <[email protected]>--- Documentation/powerpc/syscall64-abi.rst | 42 ++++--
[...]
Documentation/powerpc/syscall64-abi.rst rules, with the following differences. · line 40
Quoted 11 lines
Documentation/powerpc/syscall64-abi.rst +11
-0+Return value +------------ +- For the sc instruction, both a return value and a return error code are + returned. cr0.SO is the return error code, and r3 is the return value or + error code. When cr0.SO is clear, the syscall succeeded and r3 is the return + value. When cr0.SO is set, the syscall failed and r3 is the error code that + generally corresponds to errno. + +- For the scv 0 instruction, there is a return value indicates failure if it + is >= -MAX_ERRNO (-4095) as an unsigned comparison, in which case it is the + negated return error code. Otherwise it is the successful return value.
I believe this last paragraph is a bit confusing (didn't quite get the unsigned comparison with negative values). So instead of cr0.SO to indicate failure, scv returns the negated error code, and positive in case of success?
Thanks,
Matheus Castanho
Nicholas Piggin
reviewerRe: [RFC PATCH 2/2] powerpc/64s: system call support for scv/rfscv instructions
Excerpts from Matheus Castanho's message of May 14, 2020 6:55 am:
Quoted 27 lines
Hi Nicholas,
Small comment below:
Quoted 19 lines · Nicholas Piggin
Add support for the scv instruction on POWER9 and later CPUs.
For now this implements the zeroth scv vector 'scv 0', as identical to 'sc' system calls, with the exception that lr is not preserved, and it is 64-bit only. There may yet be changes made to this ABI, so it's for testing only.
rfscv is implemented to return from scv type system calls. It can not be used to return from sc system calls because those are defined to preserve lr.
In a comparison of getpid syscall, the test program had scv taking about 3 more cycles in user mode (92 vs 89 for sc), due to lr handling. getpid syscall throughput on POWER9 is improved by 33%, mostly due to reducing mtmsr and mtspr.
Signed-off-by Nicholas Piggin <[email protected]>--- Documentation/powerpc/syscall64-abi.rst | 42 ++++--[...]
Quoted 16 lines
Documentation/powerpc/syscall64-abi.rst rules, with the following differences. · line 40
Quoted 11 lines
Documentation/powerpc/syscall64-abi.rst +11
-0+Return value +------------ +- For the sc instruction, both a return value and a return error code are + returned. cr0.SO is the return error code, and r3 is the return value or + error code. When cr0.SO is clear, the syscall succeeded and r3 is the return + value. When cr0.SO is set, the syscall failed and r3 is the error code that + generally corresponds to errno. + +- For the scv 0 instruction, there is a return value indicates failure if it + is >= -MAX_ERRNO (-4095) as an unsigned comparison, in which case it is the + negated return error code. Otherwise it is the successful return value.I believe this last paragraph is a bit confusing (didn't quite get the unsigned comparison with negative values). So instead of cr0.SO to indicate failure, scv returns the negated error code, and positive in case of success?
Yes, it will be like other major architectures and return values from -4095..-1 indicate an error with error value equal to -return value.
I will try to make it a bit clearer.
Thanks,
Nick