From 775f37d1bec2634fcf72b64b28fdb7e2186a88d7 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Sat, 14 Nov 2026 19:51:30 +0100 Subject: [PATCH] arm64: the FP/SIMD registers belong to the thread and to the domain On arm64 nothing saved v0-v31, FPSR and FPCR: __switch_to and __switch_domain_to carry x19-x29, lr and sp, the exception frame x0-x30. The threads of a process, the guests sharing a CPU and the kernel itself (built without -mgeneral-regs-only, it used v0/v1 in printk, sprintf, fork, the hypercall path) all wrote the same registers. A preempted thread got another context's values back. The visible failure was a process killed in musl's fmt_fp: the loop which writes the integer part of a floating-point number goes through v0/v1 at each step, a corrupted value never reaches zero, and the loop writes upward until it leaves the stack. Seen twice in SOO-NG campaigns on a capsule printing "%.2f". Now: - struct fpsimd_state (v0-v31, FPSR, FPCR), saved and loaded by fpsimd_save()/fpsimd_restore(); - the scheduler saves it for the thread it leaves and loads the next one's; a new thread starts with its creator's, as fork() requires; - AVZ does the same for the domains, in vcpu_t, which the injector already copies into the capsule's context: the state travels with a snapshot (size of that context changes, both ends must be rebuilt); - the arm64 kernel is built with -mgeneral-regs-only, so the registers stay those of the context which entered it until a switch. strtod.c and roxml_xpath.c compute in doubles and are exempted; nothing in the kernel calls them. Test, SOO-NG on virt64 under QEMU, 180 s per node, a capsule whose threads hold a pattern in d0, d1, d8, d31 and read it back in user mode, or format doubles of known text: before after one capsule, one thread 37 / 29742 0 / 27421 one capsule, two threads 8535 / 17923 0 / 14218 two capsules on a node 2975 / 15535 0 / 13392 two threads formatting doubles killed at once 0 / 19.8 M (wrong reads / reads.) Before, the values found were the other thread's pattern, the other capsule's, and kernel addresses. All 17 configurations build; after the change only fpsimd_save and fpsimd_restore touch an FP/SIMD register in the kernel image. Limits: tested on SOO-NG's copy of these sources, not on a standalone SO3 nor on a board; migration of a capsule with the new context not exercised; a signal handler still runs on the interrupted thread's registers; arm32 is unchanged (vfp_save_state()/vfp_restore_state() have no caller there either). --- CHANGELOG | 8 ++- so3/so3/Makefile | 5 ++ so3/so3/arch/arm64/Makefile | 2 +- so3/so3/arch/arm64/asm-offsets.c | 3 + so3/so3/arch/arm64/fpsimd.S | 80 ++++++++++++++++++++++ so3/so3/arch/arm64/include/asm/processor.h | 16 +++++ so3/so3/avz/kernel/schedule.c | 4 ++ so3/so3/include/thread.h | 4 ++ so3/so3/kernel/schedule.c | 7 ++ so3/so3/kernel/thread.c | 5 ++ so3/so3/lib/Makefile | 5 ++ so3/so3/lib/libroxml/Makefile | 6 ++ 12 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 so3/so3/arch/arm64/fpsimd.S diff --git a/CHANGELOG b/CHANGELOG index c28be831f2..00fcae8e5f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -26,8 +26,12 @@ POSIX networking calls, and the build system aligned on Infrabase. when AVZ took it. A Raspberry Pi 5 (BCM2712) platform skeleton is added; it builds but has not been validated on a board yet. - • Kernel: the ARM timer reload is computed from the counter frequency (the - Raspberry Pi 4 tick was 2.9 % too long); a futex is freed by its waker, + • Kernel: on arm64 the FP/SIMD registers belong to the thread and to the + domain, saved and loaded at each switch, and the kernel no longer uses + them (they were shared by all, so that a preempted thread could get + another's values back, and one formatting a floating-point number be + killed in a loop without end); the ARM timer reload is computed from the + counter frequency (the Raspberry Pi 4 tick was 2.9 % too long); a futex is freed by its waker, not by the waiters it woke; the VFS lock is no longer held across an ioctl handler; malloc aligns on 8 bytes; a misaligned spinlock names its caller; the device tree accepts status = "okay". Networking gained poll, diff --git a/so3/so3/Makefile b/so3/so3/Makefile index 00969075fd..cbc8354403 100644 --- a/so3/so3/Makefile +++ b/so3/so3/Makefile @@ -354,6 +354,11 @@ endif ifeq ($(CONFIG_ARCH_ARM64),y) KBUILD_CFLAGS += -mlittle-endian -DBITS_PER_LONG=64 -fno-allow-store-data-races + +# The kernel leaves the FP/SIMD registers to the user space: they hold the +# state of the thread which entered the kernel, saved at a context switch only. + +KBUILD_CFLAGS += -mgeneral-regs-only KBUILD_AFLAGS += -mlittle-endian endif diff --git a/so3/so3/arch/arm64/Makefile b/so3/so3/arch/arm64/Makefile index 22526119f4..e8dbc8e6be 100644 --- a/so3/so3/arch/arm64/Makefile +++ b/so3/so3/arch/arm64/Makefile @@ -7,7 +7,7 @@ obj-y += head.o exception.o traps.o obj-y += fault.o backtrace.o obj-y += backtrace.o backtrace_asm.o -obj-y += cache_v8.o cache.o context.o +obj-y += cache_v8.o cache.o context.o fpsimd.o obj-y += semihosting.o semicall.o obj-$(CONFIG_AVZ) += domain.o mmio.o diff --git a/so3/so3/arch/arm64/asm-offsets.c b/so3/so3/arch/arm64/asm-offsets.c index 0ff10b146a..3cdfc9ac83 100644 --- a/so3/so3/arch/arm64/asm-offsets.c +++ b/so3/so3/arch/arm64/asm-offsets.c @@ -102,6 +102,9 @@ int main(void) BLANK(); DEFINE(OFFSET_TCB_CPU_REGS, offsetof(tcb_t, cpu_regs)); + + DEFINE(OFFSET_FPSIMD_FPSR, offsetof(struct fpsimd_state, fpsr)); + DEFINE(OFFSET_FPSIMD_FPCR, offsetof(struct fpsimd_state, fpcr)); DEFINE(OFFSET_SP_USR, offsetof(cpu_regs_t, sp_usr)); BLANK(); diff --git a/so3/so3/arch/arm64/fpsimd.S b/so3/so3/arch/arm64/fpsimd.S new file mode 100644 index 0000000000..c1632354fc --- /dev/null +++ b/so3/so3/arch/arm64/fpsimd.S @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2026 Daniel Rossier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +// The FP/SIMD state of a thread or of a domain: v0 to v31, FPSR and FPCR. +// The layout is struct fpsimd_state of asm/processor.h. + +#include + +#include + +// Store the FP/SIMD registers. +// x0: the struct fpsimd_state to fill + +ENTRY(fpsimd_save) + stp q0, q1, [x0, #(16 * 0)] + stp q2, q3, [x0, #(16 * 2)] + stp q4, q5, [x0, #(16 * 4)] + stp q6, q7, [x0, #(16 * 6)] + stp q8, q9, [x0, #(16 * 8)] + stp q10, q11, [x0, #(16 * 10)] + stp q12, q13, [x0, #(16 * 12)] + stp q14, q15, [x0, #(16 * 14)] + stp q16, q17, [x0, #(16 * 16)] + stp q18, q19, [x0, #(16 * 18)] + stp q20, q21, [x0, #(16 * 20)] + stp q22, q23, [x0, #(16 * 22)] + stp q24, q25, [x0, #(16 * 24)] + stp q26, q27, [x0, #(16 * 26)] + stp q28, q29, [x0, #(16 * 28)] + stp q30, q31, [x0, #(16 * 30)] + + mrs x1, fpsr + str w1, [x0, #OFFSET_FPSIMD_FPSR] + mrs x1, fpcr + str w1, [x0, #OFFSET_FPSIMD_FPCR] + + ret + +// Load the FP/SIMD registers. +// x0: the struct fpsimd_state to load + +ENTRY(fpsimd_restore) + ldp q0, q1, [x0, #(16 * 0)] + ldp q2, q3, [x0, #(16 * 2)] + ldp q4, q5, [x0, #(16 * 4)] + ldp q6, q7, [x0, #(16 * 6)] + ldp q8, q9, [x0, #(16 * 8)] + ldp q10, q11, [x0, #(16 * 10)] + ldp q12, q13, [x0, #(16 * 12)] + ldp q14, q15, [x0, #(16 * 14)] + ldp q16, q17, [x0, #(16 * 16)] + ldp q18, q19, [x0, #(16 * 18)] + ldp q20, q21, [x0, #(16 * 20)] + ldp q22, q23, [x0, #(16 * 22)] + ldp q24, q25, [x0, #(16 * 24)] + ldp q26, q27, [x0, #(16 * 26)] + ldp q28, q29, [x0, #(16 * 28)] + ldp q30, q31, [x0, #(16 * 30)] + + ldr w1, [x0, #OFFSET_FPSIMD_FPSR] + msr fpsr, x1 + ldr w1, [x0, #OFFSET_FPSIMD_FPCR] + msr fpcr, x1 + + ret diff --git a/so3/so3/arch/arm64/include/asm/processor.h b/so3/so3/arch/arm64/include/asm/processor.h index bab39fb80f..4e0177d009 100644 --- a/so3/so3/arch/arm64/include/asm/processor.h +++ b/so3/so3/arch/arm64/include/asm/processor.h @@ -1093,6 +1093,20 @@ enum trap_return { #define GIC_PRIO_IRQOFF (GIC_PRIO_IRQON & ~0x80) #define GIC_PRIO_PSR_I_SET (1 << 4) +/* + * The FP/SIMD state of a thread or of a domain. The kernel does not use these + * registers: they stay those of the context which entered it until a switch + * saves them here and loads those of the next one. + */ +struct fpsimd_state { + __uint128_t v[32]; + u32 fpsr; + u32 fpcr; +}; + +void fpsimd_save(struct fpsimd_state *state); +void fpsimd_restore(struct fpsimd_state *state); + /* The structure is packed since we may refer as a linear space * addressed by index */ @@ -1161,6 +1175,8 @@ typedef struct vcpu { u64 tcr_el1; u64 mair_el1; + /* FP/SIMD registers of the guest, its kernel and its user space alike */ + struct fpsimd_state fpsimd; } vcpu_t; void resume_to_guest(void); diff --git a/so3/so3/avz/kernel/schedule.c b/so3/so3/avz/kernel/schedule.c index bea149f584..bcd1f599d4 100644 --- a/so3/so3/avz/kernel/schedule.c +++ b/so3/so3/avz/kernel/schedule.c @@ -213,6 +213,8 @@ void vcpu_save_context(struct domain *d) d->vcpu.ttbr1_el1 = read_sysreg(ttbr1_el1); d->vcpu.tcr_el1 = read_sysreg(tcr_el1); d->vcpu.mair_el1 = read_sysreg(mair_el1); + + fpsimd_save(&d->vcpu.fpsimd); } /** @@ -230,6 +232,8 @@ void vcpu_restore_context(struct domain *d) write_sysreg(d->vcpu.tcr_el1, tcr_el1); write_sysreg(d->vcpu.mair_el1, mair_el1); + fpsimd_restore(&d->vcpu.fpsimd); + gic_clear_pending_irqs(); /* Check for pending vIRQs for this domain */ diff --git a/so3/so3/include/thread.h b/so3/so3/include/thread.h index 0ffb73ad95..76bcc4d2d4 100644 --- a/so3/so3/include/thread.h +++ b/so3/so3/include/thread.h @@ -113,6 +113,10 @@ struct tcb { struct list_head joinQueue; cpu_regs_t cpu_regs; + +#ifdef CONFIG_ARCH_ARM64 + struct fpsimd_state fpsimd; +#endif }; typedef struct tcb tcb_t; diff --git a/so3/so3/kernel/schedule.c b/so3/so3/kernel/schedule.c index 9d7ea5097b..500e4ba13f 100644 --- a/so3/so3/kernel/schedule.c +++ b/so3/so3/kernel/schedule.c @@ -518,6 +518,13 @@ void schedule(void) __in_interrupt = false; __in_scheduling = false; +#ifdef CONFIG_ARCH_ARM64 + if (prev) + fpsimd_save(&prev->fpsimd); + + fpsimd_restore(&next->fpsimd); +#endif + __switch_to(prev, next); } __in_scheduling = false; diff --git a/so3/so3/kernel/thread.c b/so3/so3/kernel/thread.c index 4a8cf3df0a..ac570755d8 100644 --- a/so3/so3/kernel/thread.c +++ b/so3/so3/kernel/thread.c @@ -419,6 +419,11 @@ tcb_t *thread_create(clone_args_t *args) kernel_panic(); } +#ifdef CONFIG_ARCH_ARM64 + /* A new thread starts with the FP/SIMD state of its creator, as fork() requires. */ + fpsimd_save(&tcb->fpsimd); +#endif + tcb->tid = tid_next++; /* We append the tid to the thread name */ diff --git a/so3/so3/lib/Makefile b/so3/so3/lib/Makefile index 6c7b18b3bf..b83ecc0b10 100644 --- a/so3/so3/lib/Makefile +++ b/so3/so3/lib/Makefile @@ -7,7 +7,12 @@ lib-y += ctype.o lib-y += vsprintf.o printk.o ifneq ($(CONFIG_ARCH_X86_64),y) lib-y += strtod.o + +# strtod() computes in doubles; libroxml's XPath is its only caller. + +CFLAGS_REMOVE_strtod.o = -mgeneral-regs-only endif + lib-y += list_sort.o lib-y += hashmap.o diff --git a/so3/so3/lib/libroxml/Makefile b/so3/so3/lib/libroxml/Makefile index 529c01c44f..7e0759221f 100644 --- a/so3/so3/lib/libroxml/Makefile +++ b/so3/so3/lib/libroxml/Makefile @@ -2,3 +2,9 @@ lib-y += roxml_buff.o roxml_commit.o roxml_content.o lib-y += roxml_core.o roxml_edit.o roxml_mem.o roxml_nav.o lib-y += roxml_parser.o roxml_xpath.o roxml_file.o + +# The XPath evaluation computes in doubles. + +ifeq ($(CONFIG_ARCH_ARM64),y) +CFLAGS_REMOVE_roxml_xpath.o = -mgeneral-regs-only +endif