Skip to content

LATX, fix: Complete FP state handling across signals - #489

Open
ganjue66da wants to merge 7 commits into
lat-opensource:masterfrom
ganjue66da:codex/pr436-signal-followup
Open

ganjue66da wants to merge 7 commits into
lat-opensource:masterfrom
ganjue66da:codex/pr436-signal-followup

Conversation

@ganjue66da

Copy link
Copy Markdown
Contributor

Summary / 变更说明

Follow up on #436 and complete the Linux signal FP-state semantics for LATX.

  • Initialize the complete supported user FP state at signal-handler entry
    after the interrupted state has been saved, while keeping sigreturn
    restoration intact.
  • Require zero x87 TOP before classifying a restored image as MMX.
  • Preserve soft-float and hard-float exception flags independently across
    signal delivery.
  • Distinguish x87 and SSE ownership of the shared LoongArch FCSR sticky flags,
    so x87 FSW and SSE MXCSR do not contaminate each other.
  • Rebuild restored FCSR exception enables with the same policy as translated
    x87 control-word updates.
  • Coalesce synchronization across consecutive x87 instructions as a separate
    optimization, and allow the signal runner to reuse prebuilt x86 guests on
    LoongArch build hosts.

The series is organized as seven independently reviewable patches: five
correctness fixes, one x87-region optimization, and one test-infrastructure
change.

Validation / 验证

Validated on the LoongArch 58 host:

  • Built latx-i386 and latx-x86_64 successfully.
  • Soft-float split checkpoint: 12/12 focused cases passed with
    LATX_SOFTFPU=1/2.
  • Hard-float correctness checkpoint before the region optimization: 72/72
    dynamic, TU, and AOT cases passed.
  • x87-region optimization checkpoint: 72/72 cases passed.
  • Final prebuilt-guest signal runner: 72 passed, 9 expected AVX skips, 0
    failures across x86_64, i386 rt, and i386 legacy frames.
  • Final AOT generate/load matrix: 96/96 passed across 32/64-bit guests,
    LATX_SOFTFPU=0/1/2, TU on/off, and FCSR exceptions on/off.
  • git diff --check passed. Checkpatch reported 0 errors and 5 existing
    test-only warnings (the new-file MAINTAINERS prompt and architecture guards
    used by the shared i386/x86_64 fixture).

Checklist / 检查项

  • I have read CONTRIBUTING.md. / 我已阅读 CONTRIBUTING.md
  • Every commit contains a DCO sign-off (git commit -s). /
    每个提交都包含 DCO 签署(git commit -s)。
  • I have included relevant build or test results, or explained why they
    are not applicable. /
    我已提供相关构建或测试结果,或说明了不适用的原因。

Linux starts a signal handler with initialized user floating-point
state after saving the interrupted state in its signal frame. FNINIT
alone leaves MXCSR and vector registers inherited from the interrupted
code, in addition to leaving LATX's host register copies stale.

Initialize the supported user FP components after either signal frame
has been saved successfully. Reset LATX's FCSR and x87 mode and reload
mapped vector registers without changing the saved interrupted state.

Tests:
- Native x86 and LATX i386 legacy/rt and x86_64 rt signal fixtures
- x87 entry, rounding, MMX restore, MXCSR and XMM/YMM entry/restore
- Hard-float and LATX_SOFTFPU=1/2

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
The canonical MMX image has TOP zero as well as valid tags and 0xffff
exponents. Checking only the latter two properties misclassifies legal
x87 state with nonzero TOP, so the next signal delivery canonicalizes
that state as MMX and corrupts TOP.

Require zero TOP before selecting MMX mode. Add a
signal-frame round-trip test with nonzero TOP and all-valid NaNs; the
next frame must retain the restored TOP without intervening FP code.

Tests:
- Native i386/x86_64 signal-frame round trips
- LATX hard-float and LATX_SOFTFPU=1/2: fails before, passes after
- Existing x87, MMX, MXCSR and XMM/YMM signal regressions

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
LATX_SOFTFPU keeps pending exceptions in fp_status rather than FCSR.
Signal frame creation previously wrote only the architectural x87
status, so soft-float exceptions were lost. Sigreturn also failed to
rebuild fp_status from restored x87 flags.

Synchronize fp_status into the x87 status before creating a frame and
reconstruct it after restoring x87 state.

Tests:
- i386 and x86_64 precision, denormal, and handler-isolation signal
  cases with LATX_SOFTFPU=1/2

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
Sigreturn must rebuild LATX FCSR with the same exception-enable policy
used by translated x87 control-word updates. When LATX_ENABLE_FCSR_EXC
is set, force invalid, divide-by-zero, overflow and underflow enables
while keeping precision disabled. Otherwise mirror the guest x87 masks.

Tests:
- Signal/x87 regression matrix with LATX_ENABLE_FCSR_EXC enabled
- Source truth-table comparison with translated FLDCW handling

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
Translated hard-float x87 and SSE operations share the LoongArch FCSR
sticky flags. Treating every pending bit as x87 state contaminated FSW
with SSE exceptions and left MXCSR incomplete.

Track the guest FP domain that owns pending FCSR flags. Archive flags at
each x87 boundary, route final flags to FSW or MXCSR before saving a
signal frame, and keep restored x87 flags out of the shared FCSR.

Tests:
- i386 and x86_64 x87, SSE, and mixed-domain signal cases
- Dynamic, TU, and AOT generate/load paths with FCSR exceptions on/off

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
The hard-float correctness path places an FCSR archive pair around each
x87 instruction. Consecutive x87 instructions use the same guest FP
domain, so intermediate archive and reload pairs do not change state.

Emit synchronization only at the start and end of each contiguous x87
region, while retaining per-instruction boundaries for isolated x87
operations.

Tests:
- i386 and x86_64 signal/FCSR matrix with dynamic, TU, and AOT paths

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
LoongArch build hosts do not necessarily provide an x86-capable linker,
so the signal regression runner could not exercise LATX there even when
prebuilt guest binaries were available.

Accept LATX_SIGNAL_GUEST_DIR and reuse executable test guests from that
directory. The default compile-and-run behavior remains unchanged.

Tests:
- Signal regression runner on 58 with prebuilt x86_64, i386, and i386
  legacy guests

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant