Skip to content

LATX, fix: Restore x87 state across signal handlers - #436

Open
ganjue66da wants to merge 3 commits into
lat-opensource:masterfrom
ganjue66da:codex/x87-signal-state
Open

LATX, fix: Restore x87 state across signal handlers#436
ganjue66da wants to merge 3 commits into
lat-opensource:masterfrom
ganjue66da:codex/x87-signal-state

Conversation

@ganjue66da

Copy link
Copy Markdown
Contributor

Dependency / 依赖

Depends on #424. Please merge #424 first.

Without #424, the hard-float x87-entry regression reaches the pre-existing
stale LBT TOP issue and exits with status 22 after sigreturn. With #424
applied, all nine targeted signal tests pass.

Summary / 变更说明

  • Rebuild LoongArch FCSR state from the restored x87 control and status
    words after sigreturn.
  • Encode LATX's private x87/MMX mode in the architectural signal image and
    reconstruct mode_fpu after restore.
  • Initialize the live x87 state after a signal frame has been built so a
    64-bit handler starts with Linux-compatible initial x87 state while the
    interrupted state remains available to sigreturn.

The added work is limited to signal-frame setup and sigreturn; translated
instruction and TB hot paths are unchanged.

Validation / 验证

  • ./latxbuild/build64.sh -c
    • lat-x86_64 1.6.5-340-g49a31b8402
    • SHA-256:
      a1c44b66cb77e63a5c98856164d22b70c196b3cc652dcad3eaa5c8cb3a4d1cbe
  • ninja -C build32 latx-i386
    • SHA-256:
      761a9e0c621aefd0cc589ffe1f155ed0e58e8ed526a557eee1a461334fb9c1c6
  • Current upstream/master plus this series:
  • Current upstream/master plus LATX, fix: Restore LBT TOP state after longjmp #424 and this series:
    • x87 handler-entry, rounding restore, and MMX restore pass with
      hard-float, LATX_SOFTFPU=1, and LATX_SOFTFPU=2 (9/9).

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. /
    我已提供相关构建或测试结果,或说明了不适用的原因。

FXSAVE and XSAVE restore the architectural x87 control and status
words, but LATX continues with the LoongArch FCSR left by the signal
handler.  A handler that changes rounding or exception state can
therefore affect translated x87 instructions after sigreturn.

Rebuild the FCSR rounding, exception enable, and exception flag fields
from the restored x87 control and status words.

Tests:
- Verify x87 round-down survives SIGUSR1 in 32-bit and 64-bit
  hard-float and LATX_SOFTFPU=1/2 modes

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
LATX tracks x87 and MMX execution in the private mode_fpu field, but
its saved x87 image does not encode MMX's valid tags and 0xffff
exponents.  A signal handler can change mode_fpu while the interrupted
mode exists only outside the signal frame.  In LATX_SOFTFPU=2 mode,
subsequent MMX instructions can then read the wrong host register set.

Canonicalize MMX state before building the signal frame, recognize its
physical-register encoding after restore, and rebuild mode_fpu.

Tests:
- Verify MMX state survives a SIGUSR1 handler that executes FNINIT in
  32-bit and 64-bit hard-float and LATX_SOFTFPU=1/2 modes

Signed-off-by: Wenqiang Wei <weiwenqiang@mail.ustc.edu.cn>
The 64-bit signal path saves interrupted state with non-destructive
FXSAVE or XSAVE, then enters the handler without initializing live x87
state.  The handler consequently observes the interrupted x87 stack
and control state.

Initialize only x87 after successfully building the signal frame.  Do
not change MXCSR or vector and extended state, and leave the frame
unchanged so sigreturn restores the interrupted state.

Tests:
- Verify a 64-bit SIGUSR1 handler starts with initial x87 state while
  non-x87 state remains unchanged and sigreturn restores saved state
- Verify 32-bit and 64-bit x87 state survives SIGUSR1 in hard-float and
  LATX_SOFTFPU=1/2 modes

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