Skip to contentSkip to conversation

FP/VEC/VSX switching optimisations

PATCH Superseded Cyril Bur 3 participants 14 messages Feb 15, 2016 – Feb 16, 2016

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Cyril Bur

author
#0 lore raw

v4 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

Changes in V4:
- Addressed non ABI compliant ASM macros in 1/9
- Fixed build breakage due to changing #ifdefs in V3 (6/9)
- Reordered some conditions in if statements

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/reg.h                     |   8 +
 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                      | 168 +++++++++++++--
 arch/powerpc/kernel/vector.S                       |  45 +---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  30 +++
 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 +++++++++
 21 files changed, 1368 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.1
author
#1 lore raw

PATCH v4 1/9 selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

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

---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  30 ++++
 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, 584 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
index 0c2706b..19e8191 100644
@@ -22,7 +22,8 @@ SUB_DIRS = benchmarks 		\
 	   switch_endian	\
 	   syscalls		\
 	   tm			\
-	   vphn
+	   vphn         \
+	   math
 
 endif
 
tools/testing/selftests/powerpc/basic_asm.h new file +30 -0
new file mode 100644
index 0000000..f243da0
@@ -0,0 +1,30 @@
+#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;
+
+/* It is very important to note here that _extra is the extra amount of
+ * stack space needed.
+ * This space must be accessed at sp + 32!
+ */
+#define PUSH_BASIC_STACK(_extra) \
+	mflr	r0; \
+	std	r0,16(sp); \
+	stdu	sp,-(_extra + 32)(sp); \
+	mfcr	r0; \
+	stw	r0,8(sp); \
+	std	2,24(sp);
+
+#define POP_BASIC_STACK(_extra) \
+	ld	2,24(sp); \
+	lwz	r0,8(sp); \
+	mtcr	r0; \
+	addi	sp,sp,(_extra + 32); \
+	ld	r0,16(sp); \
+	mtlr	r0;
+
tools/testing/selftests/powerpc/math/.gitignore new file +2 -0
new file mode 100644
index 0000000..b19b269
@@ -0,0 +1,2 @@
+fpu_syscall
+vmx_syscall
tools/testing/selftests/powerpc/math/Makefile new file +14 -0
new file mode 100644
index 0000000..418bef1
@@ -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.1
#2 lore raw

Re: [PATCH v4 1/9] selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

Quoted 48 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

---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  30 ++++
 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, 584 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
index 0c2706b..19e8191 100644
@@ -22,7 +22,8 @@ SUB_DIRS = benchmarks 		\
 	   switch_endian	\
 	   syscalls		\
 	   tm			\
-	   vphn
+	   vphn         \
+	   math
 
 endif
 
tools/testing/selftests/powerpc/basic_asm.h new file +3 -0
new file mode 100644
index 0000000..f243da0
@@ -0,0 +1,30 @@
+#include <ppc-asm.h>
+#include <asm/unistd.h>
+

tools/testing/selftests/powerpc/basic_asm.h line 13

Quoted 10 lines
tools/testing/selftests/powerpc/basic_asm.h new file +10 -0
+#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;
+
+/* It is very important to note here that _extra is the extra amount of
+ * stack space needed.
+ * This space must be accessed at sp + 32!

This looks to be specific to ABIv2. Is this series limited to ppc64le? If so, you might want to ensure this only builds there.

Also:
#define PPC_ABIV2_MIN_STACK_SIZE 32

or just:
#define PPC_MIN_STACK 32

... is helpful. And, you might want to base the rest of your code that use PUSH_BASIC_STACK() on that. If we ever want to have these tests run anywhere else, that'll help a lot. (See further below)

tools/testing/selftests/powerpc/basic_asm.h line 21

Quoted 8 lines
tools/testing/selftests/powerpc/basic_asm.h new file +8 -0
+ */
+#define PUSH_BASIC_STACK(_extra) \
+	mflr	r0; \
+	std	r0,16(sp); \
+	stdu	sp,-(_extra + 32)(sp); \
+	mfcr	r0; \
+	stw	r0,8(sp); \
+	std	2,24(sp);
		^^

Better to use r2 here and below.

Quoted 174 lines
tools/testing/selftests/powerpc/basic_asm.h new file +9 -0
+
+#define POP_BASIC_STACK(_extra) \
+	ld	2,24(sp); \
+	lwz	r0,8(sp); \
+	mtcr	r0; \
+	addi	sp,sp,(_extra + 32); \
+	ld	r0,16(sp); \
+	mtlr	r0;
+
tools/testing/selftests/powerpc/math/.gitignore new file +2 -0
new file mode 100644
index 0000000..b19b269
@@ -0,0 +1,2 @@
+fpu_syscall
+vmx_syscall
tools/testing/selftests/powerpc/math/Makefile new file +14 -0
new file mode 100644
index 0000000..418bef1
@@ -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 +131 -0
new file mode 100644
index 0000000..8733874
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2015, Cyril Bur, IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include "../basic_asm.h"
+
+#define PUSH_FPU(pos) \
+	stfd	f14,pos(sp); \
+	stfd	f15,pos+8(sp); \
+	stfd	f16,pos+16(sp); \
+	stfd	f17,pos+24(sp); \
+	stfd	f18,pos+32(sp); \
+	stfd	f19,pos+40(sp); \
+	stfd	f20,pos+48(sp); \
+	stfd	f21,pos+56(sp); \
+	stfd	f22,pos+64(sp); \
+	stfd	f23,pos+72(sp); \
+	stfd	f24,pos+80(sp); \
+	stfd	f25,pos+88(sp); \
+	stfd	f26,pos+96(sp); \
+	stfd	f27,pos+104(sp); \
+	stfd	f28,pos+112(sp); \
+	stfd	f29,pos+120(sp); \
+	stfd	f30,pos+128(sp); \
+	stfd	f31,pos+136(sp);
+
+#define POP_FPU(pos) \
+	lfd	f14,pos(sp); \
+	lfd	f15,pos+8(sp); \
+	lfd	f16,pos+16(sp); \
+	lfd	f17,pos+24(sp); \
+	lfd	f18,pos+32(sp); \
+	lfd	f19,pos+40(sp); \
+	lfd	f20,pos+48(sp); \
+	lfd	f21,pos+56(sp); \
+	lfd	f22,pos+64(sp); \
+	lfd	f23,pos+72(sp); \
+	lfd	f24,pos+80(sp); \
+	lfd	f25,pos+88(sp); \
+	lfd	f26,pos+96(sp); \
+	lfd	f27,pos+104(sp); \
+	lfd	f28,pos+112(sp); \
+	lfd	f29,pos+120(sp); \
+	lfd	f30,pos+128(sp); \
+	lfd	f31,pos+136(sp);
+
+#Careful calling this, it will 'clobber' fpu (by design)
+#Don't call this from C
+FUNC_START(load_fpu)
+	lfd	f14,0(r3)
+	lfd	f15,8(r3)
+	lfd	f16,16(r3)
+	lfd	f17,24(r3)
+	lfd	f18,32(r3)
+	lfd	f19,40(r3)
+	lfd	f20,48(r3)
+	lfd	f21,56(r3)
+	lfd	f22,64(r3)
+	lfd	f23,72(r3)
+	lfd	f24,80(r3)
+	lfd	f25,88(r3)
+	lfd	f26,96(r3)
+	lfd	f27,104(r3)
+	lfd	f28,112(r3)
+	lfd	f29,120(r3)
+	lfd	f30,128(r3)
+	lfd	f31,136(r3)
+	blr
+FUNC_END(load_fpu)
+
+FUNC_START(check_fpu)
+	mr r4,r3
+	li	r3,1 #assume a bad result
+	lfd	f0,0(r4)
+	fcmpu	cr1,f0,f14
+	bne	cr1,1f
+	lfd	f0,8(r4)
+	fcmpu	cr1,f0,f15
+	bne	cr1,1f
+	lfd	f0,16(r4)
+	fcmpu	cr1,f0,f16
+	bne	cr1,1f
+	lfd	f0,24(r4)
+	fcmpu	cr1,f0,f17
+	bne	cr1,1f
+	lfd	f0,32(r4)
+	fcmpu	cr1,f0,f18
+	bne	cr1,1f
+	lfd	f0,40(r4)
+	fcmpu	cr1,f0,f19
+	bne	cr1,1f
+	lfd	f0,48(r4)
+	fcmpu	cr1,f0,f20
+	bne	cr1,1f
+	lfd	f0,56(r4)
+	fcmpu	cr1,f0,f21
+	bne	cr1,1f
+	lfd	f0,64(r4)
+	fcmpu	cr1,f0,f22
+	bne	cr1,1f
+	lfd	f0,72(r4)
+	fcmpu	cr1,f0,f23
+	bne	cr1,1f
+	lfd	f0,80(r4)
+	fcmpu	cr1,f0,f24
+	bne	cr1,1f
+	lfd	f0,88(r4)
+	fcmpu	cr1,f0,f25
+	bne	cr1,1f
+	lfd	f0,96(r4)
+	fcmpu	cr1,f0,f26
+	bne	cr1,1f
+	lfd	f0,104(r4)
+	fcmpu	cr1,f0,f27
+	bne	cr1,1f
+	lfd	f0,112(r4)
+	fcmpu	cr1,f0,f28
+	bne	cr1,1f
+	lfd	f0,120(r4)
+	fcmpu	cr1,f0,f29
+	bne	cr1,1f
+	lfd	f0,128(r4)
+	fcmpu	cr1,f0,f30
+	bne	cr1,1f
+	lfd	f0,136(r4)
+	fcmpu	cr1,f0,f31

tools/testing/selftests/powerpc/math/fpu_asm.S line 141

Quoted 10 lines
tools/testing/selftests/powerpc/math/fpu_asm.S new file +10 -0
+	bne	cr1,1f
+	li	r3,0 #Sucess!!!
+1:	blr
+
+FUNC_START(test_fpu)
+	#r3 holds pointer to where to put the result of fork
+	#r4 holds pointer to the pid
+	#f14-f31 are non volatiles
+	PUSH_BASIC_STACK(256)
+	std	r3,40(sp) #Address of darray

So, this could be:

	PUSH_BASIC_STACK(256)
	std	r3,PPC_MIN_STACK+8(sp)

... though I wonder why there is +8 here?

- Naveen

author
#3 lore raw

Re: [PATCH v4 1/9] selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

Quoted 51 lines · On Mon, 15 Feb 2016 22:29:17 +0530 "Naveen N. Rao"…
Quoted 50 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

---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  30 ++++
 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, 584 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
index 0c2706b..19e8191 100644
@@ -22,7 +22,8 @@ SUB_DIRS = benchmarks 		\
 	   switch_endian	\
 	   syscalls		\
 	   tm			\
-	   vphn
+	   vphn         \
+	   math
 
 endif
 
tools/testing/selftests/powerpc/basic_asm.h new file +5 -0
new file mode 100644
index 0000000..f243da0
@@ -0,0 +1,30 @@
+#include <ppc-asm.h>
+#include <asm/unistd.h>
+
+#define LOAD_REG_IMMEDIATE(reg,expr) \
+	lis	reg,(expr)@highest;	\
Quoted 9 lines

tools/testing/selftests/powerpc/basic_asm.h line 13

Quoted 8 lines
tools/testing/selftests/powerpc/basic_asm.h new file +8 -0
+	ori	reg,reg,(expr)@higher;	\
+	rldicr	reg,reg,32,31;	\
+	oris	reg,reg,(expr)@high;	\
+	ori	reg,reg,(expr)@l;
+
+/* It is very important to note here that _extra is the extra amount of
+ * stack space needed.
+ * This space must be accessed at sp + 32!  

Hi Naveen,

Thanks for the review.

Quoted 3 lines

This looks to be specific to ABIv2. Is this series limited to ppc64le? If so, you might want to ensure this only builds there.

Is ABIv1 still in use? Can we still compile for v1?

This is for series 64bit only, I've not really got any reason to believe this is LE only, shouldn't this work BE? The makefile enforces 64bit, I believe it is ok for kernel selftests to fail to compile if they aren't going to be able to run.

Quoted 10 lines

Also:
#define PPC_ABIV2_MIN_STACK_SIZE 32

or just:
#define PPC_MIN_STACK 32

... is helpful. And, you might want to base the rest of your code that use PUSH_BASIC_STACK() on that. If we ever want to have these tests run anywhere else, that'll help a lot. (See further below)

So I thought about it. I agree that it would be nice, I just worry that I might get rabbitholed, I can see it going further and then providing stack accessors to abstract out even PPC_MIN_STACK except in a bunch of macros, and that's when I know I've gone too far.

Perhaps I could look at adding this when I write more tests, I have grand plans to push way more tests.

Quoted 11 lines

tools/testing/selftests/powerpc/basic_asm.h line 21

Quoted 8 lines
tools/testing/selftests/powerpc/basic_asm.h new file +8 -0
+ */
+#define PUSH_BASIC_STACK(_extra) \
+	mflr	r0; \
+	std	r0,16(sp); \
+	stdu	sp,-(_extra + 32)(sp); \
+	mfcr	r0; \
+	stw	r0,8(sp); \
+	std	2,24(sp);  
		^^

Better to use r2 here and below.

I think the reason I used '2' is that 'r2' isn't actually defined in ppc-asm.h for userspace, due to conventions, like 'sp', 'toc' has been used. So I could have used 'toc' but then there was an issue with toc NOT being defined, or getting undefined in some situations.

Quoted 186 lines
Quoted 174 lines
tools/testing/selftests/powerpc/basic_asm.h new file +9 -0
+
+#define POP_BASIC_STACK(_extra) \
+	ld	2,24(sp); \
+	lwz	r0,8(sp); \
+	mtcr	r0; \
+	addi	sp,sp,(_extra + 32); \
+	ld	r0,16(sp); \
+	mtlr	r0;
+
tools/testing/selftests/powerpc/math/.gitignore new file +2 -0
new file mode 100644
index 0000000..b19b269
@@ -0,0 +1,2 @@
+fpu_syscall
+vmx_syscall
tools/testing/selftests/powerpc/math/Makefile new file +14 -0
new file mode 100644
index 0000000..418bef1
@@ -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 +131 -0
new file mode 100644
index 0000000..8733874
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2015, Cyril Bur, IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include "../basic_asm.h"
+
+#define PUSH_FPU(pos) \
+	stfd	f14,pos(sp); \
+	stfd	f15,pos+8(sp); \
+	stfd	f16,pos+16(sp); \
+	stfd	f17,pos+24(sp); \
+	stfd	f18,pos+32(sp); \
+	stfd	f19,pos+40(sp); \
+	stfd	f20,pos+48(sp); \
+	stfd	f21,pos+56(sp); \
+	stfd	f22,pos+64(sp); \
+	stfd	f23,pos+72(sp); \
+	stfd	f24,pos+80(sp); \
+	stfd	f25,pos+88(sp); \
+	stfd	f26,pos+96(sp); \
+	stfd	f27,pos+104(sp); \
+	stfd	f28,pos+112(sp); \
+	stfd	f29,pos+120(sp); \
+	stfd	f30,pos+128(sp); \
+	stfd	f31,pos+136(sp);
+
+#define POP_FPU(pos) \
+	lfd	f14,pos(sp); \
+	lfd	f15,pos+8(sp); \
+	lfd	f16,pos+16(sp); \
+	lfd	f17,pos+24(sp); \
+	lfd	f18,pos+32(sp); \
+	lfd	f19,pos+40(sp); \
+	lfd	f20,pos+48(sp); \
+	lfd	f21,pos+56(sp); \
+	lfd	f22,pos+64(sp); \
+	lfd	f23,pos+72(sp); \
+	lfd	f24,pos+80(sp); \
+	lfd	f25,pos+88(sp); \
+	lfd	f26,pos+96(sp); \
+	lfd	f27,pos+104(sp); \
+	lfd	f28,pos+112(sp); \
+	lfd	f29,pos+120(sp); \
+	lfd	f30,pos+128(sp); \
+	lfd	f31,pos+136(sp);
+
+#Careful calling this, it will 'clobber' fpu (by design)
+#Don't call this from C
+FUNC_START(load_fpu)
+	lfd	f14,0(r3)
+	lfd	f15,8(r3)
+	lfd	f16,16(r3)
+	lfd	f17,24(r3)
+	lfd	f18,32(r3)
+	lfd	f19,40(r3)
+	lfd	f20,48(r3)
+	lfd	f21,56(r3)
+	lfd	f22,64(r3)
+	lfd	f23,72(r3)
+	lfd	f24,80(r3)
+	lfd	f25,88(r3)
+	lfd	f26,96(r3)
+	lfd	f27,104(r3)
+	lfd	f28,112(r3)
+	lfd	f29,120(r3)
+	lfd	f30,128(r3)
+	lfd	f31,136(r3)
+	blr
+FUNC_END(load_fpu)
+
+FUNC_START(check_fpu)
+	mr r4,r3
+	li	r3,1 #assume a bad result
+	lfd	f0,0(r4)
+	fcmpu	cr1,f0,f14
+	bne	cr1,1f
+	lfd	f0,8(r4)
+	fcmpu	cr1,f0,f15
+	bne	cr1,1f
+	lfd	f0,16(r4)
+	fcmpu	cr1,f0,f16
+	bne	cr1,1f
+	lfd	f0,24(r4)
+	fcmpu	cr1,f0,f17
+	bne	cr1,1f
+	lfd	f0,32(r4)
+	fcmpu	cr1,f0,f18
+	bne	cr1,1f
+	lfd	f0,40(r4)
+	fcmpu	cr1,f0,f19
+	bne	cr1,1f
+	lfd	f0,48(r4)
+	fcmpu	cr1,f0,f20
+	bne	cr1,1f
+	lfd	f0,56(r4)
+	fcmpu	cr1,f0,f21
+	bne	cr1,1f
+	lfd	f0,64(r4)
+	fcmpu	cr1,f0,f22
+	bne	cr1,1f
+	lfd	f0,72(r4)
+	fcmpu	cr1,f0,f23
+	bne	cr1,1f
+	lfd	f0,80(r4)
+	fcmpu	cr1,f0,f24
+	bne	cr1,1f
+	lfd	f0,88(r4)
+	fcmpu	cr1,f0,f25
+	bne	cr1,1f
+	lfd	f0,96(r4)
+	fcmpu	cr1,f0,f26
+	bne	cr1,1f
+	lfd	f0,104(r4)
+	fcmpu	cr1,f0,f27
+	bne	cr1,1f
+	lfd	f0,112(r4)
+	fcmpu	cr1,f0,f28
+	bne	cr1,1f
+	lfd	f0,120(r4)
+	fcmpu	cr1,f0,f29
+	bne	cr1,1f
+	lfd	f0,128(r4)
+	fcmpu	cr1,f0,f30
+	bne	cr1,1f
+	lfd	f0,136(r4)
+	fcmpu	cr1,f0,f31

tools/testing/selftests/powerpc/math/fpu_asm.S line 141

Quoted 10 lines
tools/testing/selftests/powerpc/math/fpu_asm.S new file +10 -0
+	bne	cr1,1f
+	li	r3,0 #Sucess!!!
+1:	blr
+
+FUNC_START(test_fpu)
+	#r3 holds pointer to where to put the result of fork
+	#r4 holds pointer to the pid
+	#f14-f31 are non volatiles
+	PUSH_BASIC_STACK(256)
+	std	r3,40(sp) #Address of darray  

So, this could be:

Quoted 5 lines
	PUSH_BASIC_STACK(256)
	std	r3,PPC_MIN_STACK+8(sp)

... though I wonder why there is +8 here?

I think the +8 is left over from my using +0 for something else and then not and not going back and being all neat about stack usage. Admittedly I didn't look over that too hard it being a selftest and all, I'm not sure optimal stack usage is super important here.

Thanks,

Cyril

Quoted 3 lines

- Naveen

#4 lore raw

Re: [PATCH v4 1/9] selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

Quoted 12 lines · Cyril Bur
Quoted 9 lines · On Mon, 15 Feb 2016 22:29:17 +0530 "Naveen N. Rao"…
Quoted 8 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

---

<snip>

Quoted 8 lines
Quoted 5 lines

tools/testing/selftests/powerpc/basic_asm.h line 13

Quoted 4 lines
tools/testing/selftests/powerpc/basic_asm.h new file +4 -0
+
+/* It is very important to note here that _extra is the extra amount of
+ * stack space needed.
+ * This space must be accessed at sp + 32!  

Hi Naveen,

Quoted 7 lines

Thanks for the review.

Quoted 3 lines

This looks to be specific to ABIv2. Is this series limited to ppc64le? If so, you might want to ensure this only builds there.

Is ABIv1 still in use? Can we still compile for v1?

Yes, that's the earlier ppc64 BE (I'm assuming these tests can be run when booted in LPARs as well)

Quoted 3 lines

This is for series 64bit only, I've not really got any reason to believe this is LE only, shouldn't this work BE? The makefile enforces 64bit, I believe it is

This won't work for ABIv1 BE since the stack setup is a bit different. I think your patches assume that 32 bytes is the minimum stack size, but that's only for ABIv2. Also, the locations of CR and TOC save areas on the stack are quite different:

http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html#STACK

Quoted 14 lines

ok for kernel selftests to fail to compile if they aren't going to be able to run.

Quoted 10 lines

Also:
#define PPC_ABIV2_MIN_STACK_SIZE 32

or just:
#define PPC_MIN_STACK 32

... is helpful. And, you might want to base the rest of your code that use PUSH_BASIC_STACK() on that. If we ever want to have these tests run anywhere else, that'll help a lot. (See further below)

Quoted 7 lines

So I thought about it. I agree that it would be nice, I just worry that I might get rabbitholed, I can see it going further and then providing stack accessors to abstract out even PPC_MIN_STACK except in a bunch of macros, and that's when I know I've gone too far.

Perhaps I could look at adding this when I write more tests, I have grand plans to push way more tests.

Sure - just that if you ever intend to have these for ABIv1, it will be way easier to put together macros now rather than later.

- Naveen

#5 lore raw

Re: [PATCH v4 1/9] selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

In reply to Cyril Bur
Quoted 33 lines · Cyril Bur
Quoted 18 lines · On Mon, 15 Feb 2016 22:29:17 +0530 "Naveen N. Rao"…
Quoted 17 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

tools/testing/selftests/powerpc/basic_asm.h new file +5 -0
new file mode 100644
index 0000000..f243da0
@@ -0,0 +1,30 @@
+#include <ppc-asm.h>
+#include <asm/unistd.h>
+
+#define LOAD_REG_IMMEDIATE(reg,expr) \
+	lis	reg,(expr)@highest;	\
Quoted 9 lines

tools/testing/selftests/powerpc/basic_asm.h line 13

Quoted 8 lines
tools/testing/selftests/powerpc/basic_asm.h new file +8 -0
+	ori	reg,reg,(expr)@higher;	\
+	rldicr	reg,reg,32,31;	\
+	oris	reg,reg,(expr)@high;	\
+	ori	reg,reg,(expr)@l;
+
+/* It is very important to note here that _extra is the extra amount of
+ * stack space needed.
+ * This space must be accessed at sp + 32!  

Hi Naveen,

Quoted 7 lines

Thanks for the review.

Quoted 3 lines

This looks to be specific to ABIv2. Is this series limited to ppc64le? If so, you might want to ensure this only builds there.

Is ABIv1 still in use? Can we still compile for v1?

YES! >:E

Quoted 16 lines

This is for series 64bit only, I've not really got any reason to believe this is LE only, shouldn't this work BE? The makefile enforces 64bit, I believe it is ok for kernel selftests to fail to compile if they aren't going to be able to run.

Quoted 10 lines

Also:
#define PPC_ABIV2_MIN_STACK_SIZE 32

or just:
#define PPC_MIN_STACK 32

... is helpful. And, you might want to base the rest of your code that use PUSH_BASIC_STACK() on that. If we ever want to have these tests run anywhere else, that'll help a lot. (See further below)

Quoted 7 lines

So I thought about it. I agree that it would be nice, I just worry that I might get rabbitholed, I can see it going further and then providing stack accessors to abstract out even PPC_MIN_STACK except in a bunch of macros, and that's when I know I've gone too far.

Perhaps I could look at adding this when I write more tests, I have grand plans to push way more tests.

You definitely need a #define for the minimum stack frame size, based on the ABI version. You can basically do what the kernel does for STACK_FRAME_MIN_SIZE.

You also need to cope with the TOC save slot moving between ABIv1 & 2, which shouldn't be hard with a macro for it.

Quoted 12 lines
Quoted 11 lines

tools/testing/selftests/powerpc/basic_asm.h line 21

Quoted 8 lines
tools/testing/selftests/powerpc/basic_asm.h new file +8 -0
+ */
+#define PUSH_BASIC_STACK(_extra) \
+	mflr	r0; \
+	std	r0,16(sp); \
+	stdu	sp,-(_extra + 32)(sp); \
+	mfcr	r0; \
+	stw	r0,8(sp); \
+	std	2,24(sp);  
		^^

Better to use r2 here and below.

Quoted 4 lines

I think the reason I used '2' is that 'r2' isn't actually defined in ppc-asm.h for userspace, due to conventions, like 'sp', 'toc' has been used. So I could have used 'toc' but then there was an issue with toc NOT being defined, or getting undefined in some situations.

That's true, ppc-asm.h doesn't define r2, instead it defines toc.

But you can always use %r2, which is preferable to 2 IMHO.

Personally I'd rather you use %r1 than sp, but I won't make you. As someone who has read lots of powerpc assembler %r1 translates as "stack pointer" where as "sp" translates as "huh?".

Quoted 11 lines
Quoted 11 lines

tools/testing/selftests/powerpc/basic_asm.h line 27

Quoted 6 lines
tools/testing/selftests/powerpc/basic_asm.h new file +6 -0
+FUNC_START(test_fpu)
+	#r3 holds pointer to where to put the result of fork
+	#r4 holds pointer to the pid
+	#f14-f31 are non volatiles
+	PUSH_BASIC_STACK(256)
+	std	r3,40(sp) #Address of darray  

So, this could be:

	PUSH_BASIC_STACK(256)
	std	r3,PPC_MIN_STACK+8(sp)
Quoted 6 lines
Quoted 1 line

... though I wonder why there is +8 here?

I think the +8 is left over from my using +0 for something else and then not and not going back and being all neat about stack usage. Admittedly I didn't look over that too hard it being a selftest and all, I'm not sure optimal stack usage is super important here.

The first free slot is at PPC_MIN_STACK(%r1), so that's what you should use.

cheers

author
#6 lore raw

PATCH v4 2/9 selftests/powerpc: Test preservation of FPU and VMX regs across preemption

In reply to Cyril Bur

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
index b19b269..1a6f09e 100644
@@ -1,2 +1,4 @@
 fpu_syscall
 vmx_syscall
+fpu_preempt
+vmx_preempt
tools/testing/selftests/powerpc/math/Makefile +4 -1
index 418bef1..b6f4158 100644
@@ -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
index 8733874..46bbe99 100644
@@ -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.1
author
#7 lore raw

PATCH v4 3/9 selftests/powerpc: Test FPU and VMX regs in signal ucontext

In reply to Cyril Bur

Load 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
index 1a6f09e..4fe13a4 100644
@@ -2,3 +2,5 @@ fpu_syscall
 vmx_syscall
 fpu_preempt
 vmx_preempt
+fpu_signal
+vmx_signal
tools/testing/selftests/powerpc/math/Makefile +3 -1
index b6f4158..5b88875 100644
@@ -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.1
author
#8 lore raw

PATCH v4 4/9 powerpc: Explicitly disable math features when copying thread

In reply to Cyril Bur

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.

---
 arch/powerpc/kernel/process.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/kernel/process.c +1 -0
index dccc87e..e0c3d2d 100644
@@ -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.1
author
#9 lore raw

PATCH v4 5/9 powerpc: Restore FPU/VEC/VSX if previously used

In reply to Cyril Bur

Currently 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        | 88 +++++++++++++++++++++++++++++++-----
 arch/powerpc/kernel/vector.S         |  4 ++
 6 files changed, 107 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/processor.h +2 -0
index ac23308..dcab21f 100644
@@ -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
index 07cebc3..10d5eab 100644
@@ -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
index 0d525ce..038e0a1 100644
@@ -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
index 2117eac..b063524 100644
@@ -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 +77 -11

127 lines not on this page · raw message

arch/powerpc/kernel/vector.S +4 -0
index 162d0f7..038cff8 100644
@@ -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.1
author
#10 lore raw

PATCH v4 6/9 powerpc: Prepare for splitting giveup_{fpu, altivec, vsx} in two

In reply to 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/include/asm/reg.h       |  8 ++++++++
 arch/powerpc/include/asm/switch_to.h |  7 +++++++
 arch/powerpc/kernel/process.c        | 31 ++++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/reg.h +8 -0
index c4cb2ff..d07b110 100644
@@ -75,6 +75,14 @@
 #define MSR_HV		0
 #endif
 
+/*
+ * To be used in shared book E/book S, this avoids needing to worry about
+ * book S/book E in shared code
+ */
+#ifndef MSR_SPE
+#define MSR_SPE 	0
+#endif
+
 #define MSR_VEC		__MASK(MSR_VEC_LG)	/* Enable AltiVec */
 #define MSR_VSX		__MASK(MSR_VSX_LG)	/* Enable VSX */
 #define MSR_POW		__MASK(MSR_POW_LG)	/* Enable Power Management */
arch/powerpc/include/asm/switch_to.h +7 -0
index 5b268b6..3690041 100644
@@ -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 +30 -1
index 55c1eb0..29da07f 100644
@@ -444,12 +444,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.1
author
#11 lore raw

PATCH v4 7/9 powerpc: Add the ability to save FPU without giving it up

In reply to Cyril Bur

This 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
index 3690041..6a201e8 100644
@@ -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
index b063524..15da2b5 100644
@@ -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
index 29da07f..a7e5061 100644
@@ -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);
@@ -459,7 +469,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.1
author
#12 lore raw

PATCH v4 8/9 powerpc: Add the ability to save Altivec without giving it up

In reply to Cyril Bur

This 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
index 6a201e8..9028822 100644
@@ -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
index a7e5061..14c09d2 100644
@@ -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);
@@ -472,7 +482,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
index 038cff8..51b0c17 100644
@@ -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.1
author
#13 lore raw

PATCH v4 9/9 powerpc: Add the ability to save VSX without giving it up

In reply to Cyril Bur

This 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
index 9028822..17c8380 100644
@@ -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
index 41e1607..ef7024da 100644
@@ -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
index 51b0c17..1c2e7a3 100644
@@ -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.1