FP/VEC/VSX switching optimisations
PATCH v3 1/9 selftests/powerpc: Test the preservation of FPU and VMX regs across syscall
In reply to Cyril BurTest that the non volatile floating point and Altivec registers get correctly preserved across the fork() syscall.
fork() works nicely for this purpose, the registers should be the same for both parent and child
--- tools/testing/selftests/powerpc/Makefile | 3 +- tools/testing/selftests/powerpc/basic_asm.h | 26 +++ tools/testing/selftests/powerpc/math/.gitignore | 2 + tools/testing/selftests/powerpc/math/Makefile | 14 ++ tools/testing/selftests/powerpc/math/fpu_asm.S | 161 +++++++++++++++++ tools/testing/selftests/powerpc/math/fpu_syscall.c | 90 ++++++++++ tools/testing/selftests/powerpc/math/vmx_asm.S | 193 +++++++++++++++++++++ tools/testing/selftests/powerpc/math/vmx_syscall.c | 92 ++++++++++ 8 files changed, 580 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/powerpc/basic_asm.h create mode 100644 tools/testing/selftests/powerpc/math/.gitignore create mode 100644 tools/testing/selftests/powerpc/math/Makefile create mode 100644 tools/testing/selftests/powerpc/math/fpu_asm.S create mode 100644 tools/testing/selftests/powerpc/math/fpu_syscall.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_asm.S create mode 100644 tools/testing/selftests/powerpc/math/vmx_syscall.c
tools/testing/selftests/powerpc/Makefile +2 -1
@@ -22,7 +22,8 @@ SUB_DIRS = benchmarks \ switch_endian \ syscalls \ tm \ - vphn + vphn \ + math endif
tools/testing/selftests/powerpc/basic_asm.h new file +26 -0
@@ -0,0 +1,26 @@ +#include <ppc-asm.h> +#include <asm/unistd.h> + +#define LOAD_REG_IMMEDIATE (reg,expr) \ + lis reg,(expr)@highest; \ + ori reg,reg,(expr)@higher; \ + rldicr reg,reg,32,31; \ + oris reg,reg,(expr)@high; \ + ori reg,reg,(expr)@l; + +#define PUSH_BASIC_STACK (size) \ + std 2,24(sp); \ + mflr r0; \ + std r0,16(sp); \ + mfcr r0; \ + stw r0,8(sp); \ + stdu sp,-size(sp); + +#define POP_BASIC_STACK (size) \ + addi sp,sp,size; \ + ld 2,24(sp); \ + ld r0,16(sp); \ + mtlr r0; \ + lwz r0,8(sp); \ + mtcr r0; \ +
tools/testing/selftests/powerpc/math/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +fpu_syscall +vmx_syscall
tools/testing/selftests/powerpc/math/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +TEST_PROGS := fpu_syscall vmx_syscall + +all: $(TEST_PROGS) + +$(TEST_PROGS): ../harness.c +$(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec + +fpu_syscall: fpu_asm.S +vmx_syscall: vmx_asm.S + +include ../../lib.mk + +clean: + rm -f $(TEST_PROGS) *.o
tools/testing/selftests/powerpc/math/fpu_asm.S new file +161 -0
161 lines not on this page · raw message
tools/testing/selftests/powerpc/math/fpu_syscall.c new file +90 -0
90 lines not on this page · raw message
tools/testing/selftests/powerpc/math/vmx_asm.S new file +193 -0
193 lines not on this page · raw message
tools/testing/selftests/powerpc/math/vmx_syscall.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
2.7.0
PATCH v3 4/9 powerpc: Explicitly disable math features when copying thread
In reply to Cyril BurCurrently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as clone() and fork() to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.
This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace.
--- arch/powerpc/kernel/process.c | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/kernel/process.c +1 -0
@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, f = ret_from_fork; } + childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX ); sp -=STACK_FRAME_OVERHEAD ;/*
Signature · 1 line
2.7.0
PATCH v3 5/9 powerpc: Restore FPU/VEC/VSX if previously used
In reply to Cyril BurCurrently the FPU, VEC and VSX facilities are lazily loaded. This is not a problem unless a process is using these facilities.
Modern versions of GCC are very good at automatically vectorising code, new and modernised workloads make use of floating point and vector facilities, even the kernel makes use of vectorised memcpy.
All this combined greatly increases the cost of a syscall since the kernel uses the facilities sometimes even in syscall fast-path making it increasingly common for a thread to take an *_unavailable exception soon after a syscall, not to mention potentially taking all three.
The obvious overcompensation to this problem is to simply always load all the facilities on every exit to userspace. Loading up all FPU, VEC and VSX registers every time can be expensive and if a workload does avoid using them, it should not be forced to incur this penalty.
An 8bit counter is used to detect if the registers have been used in the past and the registers are always loaded until the value wraps to back to zero.
Several versions of the assembly in entry_64.S. 1. Always calling C, 2. Performing a common case check and then calling C and 3. A complex check in asm. After some benchmarking it was determined that avoiding C in the common case is a performance benefit. The full check in asm greatly complicated that codepath for a negligible performance gain and the trade-off was deemed not worth it.
--- arch/powerpc/include/asm/processor.h | 2 + arch/powerpc/kernel/asm-offsets.c | 2 + arch/powerpc/kernel/entry_64.S | 21 +++++++-- arch/powerpc/kernel/fpu.S | 4 ++ arch/powerpc/kernel/process.c | 84 +++++++++++++++++++++++++++++++----- arch/powerpc/kernel/vector.S | 4 ++ 6 files changed, 103 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/processor.h +2 -0
@@ -236,11 +236,13 @@ struct thread_struct {#endif struct arch_hw_breakpoint hw_brk;/* info on the hardware breakpoint */ unsigned long trap_nr;/* last trap # on this thread */ +u8 load_fp;#ifdef CONFIG_ALTIVEC struct thread_vr_state vr_state;struct thread_vr_state *vr_save_area;unsigned long vrsave;int used_vr;/* set if process has used altivec */ +u8 load_vec;#endif /* CONFIG_ALTIVEC */ #ifdef CONFIG_VSX /* VSR status */
arch/powerpc/kernel/asm-offsets.c +2 -0
@@ -95,12 +95,14 @@ int main(void)DEFINE (THREAD_FPSTATE , offsetof(struct thread_struct , fp_state));DEFINE (THREAD_FPSAVEAREA , offsetof(struct thread_struct , fp_save_area));DEFINE (FPSTATE_FPSCR , offsetof(struct thread_fp_state , fpscr)); +DEFINE (THREAD_LOAD_FP , offsetof(struct thread_struct , load_fp));#ifdef CONFIG_ALTIVEC DEFINE (THREAD_VRSTATE , offsetof(struct thread_struct , vr_state));DEFINE (THREAD_VRSAVEAREA , offsetof(struct thread_struct , vr_save_area));DEFINE (THREAD_VRSAVE , offsetof(struct thread_struct , vrsave));DEFINE (THREAD_USED_VR , offsetof(struct thread_struct , used_vr));DEFINE (VRSTATE_VSCR , offsetof(struct thread_vr_state , vscr)); +DEFINE (THREAD_LOAD_VEC , offsetof(struct thread_struct , load_vec));#endif /* CONFIG_ALTIVEC */ #ifdef CONFIG_VSX DEFINE (THREAD_USED_VSR , offsetof(struct thread_struct , used_vsr));
arch/powerpc/kernel/entry_64.S +18 -3
@@ -210,7 +210,20 @@ system_call: /* label this so stack traces look sane */ li r11,-MAX_ERRNO andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK) bne- syscall_exit_work - cmpld r3,r11 + + andi. r0,r8,MSR_FP + beq 2f +#ifdef CONFIG_ALTIVEC + andis. r0,r8,MSR_VEC@h + bne 3f +#endif +2: addi r3,r1,STACK_FRAME_OVERHEAD + bl restore_math + ld r8,_MSR(r1) + ld r3,RESULT(r1) + li r11,-MAX_ERRNO + +3: cmpld r3,r11 ld r5,_CCR(r1) bge- syscall_error .Lsyscall_error_cont:
@@ -602,8 +615,8 @@ _GLOBAL(ret_from_except_lite) /* Check current_thread_info()->flags */ andi. r0,r4,_TIF_USER_WORK_MASK -#ifdef CONFIG_PPC_BOOK3E bne 1f +#ifdef CONFIG_PPC_BOOK3E /* * Check to see if the dbcr0 register is set up to debug. * Use the internal debug mode bit to do this.
@@ -618,7 +631,9 @@ _GLOBAL(ret_from_except_lite) mtspr SPRN_DBSR,r10 b restore #else - beq restore + addi r3,r1,STACK_FRAME_OVERHEAD + bl restore_math + b restore #endif 1: andi. r0,r4,_TIF_NEED_RESCHED beq 2f
arch/powerpc/kernel/fpu.S +4 -0
@@ -130,6 +130,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX) or r12,r12,r4 std r12,_MSR(r1) #endif + /* Don't care if r4 overflows, this is desired behaviour */ + lbz r4,THREAD_LOAD_FP(r5) + addi r4,r4,1 + stb r4,THREAD_LOAD_FP(r5) addi r10,r5,THREAD_FPSTATE lfd fr0,FPSTATE_FPSCR(r10) MTFSF_L(fr0)
arch/powerpc/kernel/process.c +73 -11
123 lines not on this page · raw message
arch/powerpc/kernel/vector.S +4 -0
@@ -91,6 +91,10 @@ _GLOBAL(load_up_altivec) oris r12,r12,MSR_VEC@h std r12,_MSR(r1) #endif + /* Don't care if r4 overflows, this is desired behaviour */ + lbz r4,THREAD_LOAD_VEC(r5) + addi r4,r4,1 + stb r4,THREAD_LOAD_VEC(r5) addi r6,r5,THREAD_VRSTATE li r4,1 li r10,VRSTATE_VSCR
Signature · 1 line
2.7.0
PATCH v3 9/9 powerpc: Add the ability to save VSX without giving it up
In reply to Cyril BurThis patch adds the ability to be able to save the VSX registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.
This patch builds on a previous optimisation for the FPU and VEC registers in the thread copy path to avoid a possibly pointless reload of VSX state.
--- arch/powerpc/include/asm/switch_to.h | 4 ---- arch/powerpc/kernel/ppc_ksyms.c | 4 ---- arch/powerpc/kernel/process.c | 42 +++++++++++++++++++++++++----------- arch/powerpc/kernel/vector.S | 17 --------------- 4 files changed, 30 insertions(+), 37 deletions(-)
arch/powerpc/include/asm/switch_to.h +0 -4
@@ -56,14 +56,10 @@ static inline void __giveup_altivec(struct task_struct *t) { }#ifdef CONFIG_VSX extern void enable_kernel_vsx (void );extern void flush_vsx_to_thread (struct task_struct *); -extern void giveup_vsx (struct task_struct *); -extern void __giveup_vsx (struct task_struct *);static inline void disable_kernel_vsx (void ) {msr_check_and_clear (MSR_FP |MSR_VEC |MSR_VSX ); } -#else -static inline void __giveup_vsx (struct task_struct *t) { }#endif #ifdef CONFIG_SPE
arch/powerpc/kernel/ppc_ksyms.c +0 -4
@@ -28,10 +28,6 @@ EXPORT_SYMBOL(load_vr_state);EXPORT_SYMBOL (store_vr_state);#endif -#ifdef CONFIG_VSX -EXPORT_SYMBOL_GPL (__giveup_vsx); -#endif -#ifdef CONFIG_EPAPR_PARAVIRT EXPORT_SYMBOL (epapr_hypercall_start);#endif
arch/powerpc/kernel/process.c +30 -12
69 lines not on this page · raw message
arch/powerpc/kernel/vector.S +0 -17
@@ -151,23 +151,6 @@ _GLOBAL(load_up_vsx) std r12,_MSR(r1) b fast_exception_return -/* - * __giveup_vsx(tsk) - * Disable VSX for the task given as the argument. - * Does NOT save vsx registers. - */ -_GLOBAL(__giveup_vsx) - addi r3,r3,THREAD /* want THREAD of task */ - ld r5,PT_REGS(r3) - cmpdi 0,r5,0 - beq 1f - ld r4,_MSR-STACK_FRAME_OVERHEAD(r5) - lis r3,MSR_VSX@h - andc r4,r4,r3 /* disable VSX for previous task */ - std r4,_MSR-STACK_FRAME_OVERHEAD(r5) -1: - blr - #endif /* CONFIG_VSX */
Signature · 1 line
2.7.0
PATCH v3 8/9 powerpc: Add the ability to save Altivec without giving it up
In reply to Cyril BurThis patch adds the ability to be able to save the VEC registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.
This patch builds on a previous optimisation for the FPU registers in the thread copy path to avoid a possibly pointless reload of VEC state.
--- arch/powerpc/include/asm/switch_to.h | 3 ++- arch/powerpc/kernel/process.c | 12 +++++++++++- arch/powerpc/kernel/vector.S | 24 ++++-------------------- 3 files changed, 17 insertions(+), 22 deletions(-)
arch/powerpc/include/asm/switch_to.h +2 -1
@@ -43,12 +43,13 @@ static inline void flush_fp_to_thread(struct task_struct *t) { }extern void enable_kernel_altivec (void );extern void flush_altivec_to_thread (struct task_struct *);extern void giveup_altivec (struct task_struct *); -extern void __giveup_altivec (struct task_struct *); +extern void save_altivec (struct task_struct *);static inline void disable_kernel_altivec (void ) {msr_check_and_clear (MSR_VEC ); }#else +static inline void save_altivec (struct task_struct *t) { }static inline void __giveup_altivec (struct task_struct *t) { }#endif
arch/powerpc/kernel/process.c +11 -1
@@ -213,6 +213,16 @@ static int restore_fp(struct task_struct *tsk) { return 0; }#ifdef CONFIG_ALTIVEC #define loadvec (thr) ((thr).load_vec) +static void __giveup_altivec (struct task_struct *tsk) +{ +save_altivec (tsk); + tsk->thread.regs->msr &= ~MSR_VEC ; +#ifdef CONFIG_VSX +if (cpu_has_feature (CPU_FTR_VSX )) + tsk->thread.regs->msr &= ~MSR_VSX ; +#endif +} +void giveup_altivec (struct task_struct *tsk) {check_if_tm_restore_required (tsk);
@@ -476,7 +486,7 @@ void save_all(struct task_struct *tsk)save_fpu (tsk);if (usermsr &MSR_VEC ) -__giveup_altivec (tsk); +save_altivec (tsk);if (usermsr &MSR_VSX )__giveup_vsx (tsk);
arch/powerpc/kernel/vector.S +4 -20
@@ -106,36 +106,20 @@ _GLOBAL(load_up_altivec) blr /* - * __giveup_altivec(tsk) - * Disable VMX for the task given as the argument, - * and save the vector registers in its thread_struct. + * save_altivec(tsk) + * Save the vector registers to its thread_struct */ -_GLOBAL(__giveup_altivec) +_GLOBAL(save_altivec) addi r3,r3,THREAD /* want THREAD of task */ PPC_LL r7,THREAD_VRSAVEAREA(r3) PPC_LL r5,PT_REGS(r3) PPC_LCMPI 0,r7,0 bne 2f addi r7,r3,THREAD_VRSTATE -2: PPC_LCMPI 0,r5,0 - SAVE_32VRS(0,r4,r7) +2: SAVE_32VRS(0,r4,r7) mfvscr v0 li r4,VRSTATE_VSCR stvx v0,r4,r7 - beq 1f - PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5) -#ifdef CONFIG_VSX -BEGIN_FTR_SECTION - lis r3,(MSR_VEC|MSR_VSX)@h -FTR_SECTION_ELSE - lis r3,MSR_VEC@h -ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX) -#else - lis r3,MSR_VEC@h -#endif - andc r4,r4,r3 /* disable FP for previous task */ - PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5) -1: blr #ifdef CONFIG_VSX
Signature · 1 line
2.7.0
PATCH v3 7/9 powerpc: Add the ability to save FPU without giving it up
In reply to Cyril BurThis patch adds the ability to be able to save the FPU registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.
This patch optimises the thread copy path (as a result of a fork() or clone()) so that the parent thread can return to userspace with hot registers avoiding a possibly pointless reload of FPU register state.
--- arch/powerpc/include/asm/switch_to.h | 3 ++- arch/powerpc/kernel/fpu.S | 21 ++++----------------- arch/powerpc/kernel/process.c | 12 +++++++++++- 3 files changed, 17 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/switch_to.h +2 -1
@@ -28,13 +28,14 @@ extern void giveup_all(struct task_struct *);extern void enable_kernel_fp (void );extern void flush_fp_to_thread (struct task_struct *);extern void giveup_fpu (struct task_struct *); -extern void __giveup_fpu (struct task_struct *); +extern void save_fpu (struct task_struct *);static inline void disable_kernel_fp (void ) {msr_check_and_clear (MSR_FP ); }#else static inline void __giveup_fpu (struct task_struct *t) { } +static inline void save_fpu (struct task_struct *t) { }static inline void flush_fp_to_thread (struct task_struct *t) { }#endif
arch/powerpc/kernel/fpu.S +4 -17
@@ -143,33 +143,20 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX) blr /* - * __giveup_fpu(tsk) - * Disable FP for the task given as the argument, - * and save the floating-point registers in its thread_struct. + * save_fpu(tsk) + * Save the floating-point registers in its thread_struct. * Enables the FPU for use in the kernel on return. */ -_GLOBAL(__giveup_fpu) +_GLOBAL(save_fpu) addi r3,r3,THREAD /* want THREAD of task */ PPC_LL r6,THREAD_FPSAVEAREA(r3) PPC_LL r5,PT_REGS(r3) PPC_LCMPI 0,r6,0 bne 2f addi r6,r3,THREAD_FPSTATE -2: PPC_LCMPI 0,r5,0 - SAVE_32FPVSRS(0, R4, R6) +2: SAVE_32FPVSRS(0, R4, R6) mffs fr0 stfd fr0,FPSTATE_FPSCR(r6) - beq 1f - PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5) - li r3,MSR_FP|MSR_FE0|MSR_FE1 -#ifdef CONFIG_VSX -BEGIN_FTR_SECTION - oris r3,r3,MSR_VSX@h -END_FTR_SECTION_IFSET(CPU_FTR_VSX) -#endif - andc r4,r4,r3 /* disable FP for previous task */ - PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5) -1: blr /*
arch/powerpc/kernel/process.c +11 -1
@@ -133,6 +133,16 @@ void __msr_check_and_clear(unsigned long bits)EXPORT_SYMBOL (__msr_check_and_clear);#ifdef CONFIG_PPC_FPU +void __giveup_fpu (struct task_struct *tsk) +{ +save_fpu (tsk); + tsk->thread.regs->msr &= ~MSR_FP ; +#ifdef CONFIG_VSX +if (cpu_has_feature (CPU_FTR_VSX )) + tsk->thread.regs->msr &= ~MSR_VSX ; +#endif +} +void giveup_fpu (struct task_struct *tsk) {check_if_tm_restore_required (tsk);
@@ -463,7 +473,7 @@ void save_all(struct task_struct *tsk)msr_check_and_set (msr_all_available);if (usermsr &MSR_FP ) -__giveup_fpu (tsk); +save_fpu (tsk);if (usermsr &MSR_VEC )__giveup_altivec (tsk);
Signature · 1 line
2.7.0
PATCH v3 6/9 powerpc: Prepare for splitting giveup_{fpu, altivec, vsx} in two
In reply to Cyril BurThis prepares for the decoupling of saving {fpu,altivec,vsx} registers and marking {fpu,altivec,vsx} as being unused by a thread.
Currently giveup_{fpu,altivec,vsx}() does both however optimisations to task switching can be made if these two operations are decoupled. save_all() will permit the saving of registers to thread structs and leave threads MSR with bits enabled.
This patch introduces no functional change.
--- arch/powerpc/include/asm/switch_to.h | 7 +++++++ arch/powerpc/kernel/process.c | 39 +++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/switch_to.h +7 -0
@@ -34,6 +34,7 @@ static inline void disable_kernel_fp(void) msr_check_and_clear(MSR_FP); }#else +static inline void __giveup_fpu (struct task_struct *t) { }static inline void flush_fp_to_thread (struct task_struct *t) { }#endif
@@ -46,6 +47,8 @@ static inline void disable_kernel_altivec(void) {msr_check_and_clear (MSR_VEC ); } +#else +static inline void __giveup_altivec (struct task_struct *t) { }#endif #ifdef CONFIG_VSX
@@ -57,6 +60,8 @@ static inline void disable_kernel_vsx(void) {msr_check_and_clear (MSR_FP |MSR_VEC |MSR_VSX ); } +#else +static inline void __giveup_vsx (struct task_struct *t) { }#endif #ifdef CONFIG_SPE
@@ -68,6 +73,8 @@ static inline void disable_kernel_spe(void) {msr_check_and_clear (MSR_SPE ); } +#else +static inline void __giveup_spe (struct task_struct *t) { }#endif static inline void clear_task_ebb (struct task_struct *t)
arch/powerpc/kernel/process.c +38 -1
@@ -349,6 +349,14 @@ void flush_spe_to_thread(struct task_struct *tsk) preempt_enable(); } } +#else +/* +* save_all() is going to test MSR_SPE, rather than pull in all the +* booke definitions all the time on a books kernel just ensure it exists +* but acts as a nop. +*/ + +#define MSR_SPE 0#endif /* CONFIG_SPE */ static unsigned long msr_all_available;
@@ -440,12 +448,41 @@ void restore_math(struct pt_regs *regs) regs->msr = msr; } +void save_all (struct task_struct *tsk) +{ +unsigned long usermsr; + +if (!tsk->thread.regs) +return ; + + usermsr = tsk->thread.regs->msr; + +if ((usermsr & msr_all_available) ==0 ) +return ; + +msr_check_and_set (msr_all_available); + +if (usermsr &MSR_FP ) +__giveup_fpu (tsk); + +if (usermsr &MSR_VEC ) +__giveup_altivec (tsk); + +if (usermsr &MSR_VSX ) +__giveup_vsx (tsk); + +if (usermsr &MSR_SPE ) +__giveup_spe (tsk); + +msr_check_and_clear (msr_all_available); +} +void flush_all_to_thread (struct task_struct *tsk) {if (tsk->thread.regs) {preempt_disable ();BUG_ON (tsk != current); -giveup_all (tsk); +save_all (tsk);#ifdef CONFIG_SPE if (tsk->thread.regs->msr &MSR_SPE )
Signature · 1 line
2.7.0
PATCH v3 3/9 selftests/powerpc: Test FPU and VMX regs in signal ucontext
In reply to Cyril BurLoad up the non volatile FPU and VMX regs and ensure that they are the expected value in a signal handler
--- tools/testing/selftests/powerpc/math/.gitignore | 2 + tools/testing/selftests/powerpc/math/Makefile | 4 +- tools/testing/selftests/powerpc/math/fpu_signal.c | 135 +++++++++++++++++++++ tools/testing/selftests/powerpc/math/vmx_signal.c | 138 ++++++++++++++++++++++ 4 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/powerpc/math/fpu_signal.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_signal.c
tools/testing/selftests/powerpc/math/.gitignore +2 -0
@@ -2,3 +2,5 @@ fpu_syscall vmx_syscall fpu_preempt vmx_preempt +fpu_signal +vmx_signal
tools/testing/selftests/powerpc/math/Makefile +3 -1
@@ -1,4 +1,4 @@ -TEST_PROGS := fpu_syscall fpu_preempt vmx_syscall vmx_preempt +TEST_PROGS := fpu_syscall fpu_preempt fpu_signal vmx_syscall vmx_preempt vmx_signal all: $(TEST_PROGS)
@@ -7,9 +7,11 @@ $(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec fpu_syscall: fpu_asm.S fpu_preempt: fpu_asm.S +fpu_signal: fpu_asm.S vmx_syscall: vmx_asm.S vmx_preempt: vmx_asm.S +vmx_signal: vmx_asm.S include ../../lib.mk
tools/testing/selftests/powerpc/math/fpu_signal.c new file +135 -0
135 lines not on this page · raw message
tools/testing/selftests/powerpc/math/vmx_signal.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.7.0
v3 cover FP/VEC/VSX switching optimisations
Cover-letter for V1 of the series is at
https://lists.ozlabs.org/pipermail/linuxppc-dev/2015-November/136350.html
Cover-letter for V2 of the series is at
https://lists.ozlabs.org/pipermail/linuxppc-dev/2016-January/138054.html
Changes in V3:
Addressed review comments from Michael Neuling
- Made commit message in 4/9 better reflect the patch
- Removed overuse of #ifdef blocks and redundant condition in 5/9
- Split 6/8 in two to better prepare for 7,8,9
- Removed #ifdefs in 6/9
Cyril Bur (9):
selftests/powerpc: Test the preservation of FPU and VMX regs across
syscall
selftests/powerpc: Test preservation of FPU and VMX regs across
preemption
selftests/powerpc: Test FPU and VMX regs in signal ucontext
powerpc: Explicitly disable math features when copying thread
powerpc: Restore FPU/VEC/VSX if previously used
powerpc: Prepare for splitting giveup_{fpu,altivec,vsx} in two
powerpc: Add the ability to save FPU without giving it up
powerpc: Add the ability to save Altivec without giving it up
powerpc: Add the ability to save VSX without giving it up
arch/powerpc/include/asm/processor.h | 2 + arch/powerpc/include/asm/switch_to.h | 13 +- arch/powerpc/kernel/asm-offsets.c | 2 + arch/powerpc/kernel/entry_64.S | 21 +- arch/powerpc/kernel/fpu.S | 25 +-- arch/powerpc/kernel/ppc_ksyms.c | 4 - arch/powerpc/kernel/process.c | 172 ++++++++++++++-- arch/powerpc/kernel/vector.S | 45 +--- tools/testing/selftests/powerpc/Makefile | 3 +- tools/testing/selftests/powerpc/basic_asm.h | 26 +++ tools/testing/selftests/powerpc/math/.gitignore | 6 + tools/testing/selftests/powerpc/math/Makefile | 19 ++ tools/testing/selftests/powerpc/math/fpu_asm.S | 195 ++++++++++++++++++ tools/testing/selftests/powerpc/math/fpu_preempt.c | 113 ++++++++++ tools/testing/selftests/powerpc/math/fpu_signal.c | 135 ++++++++++++ tools/testing/selftests/powerpc/math/fpu_syscall.c | 90 ++++++++ tools/testing/selftests/powerpc/math/vmx_asm.S | 229 +++++++++++++++++++++ tools/testing/selftests/powerpc/math/vmx_preempt.c | 113 ++++++++++ tools/testing/selftests/powerpc/math/vmx_signal.c | 138 +++++++++++++ tools/testing/selftests/powerpc/math/vmx_syscall.c | 92 +++++++++ 20 files changed, 1360 insertions(+), 83 deletions(-) create mode 100644 tools/testing/selftests/powerpc/basic_asm.h create mode 100644 tools/testing/selftests/powerpc/math/.gitignore create mode 100644 tools/testing/selftests/powerpc/math/Makefile create mode 100644 tools/testing/selftests/powerpc/math/fpu_asm.S create mode 100644 tools/testing/selftests/powerpc/math/fpu_preempt.c create mode 100644 tools/testing/selftests/powerpc/math/fpu_signal.c create mode 100644 tools/testing/selftests/powerpc/math/fpu_syscall.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_asm.S create mode 100644 tools/testing/selftests/powerpc/math/vmx_preempt.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_signal.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_syscall.c
Signature · 1 line
2.7.0
PATCH v3 2/9 selftests/powerpc: Test preservation of FPU and VMX regs across preemption
Loop in assembly checking the registers with many threads.
--- tools/testing/selftests/powerpc/math/.gitignore | 2 + tools/testing/selftests/powerpc/math/Makefile | 5 +- tools/testing/selftests/powerpc/math/fpu_asm.S | 34 +++++++ tools/testing/selftests/powerpc/math/fpu_preempt.c | 113 +++++++++++++++++++++ tools/testing/selftests/powerpc/math/vmx_asm.S | 44 +++++++- tools/testing/selftests/powerpc/math/vmx_preempt.c | 113 +++++++++++++++++++++ 6 files changed, 306 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/powerpc/math/fpu_preempt.c create mode 100644 tools/testing/selftests/powerpc/math/vmx_preempt.c
tools/testing/selftests/powerpc/math/.gitignore +2 -0
@@ -1,2 +1,4 @@ fpu_syscall vmx_syscall +fpu_preempt +vmx_preempt
tools/testing/selftests/powerpc/math/Makefile +4 -1
@@ -1,4 +1,4 @@ -TEST_PROGS := fpu_syscall vmx_syscall +TEST_PROGS := fpu_syscall fpu_preempt vmx_syscall vmx_preempt all: $(TEST_PROGS)
@@ -6,7 +6,10 @@ $(TEST_PROGS): ../harness.c $(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec fpu_syscall: fpu_asm.S +fpu_preempt: fpu_asm.S + vmx_syscall: vmx_asm.S +vmx_preempt: vmx_asm.S include ../../lib.mk
tools/testing/selftests/powerpc/math/fpu_asm.S +34 -0
@@ -159,3 +159,37 @@ FUNC_START(test_fpu) POP_BASIC_STACK(256) blr FUNC_END(test_fpu) + +#int preempt_fpu(double *darray, int *threads_running, int *running) +#On starting will (atomically) decrement not_ready as a signal that the FPU +#has been loaded with darray. Will proceed to check the validity of the FPU +#registers while running is not zero. +FUNC_START(preempt_fpu) + PUSH_BASIC_STACK(256) + std r3,32(sp) #double *darray + std r4,40(sp) #volatile int *not_ready + std r5,48(sp) #int *running + PUSH_FPU(56) + + bl load_fpu + + #Atomic DEC + ld r3,40(sp) +1: lwarx r4,0,r3 + addi r4,r4,-1 + stwcx. r4,0,r3 + bne- 1b + +2: ld r3, 32(sp) + bl check_fpu + cmpdi r3,0 + bne 3f + ld r4, 48(sp) + ld r5, 0(r4) + cmpwi r5,0 + bne 2b + +3: POP_FPU(56) + POP_BASIC_STACK(256) + blr +FUNC_END(preempt_fpu)
tools/testing/selftests/powerpc/math/fpu_preempt.c new file +113 -0
113 lines not on this page · raw message
tools/testing/selftests/powerpc/math/vmx_asm.S +40 -4
80 lines not on this page · raw message
tools/testing/selftests/powerpc/math/vmx_preempt.c new file +113 -0
113 lines not on this page · raw message
Signature · 1 line
2.7.0
Re: [PATCH v3 4/9] powerpc: Explicitly disable math features when copying thread
In reply to Cyril BurQuoted 24 lines · On Thu, 21 Jan 2016 11:55:44 +1100 Cyril Bur <[email protected]> wrote:
Currently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as
clone()andfork()to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace.
Signed-off-by Cyril Bur <[email protected]>--- arch/powerpc/kernel/process.c | 1 + 1 file changed, 1 insertion(+)arch/powerpc/kernel/process.c +0
-0
arch/powerpc/kernel/process.c int copy_thread(unsigned long clone_flags, unsigned long usp, · line 1310
Quoted 4 lines
arch/powerpc/kernel/process.c +1
-0@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp, f = ret_from_fork; } + childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX );
Ideally you want to use __msr_check_and_clear()
Basically we start with these bits off and then take an exception on use?
Quoted 3 lines
arch/powerpc/kernel/process.c +0
-0sp -=STACK_FRAME_OVERHEAD ;/*
Re: [PATCH v3 4/9] powerpc: Explicitly disable math features when copying thread
Quoted 28 lines · On Mon, 25 Jan 2016 11:04:23 +1100 Balbir Singh <[email protected]> wrote:
Quoted 25 lines · On Thu, 21 Jan 2016 11:55:44 +1100 Cyril Bur <[email protected]> wrote:
Currently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as
clone()andfork()to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace.
Signed-off-by Cyril Bur <[email protected]>--- arch/powerpc/kernel/process.c | 1 + 1 file changed, 1 insertion(+)arch/powerpc/kernel/process.c +0
-0@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
Quoted 4 lines
arch/powerpc/kernel/process.c int copy_thread(unsigned long clone_flags, unsigned long usp, · line 1310
Quoted 3 lines
arch/powerpc/kernel/process.c +1
-0f = ret_from_fork; } + childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX );
Hi Balbir,
Perhaps I'm missing something, are you saying
Quoted 2 lines
Ideally you want to use
__msr_check_and_clear()
instead of childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX); ? I don't see how that can work...
__msr_check_and_clear() operates on the currently active MSR, that is, the msr for the current kernel context. childregs->msr is the value that will be used for that userspace context when the kernel returns. Here we must ensure that that children are created with the bit disabled.
Quoted 2 lines
Basically we start with these bits off and then take an exception on use?
Currently yes, this is what I'm trying to change. This patch hasn't been necessary until now as any thread which saves its FPU/VMX/VSX data ALSO disables those bits in regs->msr and so theres no way a clone() or fork() can create a child with MSR_FP or MSR_VEC or MSR_VSX set. I add a meaning to 'having a regs->msr FP,VEC,VSX bit set' to mean that 'the regs are hot' in a subsequent patch which means this assumption no longer holds so now we must explicitly disable (so as to signal that the FPU/VMX/VSX regs are not hot) for children thread.
Sounds like I still haven't got that commit message quite right yet.
Quoted 4 lines
Quoted 3 lines
arch/powerpc/kernel/process.c +0
-0sp -=STACK_FRAME_OVERHEAD ;/*
Re: [PATCH v3 4/9] powerpc: Explicitly disable math features when copying thread
Quoted 43 lines · Cyril Bur
Quoted 28 lines · On Mon, 25 Jan 2016 11:04:23 +1100 Balbir Singh <[email protected]> wrote:
Quoted 25 lines · On Thu, 21 Jan 2016 11:55:44 +1100 Cyril Bur <[email protected]> wrote:
Currently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as
clone()andfork()to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace.
Signed-off-by Cyril Bur <[email protected]>--- arch/powerpc/kernel/process.c | 1 + 1 file changed, 1 insertion(+)arch/powerpc/kernel/process.c +0
-0@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,Quoted 4 lines
arch/powerpc/kernel/process.c int copy_thread(unsigned long clone_flags, unsigned long usp, · line 1310
Quoted 3 lines
arch/powerpc/kernel/process.c +1
-0f = ret_from_fork; } + childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX );Hi Balbir,
Perhaps I'm missing something, are you saying
Quoted 2 lines
Ideally you want to use
__msr_check_and_clear()
Quoted 8 lines
instead of
childregs->msr&= ~(MSR_FP|MSR_VEC|MSR_VSX); ? I don't see how that can work...
__msr_check_and_clear()operates on the currently active MSR, that is, the msr for the current kernel context.childregs->msris the value that will be used for that userspace context when the kernel returns. Here we must ensure that that children are created with the bit disabled.
Yes, my bad! I thought the routine took generic bits, hoping to reuse the CONFIG_VSX bits. I don't think it helps much, what you have is correct.
Quoted 12 lines
Quoted 2 lines
Basically we start with these bits off and then take an exception on use?
Currently yes, this is what I'm trying to change. This patch hasn't been necessary until now as any thread which saves its FPU/VMX/VSX data ALSO disables those bits in
regs->msrand so theres no way aclone()orfork()can create a child withMSR_FPorMSR_VECorMSR_VSXset. I add a meaning to 'having aregs->msrFP,VEC,VSX bit set' to mean that 'the regs are hot' in a subsequent patch which means this assumption no longer holds so now we must explicitly disable (so as to signal that the FPU/VMX/VSX regs are not hot) for children thread.
Quoted 1 line
Sounds like I still haven't got that commit message quite right yet.
I think the older series had more data to help understand the patch. It would help to move some of them to the current series
Balbir
Re: [PATCH v3 4/9] powerpc: Explicitly disable math features when copying thread
Quoted 69 lines · On Wed, 27 Jan 2016 23:01:59 +1100 Balbir Singh <[email protected]> wrote:
Quoted 43 lines · Cyril Bur
Quoted 28 lines · On Mon, 25 Jan 2016 11:04:23 +1100 Balbir Singh <[email protected]> wrote:
Quoted 25 lines · On Thu, 21 Jan 2016 11:55:44 +1100 Cyril Bur <[email protected]> wrote:
Currently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as
clone()andfork()to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace.
Signed-off-by Cyril Bur <[email protected]>--- arch/powerpc/kernel/process.c | 1 + 1 file changed, 1 insertion(+)arch/powerpc/kernel/process.c +0
-0@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,Quoted 4 lines
arch/powerpc/kernel/process.c int copy_thread(unsigned long clone_flags, unsigned long usp, · line 1310
Quoted 3 lines
arch/powerpc/kernel/process.c +1
-0f = ret_from_fork; } + childregs->msr &= ~(MSR_FP |MSR_VEC |MSR_VSX );Hi Balbir,
Perhaps I'm missing something, are you saying
Quoted 2 lines
Ideally you want to use
__msr_check_and_clear()Quoted 8 lines
instead of
childregs->msr&= ~(MSR_FP|MSR_VEC|MSR_VSX); ? I don't see how that can work...
__msr_check_and_clear()operates on the currently active MSR, that is, the msr for the current kernel context.childregs->msris the value that will be used for that userspace context when the kernel returns. Here we must ensure that that children are created with the bit disabled.Yes, my bad! I thought the routine took generic bits, hoping to reuse the
CONFIG_VSXbits. I don't think it helps much, what you have is correct.Quoted 12 lines
Quoted 2 lines
Basically we start with these bits off and then take an exception on use?
Currently yes, this is what I'm trying to change. This patch hasn't been necessary until now as any thread which saves its FPU/VMX/VSX data ALSO disables those bits in
regs->msrand so theres no way aclone()orfork()can create a child withMSR_FPorMSR_VECorMSR_VSXset. I add a meaning to 'having aregs->msrFP,VEC,VSX bit set' to mean that 'the regs are hot' in a subsequent patch which means this assumption no longer holds so now we must explicitly disable (so as to signal that the FPU/VMX/VSX regs are not hot) for children thread.
Quoted 5 lines
Quoted 1 line
Sounds like I still haven't got that commit message quite right yet.
I think the older series had more data to help understand the patch. It would help to move some of them to the current series
The previous commit message to this patch was:
With threads leaving the math bits enabled in their saved MSR to indicate that the hardware is hot and a restore is not needed, children need to turn it off as when they do get scheduled, there's no way their registers could have been hot.
Mikey pointed out, that was misleading as it wasn't clear that I was preparing for future work and that no bug currently exists.
Are you talking about another patch?
I have tried to incorporate more from the old commit message into the new one, see below:
Currently when threads get scheduled off they always giveup the FPU, Altivec (VMX) and Vector (VSX) units if they were using them. When they are scheduled back on a fault is then taken to enable each facility and load registers. As a result explicitly disabling FPU/VMX/VSX has not been necessary.
Future changes and optimisations remove this mandatory giveup and fault which could cause calls such as clone() and fork() to copy threads and run them later with FPU/VMX/VSX enabled but no registers loaded.
This patch starts the process of having MSR_{FP,VEC,VSX} mean that a threads registers are hot while not having MSR_{FP,VEC,VSX} means that the registers must be loaded. This allows for a smarter return to userspace. In the case of fork() and clone(), children must have their FPU/VMX/VSX facilities disabled as there is no way they could have been hot.
Mpe: Let me know if you want me to send this as a new series or if you're happy to incorporate that from here.
Cyril
Michael Ellerman
reviewerRe: [v3, 6/9] powerpc: Prepare for splitting giveup_{fpu, altivec, vsx} in two
In reply to Cyril BurQuoted 17 lines · Cyril Bur
This prepares for the decoupling of saving {fpu,altivec,vsx} registers and marking {fpu,altivec,vsx} as being unused by a thread.
Currently giveup_{fpu,altivec,vsx}() does both however optimisations to task switching can be made if these two operations are decoupled.
save_all()will permit the saving of registers to thread structs and leave threads MSR with bits enabled.This patch introduces no functional change.
arch/powerpc/kernel/process.c +0
-0@@ -349,6 +349,14 @@ void flush_spe_to_thread(struct task_struct *tsk)preempt_enable (); }
arch/powerpc/kernel/process.c void flush_spe_to_thread(struct task_struct *tsk) · line 360
Quoted 10 lines
arch/powerpc/kernel/process.c +8
-0} +#else +/* +* save_all() is going to test MSR_SPE, rather than pull in all the +* booke definitions all the time on a books kernel just ensure it exists +* but acts as a nop. +*/ + +#define MSR_SPE 0#endif /* CONFIG_SPE */
Building corenet32/64_defconfig this gives me:
arch/powerpc/kernel/process.c:392:0: error: "MSR_SPE" redefined [-Werror]
#define MSR_SPE 0
^
In file included from ./arch/powerpc/include/asm/reg.h:18:0,
from ./arch/powerpc/include/asm/processor.h:13,
from ./arch/powerpc/include/asm/thread_info.h:33,
from include/linux/thread_info.h:54,
from include/asm-generic/preempt.h:4,
from arch/powerpc/include/generated/asm/preempt.h:1,
from include/linux/preempt.h:59,
from include/linux/spinlock.h:50,
from include/linux/seqlock.h:35,
from include/linux/time.h:5,
from include/uapi/linux/timex.h:56,
from include/linux/timex.h:56,
from include/linux/sched.h:19,
from arch/powerpc/kernel/process.c:18:
./arch/powerpc/include/asm/reg_booke.h:33:0: note: this is the location of the previous definition
#define MSR_SPE __MASK(MSR_SPE_LG)
^
cc1: all warnings being treated as errors
scripts/Makefile.build:258: recipe for target 'arch/powerpc/kernel/process.o' failed make[1]: *** [arch/powerpc/kernel/process.o] Error 1
make[1]: *** Waiting for unfinished jobs....
Makefile:950: recipe for target 'arch/powerpc/kernel' failed
cheers
Michael Ellerman
reviewerRe: [v3, 1/9] selftests/powerpc: Test the preservation of FPU and VMX regs across syscall
In reply to Cyril Bur[offlist]
Quoted 5 lines · Cyril Bur
Test that the non volatile floating point and Altivec registers get correctly preserved across the
fork()syscall.
fork()works nicely for this purpose, the registers should be the same for both parent and child
...
Quoted 13 lines
tools/testing/selftests/powerpc/basic_asm.h new file +7
-0@@ -0,0 +1,26 @@ +#include <ppc-asm.h> +#include <asm/unistd.h> + +#define LOAD_REG_IMMEDIATE (reg,expr) \ + lis reg,(expr)@highest; \ + ori reg,reg,(expr)@higher; \ + rldicr reg,reg,32,31; \
tools/testing/selftests/powerpc/basic_asm.h line 17
Quoted 10 lines
tools/testing/selftests/powerpc/basic_asm.h new file +10
-0+ oris reg,reg,(expr)@high; \ + ori reg,reg,(expr)@l; + +#define PUSH_BASIC_STACK (size) \ + std 2,24(sp); \ + mflr r0; \ + std r0,16(sp); \ + mfcr r0; \ + stw r0,8(sp); \ + stdu sp,-size(sp);
Unless I'm completely misreading this, that's not ABI compliant.
You are supposed to save LR into your caller's stack frame, but the TOC and CR go into *your* stack frame.
I know that's not super obvious from the ABI doc.
See for example this code generated by GCC:
extern int global_thing;
int foo(int x, int (*fp)(int x))
{
int a, b, c;
a = x;
b = a + global_thing;
c = b + 2;
a = fp(x);
printf("%d %d %d\n", a, b, c);
return a;
}0000000000000000 <foo>:
0: 00 00 4c 3c addis r2,r12,0
0: R_PPC64_REL16_HA .TOC. 4: 00 00 42 38 addi r2,r2,0 4: R_PPC64_REL16_LO .TOC.+0x4 8: a6 02 08 7c mflr r0 c: f0 ff c1 fb std r30,-16(r1) <- save r30 into (what will be) our stack frame 10: f8 ff e1 fb std r31,-8(r1) <- save r31 into (what will be) our stack frame 14: 78 23 8c 7c mr r12,r4 <- put fp into r12 because we'll be calling its global entry point 18: a6 03 89 7c mtctr r4 <- put fp into CTR 1c: 00 00 22 3d addis r9,r2,0 1c: R_PPC64_TOC16_HA .toc 20: 00 00 29 e9 ld r9,0(r9) 20: R_PPC64_TOC16_LO_DS .toc 24: 10 00 01 f8 std r0,16(r1) <- save r0 (LR) into *our caller's* stack frame 28: 91 ff 21 f8 stdu r1,-112(r1) <- create our stack frame & store the backchain pointer 2c: 18 00 41 f8 std r2,24(r1) <- save r2 (TOC) into *our* stack frame 30: 00 00 e9 83 lwz r31,0(r9) 34: 14 1a ff 7f add r31,r31,r3 38: 21 04 80 4e bctrl <- call fp, it will get it's TOC pointer using r12 3c: 18 00 41 e8 ld r2,24(r1) <- restore our r2 (TOC) from *our* stack frame 40: 02 00 ff 38 addi r7,r31,2 44: b4 07 e6 7f extsw r6,r31 48: b4 07 e7 7c extsw r7,r7 4c: 78 1b 7e 7c mr r30,r3 50: 00 00 82 3c addis r4,r2,0 50: R_PPC64_TOC16_HA .rodata.str1.8 54: 78 f3 c5 7f mr r5,r30 58: 00 00 84 38 addi r4,r4,0 58: R_PPC64_TOC16_LO .rodata.str1.8 5c: 01 00 60 38 li r3,1 60: 01 00 00 48 bl 60 <foo+0x60> 60: R_PPC64_REL24 __printf_chk 64: 00 00 00 60 nop 68: 70 00 21 38 addi r1,r1,112 <- pop our stack frame 6c: 78 f3 c3 7f mr r3,r30 70: 10 00 01 e8 ld r0,16(r1) <- restore r0 (LR) from our caller's stack frame 74: f0 ff c1 eb ld r30,-16(r1) <- restore r30 from (what was) our stack frame 78: f8 ff e1 eb ld r31,-8(r1) <- restore r31 from (what was) our stack frame 7c: a6 03 08 7c mtlr r0 <- restore LR
80: 20 00 80 4e blr <- return
So your macro wants to be more like:
#define PUSH_BASIC_STACK(_size) \ mflr r0; \ std r0,16(r1); \ stdu r1,-_size(r1); \ mfcr r0; \ stw r0,8(r1); \ std r2,24(r1);
Though untested.
Also you're relying on the caller to ensure size is big enough. So maybe it'd be better if you ensured that, making the size parameter the *extra* size above the minimum stack frame. eg:
#define PUSH_BASIC_STACK(_extra) \ mflr r0; \ std r0,16(r1); \ stdu r1,-(_extra + 32)(r1); \ mfcr r0; \ stw r0,8(r1); \ std r2,24(r1);
cheers