Skip to contentSkip to conversation

powerpc: inline current_stack_pointer()

PATCH In review Christophe Leroy 5 participants 7 messages May 23, 2016 – May 31, 2016 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: inline current_stack_pointer()

current_stack_pointeur() is a single instruction function. it It is not worth breaking the execution flow with a bl/blr for a single instruction

---
 arch/powerpc/include/asm/reg.h  | 7 ++++++-
 arch/powerpc/kernel/misc.S      | 4 ----
 arch/powerpc/kernel/ppc_ksyms.c | 2 --
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/reg.h +6 -1
index c1e82e9..7ce6777 100644
@@ -1301,7 +1301,12 @@ static inline unsigned long mfvtb (void)
 
 #define proc_trap()	asm volatile("trap")
 
-extern unsigned long current_stack_pointer(void);
+static inline unsigned long current_stack_pointer(void)
+{
+	register unsigned long *ptr asm("r1");
+
+	return *ptr;
+}
 
 extern unsigned long scom970_read(unsigned int address);
 extern void scom970_write(unsigned int address, unsigned long value);
arch/powerpc/kernel/misc.S +0 -4
index 0d43219..7ce26d4 100644
@@ -114,7 +114,3 @@ _GLOBAL(longjmp)
 	mtlr	r0
 	mr	r3,r4
 	blr
-
-_GLOBAL(current_stack_pointer)
-	PPC_LL	r3,0(r1)
-	blr
arch/powerpc/kernel/ppc_ksyms.c +0 -2
index 9f01e28..eb5c5dc 100644
@@ -33,5 +33,3 @@ EXPORT_SYMBOL(store_vr_state);
 #ifdef CONFIG_EPAPR_PARAVIRT
 EXPORT_SYMBOL(epapr_hypercall_start);
 #endif
-
-EXPORT_SYMBOL(current_stack_pointer);
Signature · 1 line
2.1.0
#1 lore raw

arch/powerpc/include/asm/reg.h static inline unsigned long mfvtb (void) · line 1309

Quoted 6 lines · Christophe Leroy
arch/powerpc/include/asm/reg.h +6 -0
+static inline unsigned long current_stack_pointer(void)
+{
+	register unsigned long *ptr asm("r1");
+
+	return *ptr;
+}

Register asm is only guaranteed to work as input to inline asm. NAK.

Segher

#2 lore raw
Quoted 9 lines · Segher Boessenkool

arch/powerpc/include/asm/reg.h static inline unsigned long mfvtb (void) · line 1309

Quoted 6 lines · Christophe Leroy
arch/powerpc/include/asm/reg.h +6 -0
+static inline unsigned long current_stack_pointer(void)
+{
+	register unsigned long *ptr asm("r1");
+
+	return *ptr;
+}

Register asm is only guaranteed to work as input to inline asm. NAK.

Does it mean that the following declaration in arch/powerpc/include/asm/paca.h is wrong too ?

register struct paca_struct *local_paca asm("r13");

Christophe

#3 lore raw
Quoted 8 lines · Christophe Leroy
Quoted 8 lines

arch/powerpc/include/asm/reg.h static inline unsigned long mfvtb (void) · line 1309

Quoted 6 lines
arch/powerpc/include/asm/reg.h +6 -0
+static inline unsigned long current_stack_pointer(void)
+{
+	register unsigned long *ptr asm("r1");
+
+	return *ptr;
+}

Register asm is only guaranteed to work as input to inline asm. NAK.

Quoted 4 lines

Does it mean that the following declaration in
arch/powerpc/include/asm/paca.h is wrong too ?

register struct paca_struct *local_paca asm("r13");

That one is fine, because it is a global var.

https://gcc.gnu.org/onlinedocs/gcc/Explicit-Register-Variables.html

Segher

#4 lore raw
In reply to Christophe Leroy
Quoted 3 lines · Christophe Leroy

current_stack_pointeur() is a single instruction function. it It is not worth breaking the execution flow with a bl/blr for a single instruction

Are you sure that the result is always the same?

Calling an external function prevents the compiler from considering the caller of of current_stack_pointer a leaf function, which certainly does not help the compiler, but in a leaf function the compiler is free not to establish a new frame.

If the compiler decides to establishes a new frame (typically with "stwu r1,-frame_size(r1)"), *r1 is the previous stack pointer, or the caller's stack pointer, or the current function frame pointer if I remember correctly the ABI definitions.

However, if the compiler decides that it can get away without a frame for the function, *r1 is the stack pointer of the caller's caller.

Depending on the application, this may or may not be important.

By the way, isn't there a GCC builtin which can perform this task, for example builtin_frame_address()?

Gabriel

Quoted 54 lines
---
 arch/powerpc/include/asm/reg.h  | 7 ++++++-
 arch/powerpc/kernel/misc.S      | 4 ----
 arch/powerpc/kernel/ppc_ksyms.c | 2 --
 3 files changed, 6 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/reg.h +6 -1
index c1e82e9..7ce6777 100644
@@ -1301,7 +1301,12 @@ static inline unsigned long mfvtb (void)
 
 #define proc_trap()	asm volatile("trap")
 
-extern unsigned long current_stack_pointer(void);
+static inline unsigned long current_stack_pointer(void)
+{
+	register unsigned long *ptr asm("r1");
+
+	return *ptr;
+}
 
 extern unsigned long scom970_read(unsigned int address);
 extern void scom970_write(unsigned int address, unsigned long value);
arch/powerpc/kernel/misc.S +0 -4
index 0d43219..7ce26d4 100644
@@ -114,7 +114,3 @@ _GLOBAL(longjmp)
 	mtlr	r0
 	mr	r3,r4
 	blr
-
-_GLOBAL(current_stack_pointer)
-	PPC_LL	r3,0(r1)
-	blr
arch/powerpc/kernel/ppc_ksyms.c +0 -2
index 9f01e28..eb5c5dc 100644
@@ -33,5 +33,3 @@ EXPORT_SYMBOL(store_vr_state);
 #ifdef CONFIG_EPAPR_PARAVIRT
 EXPORT_SYMBOL(epapr_hypercall_start);
 #endif
-
-EXPORT_SYMBOL(current_stack_pointer);
Signature · 5 lines
2.1.0
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
#5 lore raw
Quoted 17 lines · Gabriel Paubert
Quoted 3 lines · Christophe Leroy

current_stack_pointeur() is a single instruction function. it It is not worth breaking the execution flow with a bl/blr for a single instruction

Are you sure that the result is always the same?

Calling an external function prevents the compiler from considering the caller of of current_stack_pointer a leaf function, which certainly does not help the compiler, but in a leaf function the compiler is free not to establish a new frame.

If the compiler decides to establishes a new frame (typically with "stwu r1,-frame_size(r1)"), *r1 is the previous stack pointer, or the caller's stack pointer, or the current function frame pointer if I remember correctly the ABI definitions.

Quoted 4 lines

However, if the compiler decides that it can get away without a frame for the function, *r1 is the stack pointer of the caller's caller.

Depending on the application, this may or may not be important.

Right. I think I wrote the original current_stack_pointer() implementation, and that I deliberately didn't make it an inline so that the caller would have to establish its own stack frame, and thus its stack pointer value would be a well-defined thing.

Paul.

#6 lore raw
In reply to Christophe Leroy

Hi,

Quoted 3 lines

current_stack_pointeur() is a single instruction function. it It is not worth breaking the execution flow with a bl/blr for a single instruction

Check out bfe9a2cfe91a ("powerpc: Reimplement __get_SP() as a function not a define") to see why we made it a function.

Anton