target/mips: don't pass cpu_gpr[0] to TCG ops for $zero writes#6
Merged
Conversation
The Penguin guest-hypercall hook replaced two upstream guards that treated
writes to $zero as NOPs:
- gen_cond_move(): the 'if (rd == 0) return;' NOP guard was narrowed to
only the movz $0,$0,$0 hypercall trigger, so movz/movn $0, rs, rt now
falls through to tcg_gen_movcond_tl(..., cpu_gpr[0], ...).
- gen_cp0()/OPC_MFC0: the 'if (rt == 0) return;' NOP guard was removed, so
mfc0 $0, rd now falls through to gen_mfc0(ctx, cpu_gpr[0], ...).
On MIPS cpu_gpr[0] is NULL ($zero is special-cased and never allocated as a
TCG global), so both paths hand a NULL TCGv to the code generator. That does
not fault at translation time; it yields a garbage temp index and silently
corrupts unrelated TCG temps / guest registers, producing intermittent wild
control-flow transfers in the guest (observed as random SIGSEGVs in large
dynamically-linked programs such as CPython, triggered by the kernel's
privileged 'mfc0 $0' CP0 hazard reads).
Restore the NOP guards after the hypercall check. RISC-V and LoongArch route
their hypercall results through dest_gpr()/gen_set_gpr() helpers that already
handle the zero register, so only MIPS (which indexes cpu_gpr[] directly) is
affected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Penguin guest-hypercall hook in
target/mips/tcg/translate.creplaced two upstream guards that treated writes to$zeroas NOPs. On MIPScpu_gpr[0]is NULL ($zerois special-cased and never allocated as a TCG global — seemips_tcg_init()), so both changed paths now hand a NULLTCGvto the code generator:gen_cond_move()— theif (rd == 0) return;NOP guard was narrowed to only themovz $0,$0,$0hypercall trigger, somovz/movn $0, rs, rtfalls through totcg_gen_movcond_tl(..., cpu_gpr[0], ...).gen_cp0()/OPC_MFC0— theif (rt == 0) return;NOP guard was removed, somfc0 $0, rdfalls through togen_mfc0(ctx, cpu_gpr[0], ...).A NULL
TCGvdoes not fault at translation time; it produces a garbage temp index, so the translated block silently corrupts unrelated TCG temps / guest registers. The result is intermittent wild control-flow transfers in the guest — observed as random SIGSEGVs in large dynamically-linked programs (e.g. CPython), with the trigger being the guest kernel's privilegedmfc0 $0CP0 hazard reads.Diagnosis
Captured guest core dumps (mipsel and mips64el) showed a consistent signature: PC jumped to an unmapped address in the main executable's region,
raclobbered to page-aligned garbage, whilet9/gpwere still valid. The crash never reproduces under upstream qemu-user (which retains the guards). Only MIPS is affected: RISC-V and LoongArch route their hypercall results throughdest_gpr()/gen_set_gpr()helpers that already handle the zero register, whereas MIPS indexescpu_gpr[]directly.Fix
Restore the NOP guards after the hypercall check (matches pre-hook upstream behavior). 18 lines, MIPS-only.