From d1a52d37e5e2dbf5c2334587797abe99d6720956 Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Tue, 17 Nov 2026 08:34:52 +0000 Subject: [PATCH 1/2] internal/fakecgo: reserve threadentry's spill slot on ppc64le --- .github/workflows/test.yml | 4 ++ internal/fakecgo/threadentry_ppc64le.go | 12 ++++ internal/fakecgo/threadentry_ppc64le_test.go | 68 ++++++++++++++++++++ internal/fakecgo/trampolines_ppc64le.s | 26 ++++++-- 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 internal/fakecgo/threadentry_ppc64le.go create mode 100644 internal/fakecgo/threadentry_ppc64le_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 825df892..5e91bd8b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -287,9 +287,13 @@ jobs: # binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute # the test binary. Run its child mode directly. env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v + env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo + env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -buildmode=pie -ldflags=-linkmode=internal -o=purego-test-nocgo-pie . env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-nocgo-pie -test.shuffle=on -test.v -test.count=10 env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo-pie -test.run='^TestCallbackEntries$' -test.v + env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -buildmode=pie -ldflags=-linkmode=internal -o=fakecgo-test-nocgo-pie ./internal/fakecgo + env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./fakecgo-test-nocgo-pie -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v diff --git a/internal/fakecgo/threadentry_ppc64le.go b/internal/fakecgo/threadentry_ppc64le.go new file mode 100644 index 00000000..3c69c588 --- /dev/null +++ b/internal/fakecgo/threadentry_ppc64le.go @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2026 The Ebitengine Authors + +//go:build !cgo && linux + +package fakecgo + +// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that +// passes arg in R3 and keeps r14 in R14 across the call. It returns R14 as the +// trampoline leaves it. +// It is only used by tests. Implemented in trampolines_ppc64le.s. +func callThreadEntryTrampoline(arg, r14 uintptr) uintptr diff --git a/internal/fakecgo/threadentry_ppc64le_test.go b/internal/fakecgo/threadentry_ppc64le_test.go new file mode 100644 index 00000000..bb375d96 --- /dev/null +++ b/internal/fakecgo/threadentry_ppc64le_test.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2026 The Ebitengine Authors + +//go:build !cgo && linux + +package fakecgo + +import ( + "testing" + "unsafe" +) + +// spillProbeArg marks the call made by TestThreadEntryTrampolinePreservesR14. +var spillProbeArg byte + +// spillProbeAbove is the word just above spillProbe's spill slot. +var spillProbeAbove uintptr + +// spillProbe stands in for threadentry during the test. Taking the address of +// v makes the compiler store v in its spill slot. Threads the runtime starts +// in the meantime still get the real threadentry. They have no g yet, which is +// why spillProbe is nosplit and norace like threadentry. +// +//go:nosplit +//go:norace +func spillProbe(v unsafe.Pointer) unsafe.Pointer { + if v != unsafe.Pointer(&spillProbeArg) { + return threadentry(v) + } + readAboveSlot(&v) + return nil +} + +// readAboveSlot records the word just above *p in spillProbeAbove. It must be +// nosplit: otherwise it could be preempted for a stack scan, which cannot +// unwind through threadentry_trampoline and would crash the test binary. It is +// noinline, which makes spillProbe store v before the call. +// +//go:nosplit +//go:norace +//go:noinline +func readAboveSlot(p *unsafe.Pointer) { + spillProbeAbove = *(*uintptr)(unsafe.Add(unsafe.Pointer(p), unsafe.Sizeof(*p))) +} + +// TestThreadEntryTrampolinePreservesR14 checks that threadentry_trampoline +// reserves the spill slot of threadentry's register argument. Without the +// reservation, the slot is where the trampoline saved R14, which the ELFv2 ABI +// makes callee-saved, for its C caller. +func TestThreadEntryTrampolinePreservesR14(t *testing.T) { + const r14 uintptr = 0x0123456789abcdef + arg := uintptr(unsafe.Pointer(&spillProbeArg)) + + old := threadentry_call + threadentry_call = spillProbe + defer func() { threadentry_call = old }() + + spillProbeAbove = 0 + if got := callThreadEntryTrampoline(arg, r14); got != r14 { + t.Fatalf("R14 = %#x after threadentry_trampoline, want %#x (the argument was %#x)", got, r14, arg) + } + // The saved R14 sits just above the reserved slot. If it isn't there, v is + // not in that slot, because the compiler or the trampoline's layout changed, + // and this test checks nothing. + if spillProbeAbove != r14 { + t.Errorf("word above the spill slot = %#x, want the saved R14 %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, r14) + } +} diff --git a/internal/fakecgo/trampolines_ppc64le.s b/internal/fakecgo/trampolines_ppc64le.s index f34661f2..05f3dc77 100644 --- a/internal/fakecgo/trampolines_ppc64le.s +++ b/internal/fakecgo/trampolines_ppc64le.s @@ -73,10 +73,13 @@ TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0 MOVW CR, R0 MOVD R0, 8(R1) - MOVDU R1, -320(R1) + // threadentry is ABIInternal, so the caller reserves the spill slot for + // its register argument at 32(R1), after the fixed frame. The saved + // registers start at 40(R1), and the frame is rounded up to 16 bytes. + MOVDU R1, -336(R1) - SAVE_GPR(32) - SAVE_FPR(32+SAVE_GPR_SIZE) + SAVE_GPR(40) + SAVE_FPR(40+SAVE_GPR_SIZE) MOVD $0, R0 @@ -85,10 +88,10 @@ TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0 MOVD R12, CTR CALL CTR - RESTORE_FPR(32+SAVE_GPR_SIZE) - RESTORE_GPR(32) + RESTORE_FPR(40+SAVE_GPR_SIZE) + RESTORE_GPR(40) - ADD $320, R1 + ADD $336, R1 MOVD 16(R1), R0 MOVD R0, LR @@ -96,6 +99,17 @@ TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0 MOVW R0, CR RET +// func callThreadEntryTrampoline(arg, r14 uintptr) uintptr +// Test helper; see threadentry_ppc64le_test.go. +TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-24 + MOVD arg+0(FP), R3 + MOVD r14+8(FP), R14 + CALL threadentry_trampoline(SB) + // The trampoline returns to C with CR's value in R0. Go needs R0 = 0. + XOR R0, R0, R0 + MOVD R14, ret+16(FP) + RET + TEXT ·call5(SB), NOSPLIT|NOFRAME, $0-56 MOVD LR, R20 MOVW CR, R21 From b68e96ac8d2b05fa6215ee92d57736cb7733da1b Mon Sep 17 00:00:00 2001 From: AnyCPU Date: Mon, 23 Nov 2026 20:23:38 +0000 Subject: [PATCH 2/2] internal/fakecgo: remove threadentry spill slot test --- .github/workflows/test.yml | 4 -- internal/fakecgo/threadentry_ppc64le.go | 12 ---- internal/fakecgo/threadentry_ppc64le_test.go | 68 -------------------- internal/fakecgo/trampolines_ppc64le.s | 11 ---- 4 files changed, 95 deletions(-) delete mode 100644 internal/fakecgo/threadentry_ppc64le.go delete mode 100644 internal/fakecgo/threadentry_ppc64le_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e91bd8b..825df892 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -287,13 +287,9 @@ jobs: # binfmt_misc is not registered here, so TestCallbackEntries cannot re-execute # the test binary. Run its child mode directly. env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo -test.run='^TestCallbackEntries$' -test.v - env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -o=fakecgo-test-nocgo ./internal/fakecgo - env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./fakecgo-test-nocgo -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -buildmode=pie -ldflags=-linkmode=internal -o=purego-test-nocgo-pie . env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-nocgo-pie -test.shuffle=on -test.v -test.count=10 env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-nocgo-pie -test.run='^TestCallbackEntries$' -test.v - env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -buildmode=pie -ldflags=-linkmode=internal -o=fakecgo-test-nocgo-pie ./internal/fakecgo - env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./fakecgo-test-nocgo-pie -test.shuffle=on -test.v -test.count=10 env GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go test -c -o=purego-test-cgo . env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-cgo -test.shuffle=on -test.v -test.count=10 env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu PUREGO_TEST_CALLBACK_ENTRIES_CHILD=1 qemu-ppc64le ./purego-test-cgo -test.run='^TestCallbackEntries$' -test.v diff --git a/internal/fakecgo/threadentry_ppc64le.go b/internal/fakecgo/threadentry_ppc64le.go deleted file mode 100644 index 3c69c588..00000000 --- a/internal/fakecgo/threadentry_ppc64le.go +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2026 The Ebitengine Authors - -//go:build !cgo && linux - -package fakecgo - -// callThreadEntryTrampoline calls threadentry_trampoline like a C caller that -// passes arg in R3 and keeps r14 in R14 across the call. It returns R14 as the -// trampoline leaves it. -// It is only used by tests. Implemented in trampolines_ppc64le.s. -func callThreadEntryTrampoline(arg, r14 uintptr) uintptr diff --git a/internal/fakecgo/threadentry_ppc64le_test.go b/internal/fakecgo/threadentry_ppc64le_test.go deleted file mode 100644 index bb375d96..00000000 --- a/internal/fakecgo/threadentry_ppc64le_test.go +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2026 The Ebitengine Authors - -//go:build !cgo && linux - -package fakecgo - -import ( - "testing" - "unsafe" -) - -// spillProbeArg marks the call made by TestThreadEntryTrampolinePreservesR14. -var spillProbeArg byte - -// spillProbeAbove is the word just above spillProbe's spill slot. -var spillProbeAbove uintptr - -// spillProbe stands in for threadentry during the test. Taking the address of -// v makes the compiler store v in its spill slot. Threads the runtime starts -// in the meantime still get the real threadentry. They have no g yet, which is -// why spillProbe is nosplit and norace like threadentry. -// -//go:nosplit -//go:norace -func spillProbe(v unsafe.Pointer) unsafe.Pointer { - if v != unsafe.Pointer(&spillProbeArg) { - return threadentry(v) - } - readAboveSlot(&v) - return nil -} - -// readAboveSlot records the word just above *p in spillProbeAbove. It must be -// nosplit: otherwise it could be preempted for a stack scan, which cannot -// unwind through threadentry_trampoline and would crash the test binary. It is -// noinline, which makes spillProbe store v before the call. -// -//go:nosplit -//go:norace -//go:noinline -func readAboveSlot(p *unsafe.Pointer) { - spillProbeAbove = *(*uintptr)(unsafe.Add(unsafe.Pointer(p), unsafe.Sizeof(*p))) -} - -// TestThreadEntryTrampolinePreservesR14 checks that threadentry_trampoline -// reserves the spill slot of threadentry's register argument. Without the -// reservation, the slot is where the trampoline saved R14, which the ELFv2 ABI -// makes callee-saved, for its C caller. -func TestThreadEntryTrampolinePreservesR14(t *testing.T) { - const r14 uintptr = 0x0123456789abcdef - arg := uintptr(unsafe.Pointer(&spillProbeArg)) - - old := threadentry_call - threadentry_call = spillProbe - defer func() { threadentry_call = old }() - - spillProbeAbove = 0 - if got := callThreadEntryTrampoline(arg, r14); got != r14 { - t.Fatalf("R14 = %#x after threadentry_trampoline, want %#x (the argument was %#x)", got, r14, arg) - } - // The saved R14 sits just above the reserved slot. If it isn't there, v is - // not in that slot, because the compiler or the trampoline's layout changed, - // and this test checks nothing. - if spillProbeAbove != r14 { - t.Errorf("word above the spill slot = %#x, want the saved R14 %#x: v is not in the reserved slot (the compiler or the trampoline's layout changed)", spillProbeAbove, r14) - } -} diff --git a/internal/fakecgo/trampolines_ppc64le.s b/internal/fakecgo/trampolines_ppc64le.s index 05f3dc77..aae2363b 100644 --- a/internal/fakecgo/trampolines_ppc64le.s +++ b/internal/fakecgo/trampolines_ppc64le.s @@ -99,17 +99,6 @@ TEXT threadentry_trampoline(SB), NOSPLIT|NOFRAME, $0-0 MOVW R0, CR RET -// func callThreadEntryTrampoline(arg, r14 uintptr) uintptr -// Test helper; see threadentry_ppc64le_test.go. -TEXT ·callThreadEntryTrampoline(SB), NOSPLIT, $0-24 - MOVD arg+0(FP), R3 - MOVD r14+8(FP), R14 - CALL threadentry_trampoline(SB) - // The trampoline returns to C with CR's value in R0. Go needs R0 = 0. - XOR R0, R0, R0 - MOVD R14, ret+16(FP) - RET - TEXT ·call5(SB), NOSPLIT|NOFRAME, $0-56 MOVD LR, R20 MOVW CR, R21