arm: Add missing hflag update after writing PSTATE - #2386
Open
phverg wants to merge 2 commits into
Open
Conversation
reg_write() for UC_ARM64_REG_PSTATE called pstate_write() but never refreshed env->hflags. The hflags cache holds PSTATE-derived state such as the current exception level, so after a user raises the EL by writing PSTATE, MRS CurrentEL (which materializes the EL as a translate-time constant from the TB flags) and a subsequent ERET still observed the stale level. The sibling UC_ARM64_REG_CP_REG case already calls arm_rebuild_hflags(); do the same after pstate_write(), matching the AArch32 fix in unicorn-engine#2268. Fixes unicorn-engine#2333.
Write PSTATE to raise the exception level to EL2 and check that MRS CurrentEL observes EL2. Without the hflags rebuild the read returns the stale EL1.
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
Fixes #2333.
uc_reg_write(UC_ARM64_REG_PSTATE, ...)calledpstate_write()but did not refreshenv->hflags. QEMU caches PSTATE-derived state (current exception level, SP selection, ...) inhflags, andMRS CurrentELin particular is materialized as a translate-time constant from the TB flags. So after a user raises the exception level by writing PSTATE,MRS CurrentELand a subsequentERETstill observed the stale level.The fix mirrors the existing
UC_ARM64_REG_CP_REGcase (which already callsarm_rebuild_hflags()), and the merged AArch32 counterpart #2268 ("arm: Add missing hflag update after writing xpsr"). Thearm_rebuild_hflagsmechanism itself was introduced for cp-regs in #1844.Before / after
Writing PSTATE to elevate to EL2, then executing
MRS X0, CurrentEL:X0 = 0x4(stale EL1).X0 = 0x8(EL2), matching the written PSTATE.Testing
Added
test_arm64_pstate_hflags_rebuildintests/unit/test_arm64.c: it writes PSTATE to EL2 and assertsMRS CurrentELreports EL2. The assertion fails on the unpatched tree (returns EL1) and passes with the fix. The full unit-test suite passes.Scope
TTBR0/1,MAIR,PAR,VBAR) do not feedhflags, so they need no rebuild.UC_ARM64_REG_CPACR_EL1does feed the SVE-related hflags (SVEEXC_EL/ZCR_LEN); refreshing it is a separate, lower-impact concern that can be handled as a follow-up if desired.arm_rebuild_hflagscall after writing toUC_ARM64_REG_PSTATE? #2333 also mentions a secondary problem (aCurrentELraw_read/fieldoffsetassert); that is out of scope for this change.