Skip to content

Support the macOS 27 address-space layout - #11

Merged
ly05010419 merged 1 commit into
ly05010419:mainfrom
joevans:macos-27-address-space
Oct 18, 2026
Merged

ly05010419 merged 1 commit into
ly05010419:mainfrom
joevans:macos-27-address-space

Conversation

@joevans

@joevans joevans commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On macOS 27 the game stops at startup with FAIL: reserve the 64 GiB alias region at 0x70_0000_0000. macOS 27 lays out the address space below 2^39 differently:

  • The shared cache now fills 0x1_8000_0000..0xF_C000_0000, which is where the 6 GiB stack and heap were searched for.
  • The system reserves about 24 GiB (two ~12 GiB regions) at a randomized place in 0x70_0000_0000..2^39 on every launch, so a fixed 64 GiB reservation at 0x70_0000_0000 never fits. Even TOTK_ALIAS_GIB=16 fails on some launches.

This PR keeps the fixed macOS 26 layout whenever it fits. When it doesn't, memory_init uses the approach the iOS layout already takes: stack, heap and the alias region go side by side in one free hole below 2^39. The alias region shrinks in 4 GiB steps, down to 8 GiB, until it fits; the game uses less than 3 GiB of it. The size reported to the game comes from the same alias_size(), so the game sees the size that was actually reserved.

macOS 26: the original reserve_fixed(ALIAS_SPACE_AT, …) path runs first and is unchanged. The fallback runs only when that path fails. The only other change makes the cached alias_size() value adjustable.

Testing

  • Mac mini M4 Pro, 48 GB, macOS 27.0.1, base 1.0.0 from NSP.
  • Before: FAIL: reserve the 64 GiB alias region at 0x70_0000_0000 on every launch.
  • After: two launches picked different holes, which shows the randomization is handled:
    • guest layout: alias 0x7d00000000+0x300000000 stack 0x7b90000000+0x10000000 … (12 GiB)
    • guest layout: alias 0x7180000000+0x500000000 stack 0x7010000000+0x10000000 … (20 GiB)
  • The game reached first presented frame and kept running and translating new shaders.
  • Not tested: macOS 26 (I have no macOS 26 machine; that path is unchanged by design), long sessions, and saves.
  • One 【红】预排布贴图存储不在内存池里,回退 CPU line appeared once after the first frame. mtl_addr is a pool-index lookup that doesn't depend on where the guest regions sit, so I don't think it's related, but I couldn't compare it with a macOS 26 run.

The layout was measured with a small mach_vm_region probe; the details are in the docs paragraph added to docs/how-it-works.md.


中文摘要:macOS 27 的共享缓存占满 0x1_8000_0000..0xF_C000_0000,系统还在 0x70_0000_0000..2^39 的随机位置保留约 24 GiB,固定布局无法预留,启动即失败。本 PR 在固定布局可用时(macOS 26)保持不变;否则像 iOS 布局一样把栈、堆和别名区放进同一个空洞,别名区按 4 GiB 递减到最少 8 GiB。已在 macOS 27.0.1 上启动到第一帧;未在 macOS 26 上复测。

🤖 Generated with Claude Code

macOS 27 leaves no room for the fixed guest layout: the shared cache fills
0x1_8000_0000..0xF_C000_0000 (where stack and heap were searched for) and the
system reserves ~24 GiB at a randomized place in 0x70_0000_0000..2^39, so the
64 GiB alias region at 0x70_0000_0000 cannot be reserved and startup fails.

Keep the fixed layout when it fits (macOS 26). Otherwise place stack, heap and
alias region side by side in one free hole below 2^39, as the iOS layout does,
shrinking the alias region in 4 GiB steps down to 8 GiB.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@RVZO6

RVZO6 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

I'm on macOS 27.0.1 (M4 Pro) too, so this fix matters here. I've been running the game with a local allocator that fixes the same problem a different way: it searches for the alias region first, then stack and heap below it, and only shrinks the alias region when TOTK_ALIAS_GIB isn't set. I reviewed this PR's code but haven't run it here. The side-by-side fallback makes sense, but it changes how an explicit TOTK_ALIAS_GIB behaves:

  • An explicit value below 8 (allowed range is 1..64) now fails on the fallback path. The loop is for (g = ALIAS_SIZE; g >= 8 GiB; ...), so with TOTK_ALIAS_GIB=4 it never runs and the runtime reports that it can't reserve the regions, even though a smaller layout would fit more easily.
  • An explicit larger value can be quietly reduced: TOTK_ALIAS_GIB=20 can end up at 16 or 12 GiB.

Suggestion: only shrink when the size is the default. If TOTK_ALIAS_GIB is set, try exactly that size in the side-by-side layout and fail with a clear message if it doesn't fit. If you do keep the shrinking, use min(ALIAS_SIZE, 8 GiB) as the floor so small explicit requests are not rejected.

@ly05010419

Copy link
Copy Markdown
Owner

感谢贡献!

@ly05010419
ly05010419 merged commit dcd7d16 into ly05010419:main Oct 18, 2026
1 check passed
joevans added a commit to joevans/MacNativeGames that referenced this pull request Oct 18, 2026
The side-by-side fallback shrank the alias region from TOTK_ALIAS_GIB down
to 8 GiB, so an explicit size below 8 always failed and a larger one could
be lowered without notice. Only the default size shrinks now; an explicit
size is tried as given and the start fails naming the variable if it does
not fit. The loop also no longer subtracts below the smallest size.

Reported by @RVZO6 in ly05010419#11.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@joevans

joevans commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @RVZO6, you're right on both points. Fixed in a396e2d, following your first suggestion:

  • TOTK_ALIAS_GIB unset: the fallback still shrinks the alias region from 64 GiB down to 8 GiB until it fits.
  • TOTK_ALIAS_GIB set: that exact size is tried in the side-by-side layout. If it doesn't fit, the start fails with a message that names the variable and the size.

The loop is also restructured so it can't subtract below the smallest size, because an explicit value under 4 GiB would otherwise have wrapped. The docs say so too.

Booted the packaged app on macOS 27.0 (M4 Pro), with the result taken from the guest layout / FAIL log line:

TOTK_ALIAS_GIB Result
4 alias 0x7180000000+0x100000000, exactly 4 GiB (this failed before)
64 FAIL: reserve guest stack, heap and the TOTK_ALIAS_GIB=64 alias region below 2^39 (unset it to fit a smaller one)
unset alias 0x7180000000+0x200000000, shrunk to 8 GiB this run (macOS places its reservation randomly; earlier boots got 12 and 20 GiB)

The macOS 26 path (the fixed layout at 0x70_0000_0000) is unchanged. I still can't test on macOS 26 myself.

ly05010419 added a commit that referenced this pull request Oct 19, 2026
Resolved against the iOS layout change (alias_len / alias_requested). An explicit
TOTK_ALIAS_GIB now pins one size on the side-by-side fallback, as on iOS: it is no
longer rejected below 8 GiB or silently reduced, and a size that does not fit fails
with its own message. Only the default size shrinks in 4 GiB steps down to 8 GiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ly05010419 added a commit that referenced this pull request Oct 21, 2026
…inear BC rows

Three layout rules now match the original NVN driver (found by digitalhivehub for
nxnative, PR #11 98fc8a0 / bacf2cb; checked here against 15 original-driver shapes):
- CUBEMAP_ARRAY depth already counts layer-faces: the size and the upload were six
  times too large, and Metal's arrayLength is depth / 6 cubes.
- Packaged layout byte 0: the low nibble is log2 block height, the high nibble log2
  block depth; 3D textures ignored the packaged block depth before.
- Linear textures count pixel rows, also for compressed formats.

In TOTK 1.4.3 the two 4x4 cube arrays now report 0xc00 bytes, as the driver does
(0x4800 before); the grass save and the quick menu look the same as before.
The texture layout probe adds the 15 driver shapes; the old code fails the cube case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XEAgjWRRPcUigVLG8vrbv5
DavidZande pushed a commit to DavidZande/MacNativeGames that referenced this pull request Oct 22, 2026
…inear BC rows

Three layout rules now match the original NVN driver (found by digitalhivehub for
nxnative, PR ly05010419#11 98fc8a0 / bacf2cb; checked here against 15 original-driver shapes):
- CUBEMAP_ARRAY depth already counts layer-faces: the size and the upload were six
  times too large, and Metal's arrayLength is depth / 6 cubes.
- Packaged layout byte 0: the low nibble is log2 block height, the high nibble log2
  block depth; 3D textures ignored the packaged block depth before.
- Linear textures count pixel rows, also for compressed formats.

In TOTK 1.4.3 the two 4x4 cube arrays now report 0xc00 bytes, as the driver does
(0x4800 before); the grass save and the quick menu look the same as before.
The texture layout probe adds the 15 driver shapes; the old code fails the cube case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XEAgjWRRPcUigVLG8vrbv5
(cherry picked from commit 3c482a7)
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.

3 participants