Skip to content

Only unmap HVF sections that were mapped - #277

Merged
themartiano merged 1 commit into
omacom:mainfrom
stevederico:fix/hvf-romd-double-unmap
Sep 28, 2026
Merged

themartiano merged 1 commit into
omacom:mainfrom
stevederico:fix/hvf-romd-double-unmap

Conversation

@stevederico

@stevederico stevederico commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Refs #151. hvf_set_phys_mem() unmaps every removed section, and turns an add it cannot map into an unmap as well. When pflash leaves romd_mode on a write command, the flat view deletes its mapped ROMD section and adds an I/O section for the same range; that add becomes a second hv_vm_unmap() of the range, and deleting the I/O section on the way back to romd_mode is a third. #151's crash is EXC_GUARD (DEALLOC_GAP) in hv_vm_unmap, called from pflash_mem_write_with_attrs.

The ROM device's romd_mode already holds its new value when the old section is deleted, so the region cannot tell a mapped section from one that never was. The new QEMU patch macos/patches/qemu-hvf-mapped-sections.patch records the sections it maps (keyed by start address, since a section is removed with the range it was added with) and only unmaps what it recorded, like KVM's slot lookup. It applies after the free-page reclaim patch.

Testing

  • python3 macos/Tests/test-hvf-mapped-sections.py: compiles the patched function against a fake HVF that refuses to unmap what it does not hold, and checks the mapping after every step of a pflash write/read cycle, device removal and an unaligned section. The original function makes four bad unmaps; the patched one makes none, and the flash traps while out of romd_mode
  • The patch applies to QEMU c3d48b7 after all existing project patches
  • make test passes on this branch on macOS 15.6 (MacBook Air, Apple Silicon)
  • make runtime builds QEMU with this patch on macOS 15.6
  • The crash was not reproduced (it was reported on an M4 with macOS 26.3), and the app was not run: a fresh guest can't be built right now (make guest fails: hyprland-guiutils 0.2.2-3 is gone from ALARM, and 0.2.2-4 needs hyprtoolkit 0.6 #280). An HVF trace from an affected M4 would confirm this is the path (see Add an opt-in HVF trace log to the launcher #278)

hvf_set_phys_mem() unmaps every removed section, and turns an add it
cannot map into an unmap too. When pflash leaves romd_mode on a write
command, the flat view deletes its mapped ROMD section and adds an I/O
section for the same range; that add became a second hv_vm_unmap() of the
range, and deleting the I/O section on the way back was a third. An
unaligned section is unmapped without ever being mapped in the same way.

The ROM device's romd_mode already holds its new value when the old
section is deleted, so the region cannot tell a mapped section from one
that never was. Add a QEMU patch that records mapped sections, as KVM does
with its slot lookup, and only unmaps what it recorded.

omacom#151 is an M4 on macOS 26.3 killed with EXC_GUARD (DEALLOC_GAP) in
hv_vm_unmap, called from pflash_mem_write_with_attrs.

The test compiles the patched function against a fake HVF that
refuses to unmap what it does not hold, and checks the mapping after every
step of a pflash write/read cycle, device removal and an unaligned
section: the original makes four bad unmaps, the patched one none, and the
flash traps while out of romd_mode. QEMU was not rebuilt and the crash was
not reproduced here; an HVF trace from an affected Mac would confirm it.

Refs omacom#151
@stevederico
stevederico force-pushed the fix/hvf-romd-double-unmap branch from aa049d1 to e1a35c2 Compare September 28, 2026 05:04
@stevederico
stevederico marked this pull request as ready for review September 28, 2026 05:05
@themartiano
themartiano merged commit e90def3 into omacom:main Sep 28, 2026
2 checks passed
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.

2 participants