macOS: Fix Hypervisor BadArgument Launch Crash #89
No reviewers
Labels
No labels
audio
bug
confirmed
cpu
critical
development
enhancement
gpu
graphics-backend:opengl
graphics-backend:vulkan
gui
help wanted
horizon/hle
i18n
infra
input
kernel
linux
macos
suggestion
windows
No assignees
4 participants
Notifications
Due date
No due date set.
Reference
projects/Ryubing!89
Loading…
Reference in a new issue
No description provided.
Delete branch "Neo/Ryubing:macos-hypervisor"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Ayyyy, welcome to the macOS: Fix Hypervisor BadArgument Launch Crash PR!
We are so so back my friends.
This PR fixes the macOS Hypervisor
BadArgumentlaunch crash, while also sneaking in some improvements to help potentially mitigate those lovely 0 FPS deadlock on macOS in certain games with Vulkan + Hypervisor enabled (there will be a separate PR in the future to address this).The PR is slightly different in terms of the PR messages that I write, but I hope that it'll provide the necessary context to those unaware of the reasoning/causes of the issue.
Also, this is a bit of a read, so grab some snacks, pour a drink, get comfortable, and enjoy some…
STORY TIME!!!
Before we proceed forward, we must properly understand what Hypervisor is.
Hypervisor refers to using Apple's built-in virtualization technology, the Hypervisor Framework, to accelerate CPU emulation.
There are two main ways to do this:
1. Interpreter – executes instructions one by one (very slow).
2. JIT (Just-In-Time) recompilation – translates Switch CPU instructions into native code (much faster).
On Apple Silicon Macs (M1, M2, M3, etc.), the guest CPU and host CPU are both ARM64-based. As the Nintendo Switch itself is ARM64, The Hypervisor Framework can allow Ryujinx to execute large amounts of guest code more directly, reducing emulation overhead. The architectural similarity makes hardware-assisted execution practical. On Intel Macs, there isn't the same direct ARM-to-ARM advantage, so the benefits are much more limited or unavailable.
When Hypervisor mode is available and working correctly, it can provide higher frames, reduced CPU bottlenecks, faster shader compilation in certain instances, smoother gameplay, and lower CPU usage.
The improvement depends heavily on the game. CPU-heavy titles tend to benefit the most.
There are downsides though. Sometimes, certain games may have compatibilities issues, certain feature may not work exactly the same, bugs in the actual Hypervisor implementation can cause crashes or issues, and performance gain vary significantly on a game-to-game basis.
In terms of a simple analogy:
Without Hypervisor: Ryujinx acts like a translator who rewrites every sentence before speaking it.
With Hypervisor: Ryujinx can let the Mac's CPU understand and execute much more of the Switch's code directly, so less translation work is needed.
Anyway, now that we have the background, onto the story!
Once upon a time, around ~December 2025 / January 2026, users — and yes, even devs (though let’s be honest, macOS devs are basically cryptids at this point)
(Authors note: Cryptids - animals or other beings whose present existence is disputed or unsubstantiated by science)
Started noticing inconsistent behaviour with the macOS Hypervisor. In other words, Hypervisor began showing inconsistent launch behaviour for users who've upgraded to the then current macOS versions (15.7.3 / 26.2). This behaviour continued as macOS got updated, and is still present on the latest version (Tahoe 26.5.1).
Initially, Hypervisor was mostly crashing with an
Unexpected result "Denied". The likely cause is two fold:(It is likely to be the former, not the later. This is the main theory, at least.)
However, this crash later evolved into (because of course it did) into
Unexpected result "BadArgument".We'll come back to this in a bit.
For users who wished to run games with Hypervisor enabled, they had a simple solution - build Ryujinx locally. Yes, this is an effective solution in its own right, as that does bypass certain macOS signing and certain .app bundle-related constraints, as well as certain security permissions.
However, this doesn't explain why:
So now we’re here:
Denied→ Evolved intoBadArgumentAnd so, after attempting to fix this for quite some time, I've determined it to be unfixable. Ryujinx should drop macOS support because development has been held back far too long, and there is nothing we can possibly do. Every possible solution was tested and the results were null.
So, it is with great sadness that, here and now, I proclaim that Ryujinx will terminate further macOS support.
Bye-bye, Ryujinx on macOS! You had a great run, and you'll always be remembered. Thank you for bringing so much great memories to us, macOS users!
...
...
...
...
...
HAHA - NOT SO FAST!
(Please tell me I got you there)
Not all hope is lost - there is light at the end of the tunnel!
The key insight is this:
BadArgumentis not just “Deniedbut newer and worse”. It’s something else entirely.Denied→ macOS refuses the request at the security boundary. Nothing even starts.BadArgument→ macOS accepts the request, but the data being passed is now invalid under newer constraints.So instead of:
“You shall not pass.”
We now have:
“You may pass… but what is THAT?”
And this brings us to this PR!
The PR
Users on macOS were experiencing two major issues when Hypervisor was enabled:
1. Hard Crash – "Unexpected result
BadArgument"The most frequent stack trace pointed to:
This crash occurred very early during game initialization, most often inside supervisor calls such as
GetThreadContext3.2. Intermittent Permanent 0 FPS Freezes with Vulkan (unrelated to
BadArgument, but somewhat addressed in this PR)The game would run normally for a while, then drop to 0 FPS permanently. Disabling Hypervisor eliminated the freezes but performance was at times inconsistent.
The Explanation
HvResult.BadArgumentis returned by Apple's Hypervisor framework (hv_vcpu_get_reg,hv_vcpu_get_sys_reg,hv_vcpu_get_simd_fp_reg, etc.) when it determines that the requested operation cannot be performed in the current vCPU state.Common reasons why this happens:
Because the original implementation called
.ThrowOnError()unconditionally on every Hypervisor call, even a single transientBadArgumentwould immediately terminate execution. This was the direct cause of the crash.The 0 FPS issue was a separate but related symptom: when Hypervisor is enabled, the guest CPU runs significantly faster than MoltenVK's presentation queue expects, leading to command buffer starvation and queue deadlock.
The Changes
Implemented: Complete Shadow Register Cache (
HvExecutionContextVcpu.cs)private readonly ulong[] _x = new ulong[32];V128[] _v = new V128[32];_pc,_elrEl1,_esrEl1,_tpidrEl0,_tpidrroEl0,_fpcr,_fpsr, and_pstateRaw(stored asulonginternally to avoid type conversion issues).BadArgument, we now have a safe, previously-valid value to return instead of crashing. A shadow cache gives us that last known good value. The cache is updated on every successful read, so correctness is preserved in normal operation. This is the central fix for theBadArgumentcrash.Defensive BadArgument Handling on Every Register Access Path
GetX,GetV,Pstate, cached getters, etc.) to explicitly check the return value, with the same pattern applied across all accessors.AggressiveMode Global Option (NOT IMPLEMENTED IN THIS PR, BUT A FUTURE PR WILL ADD THIS FULLY)
public static bool AggressiveMode { get; set; } = false;Hot-Path Optimizations
if (index < 0 || index > 30)toif ((uint)index > 30)inGetX,SetX,GetV, andSetV.InitializeCacheDefaults()method called by constructor andReset().GetXis called millions of times per second. Reducing branches improves CPU branch prediction and overall throughput. The clean initialization pattern ensures reliable behavior when vCPUs are reused from the pool.Improved Logging, Diagnostics & Monitoring
WarningCooldownTicks = 1_000_000_000 ≈ 1 second).public long GetFallbackCount()for runtime monitoring.Quick side note:
The logging mentions "PAC failure" rather than HV
BadArgument, and this is a shorthand I've added right now for myself.Technically, we are catching HvResult.BadArgument. It's called "PAC failure" in logs because PAC is a frequent trigger, especially with games like TOTK that use heavy mods (AKA TOTK Optimiser).
Right now, I'm calling it a PAC failure, but will later rename to
BadArgumentas I progress further on a certain issue that's been sitting in our GitHub for some time.Back to the main stuff:
HvExecutionContext.cs) (PR FOLLOW UP IN THE FUTURE)TryGpuSync()called periodically in the mainExecute()loop (every 10–12 execution steps).Thread.Yield()afterSvcAarch64handling and in sync points.These are all of the changes in this PR.
It's been quite a ride getting Hypervisor back up and running, but I loved every second of it.
If there is anything that is unclear in this PR, please let me know and I'll provide more details and update the description above.
Here is a small FAQ section to answer some more questions. These answers come from my own personal testing and behaviour that I observed.
FAQ
Q: Why can some users play games with Hypervisor enabled and others can't on newer macOS versions?
A: This needs a bit of clarification.
You can still run games with Hypervisor enabled on latest macOS - just not all games. Games such as Pokémon Legends: Z-A, The Legend of Zelda: Tears of the Kingdom, The Legend of Zelda: Breath of the Wild, can all run and will boot with Hypervisor enabled.
However, some other games, like Beyblade X Xone, Beyblade X Evobattle, Tales of Xillia Remastered, Tales of Berserk Remastered, etc, will crash on boot with
BadArgumenterror.So it's not that Hypervisor works for one user and doesn't work for the other.
It's that the game doesn't cause Hypervisor to crash.
Games differ in how aggressively they read registers early in boot and how much they rely on modded/JIT code. Games with heavy early supervisor calls and/or mods that manipulate pointers/PAC (pointer authentication errors) are far more likely to hit BadArgument.
(PAC is the reason why TOTK crashes with UltraCam when Hypervisor is disabled on macOS)
Lightweight or less modded titles often avoid the problematic code paths.
––––––
Q: How stable is this fix?
A: Updating macOS versions (Tahoe -> Tahoe) didn't break this implementation. Current data and testing all provide positive results (i.e. - no crash, hooray!)
––––––
Q: What about the
Deniederror?A: This has more so to do with macOS permissions. Currently, Ryujinx is not being shipped in a DMG (which, by Apple conventions, is a more proper way of handling macOS app installations outside of the App Store). There is a theory that packaging Ryujinx into a DMG file will alleviate any permission issues that we currently have, and may help fix this type of error, so this PR may not even be needed (but we'll have to see how it goes).
That said, this error has not appeared in any of the more recent crash logs that I've analysed or acquired myself (from testing), leading to a possible hypothesis that the permission issues have been resolved, but the Hypervisor implementation broke on server builds due to an update to the framework. This is, however, unlikely, because, again, only builds after a particular version (i.e. - the last working version on a users system) broke.
However, DMG packaging is still on a table and something that we want to implement as soon as possible.
––––––
Q: So why do local builds run then?
A: A lot of different factors - build environment, folder location, lack of permissions associated with running .exe files, different ways of handling pure .exe files and .app bundles, notarisation, and more. There was no "scientific testing" done on each of the factors and seeing which one affects what exactly. Pushing this PR and implementing the DMG will tell.
WIP: macOS: Hypervisor Shenanigansto WIP: macOS: Fix Hypervisor ShenanigansNot sure which issues this PR is supposed to solve, but my main issue with the hypervisor have been graphical issues in TOTK, which are not resolved by this PR. I'm unsure of the cause.
Hi @Emiyl! Thank you for the comment. I'll be updating the PR message a bit later tomorrow, but this PR does not target graphical issues in Hypervisor, but rather the issue of actually launching the Hypervisor.
Right now, Hypervisor either launches for some people or either doesn't. Many games crash and many don't, so there is this inconsistency. This PR aims to resolve the launching issues/crashes due to
BadArgumenterrors.However, if you have a log to share where the graphical issues occur (and also a video perhaps), I would take a look at it and see if it perhaps has something that pertains to this PR (I'm also willing to look into what is causing the graphical issues - I just require a bit more information, primarily if it caused only when Hypervisor is enabled or not, OS, game, etc.).
WIP: macOS: Fix Hypervisor Shenanigansto WIP: macOS: Fix Hypervisor Launch CrashWIP: macOS: Fix Hypervisor Launch Crashto WIP: macOS: Fix Hypervisor Launch Crash (And maybe more fixes to come)WIP: macOS: Fix Hypervisor Launch Crash (And maybe more fixes to come)to WIP: macOS: Fix Hypervisor BadArgument Launch Crash (Maybe more fixes here...)WIP: macOS: Fix Hypervisor BadArgument Launch Crash (Maybe more fixes here...)to WIP: macOS: Fix Hypervisor BadArgument Launch Crashcf010dd50ea70502993bWIP: macOS: Fix Hypervisor BadArgument Launch Crashto macOS: Fix Hypervisor BadArgument Launch CrashIt should be noted that the
HvResult.BadArgumentresult when callinghv_vcpu_get_regand friends will be thrown if those methods are called on the wrong thread (they must be called from the thread that owns the VM).I know the GDB stub does this and that causes crashes when debugging, but it is something that should be noted in this case 🤔
I've implemented a workaround before that periodically updated / stored the shadow registers on interrupts and used those to read / write the registers if requested from outside the owner thread, but I haven't published this due to the inherent hackiness of the solution ..
Hi @conhlee! Thank you for the comment!
Would it be possible for you to test this with GDB stub in your own workflow and report back the results? I've seen a comment somewhere about it, but perhaps this fix resolves it? I'm still tweaking it in some places. If you have some logs while working with GDB, it would be beneficial as well!
Thank you in advance!
Excellent work. Excited to get this merged for 1.4.0!
@ -267,3 +269,4 @@ReturnToPool(vcpu);ushort id = (ushort)esr;SupervisorCallHandler(elr - 4UL, id);Thread.Yield(); // Helps with Vulkan + Hypervisor stabilityI know we talked about this, but could you add a brief mention of why here in the comment? That way other users looking at this code later understand why this helps with stability.
Sure thing! I've taken the explanation from the PR message above and slightly adjusted it. Also added a comment to the TryGPUSync().
@ -19,4 +44,2 @@static HvExecutionContextVcpu(){// .NET does not support passing vectors by value, so we need to pass a pointer and use a native// function to load the value into a vector register.Why did we delete this comment?
075751855a1076c3ba6bHi! For whatever reason, Forgojo, doesn't let me reply to the other comment. The comment in the code probably got somehow removed when I was reorganising the file a bit. I've added it back!