git: 26b47fd4b63c - main - powerpc: save and restore VRSAVE with the vector state

From: Piotr Kubaj <pkubaj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 08:40:42 UTC
The branch main has been updated by pkubaj:

URL: https://cgit.FreeBSD.org/src/commit/?id=26b47fd4b63c4e67a952f9a4f0ceb89bcb6a24ac

commit 26b47fd4b63c4e67a952f9a4f0ceb89bcb6a24ac
Author:     Piotr Kubaj <pkubaj@FreeBSD.org>
AuthorDate: 2026-10-18 10:41:40 +0000
Commit:     Piotr Kubaj <pkubaj@FreeBSD.org>
CommitDate: 2026-11-17 08:40:35 +0000

    powerpc: save and restore VRSAVE with the vector state
    
    VRSAVE was never saved or restored, although user code can write it
    and the mcontext carries it, so every process saw the value the
    previous process on that CPU had left there.
    
    Two processes pinned to the same CPU and writing different values to
    VRSAVE each read back the other's; each reads back its own now.
    
    Reviewed by:    jhibbits
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D61544
---
 sys/powerpc/powerpc/altivec.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/sys/powerpc/powerpc/altivec.c b/sys/powerpc/powerpc/altivec.c
index 928e5cf2f0eb..b847a2e5f12e 100644
--- a/sys/powerpc/powerpc/altivec.c
+++ b/sys/powerpc/powerpc/altivec.c
@@ -39,8 +39,10 @@
 #include <sys/limits.h>
 
 #include <machine/altivec.h>
+#include <machine/cpufunc.h>
 #include <machine/pcb.h>
 #include <machine/psl.h>
+#include <machine/spr.h>
 
 static void
 save_vec_int(struct thread *td)
@@ -80,6 +82,7 @@ save_vec_int(struct thread *td)
 	 */
 	__asm __volatile("mfvscr 0; vspltw 0,0,3; stvewx 0,0,%0"
 	    :: "b"(&pcb->pcb_vec.vscr));
+	pcb->pcb_vec.vrsave = mfspr(SPR_VRSAVE);
 
 	/*
 	 * Disable vector unit again
@@ -126,6 +129,8 @@ enable_vec(struct thread *td)
 	msr = mfmsr();
 	mtmsr(msr | PSL_VEC);
 
+	mtspr(SPR_VRSAVE, pcb->pcb_vec.vrsave);
+
 	/*
 	 * Restore VSCR by first loading it into a vector and then into VSCR.
 	 * (this needs to done before loading the user's vector registers