Skip to content

Make the Xenoblade Chronicles: Definitive Edition profile work on Android (2/3): core_preempt lock - #641

Open
tapionallanaire wants to merge 1 commit into
igawa6:mainfrom
tapionallanaire:pr/core-preempt-lock
Open

tapionallanaire wants to merge 1 commit into
igawa6:mainfrom
tapionallanaire:pr/core-preempt-lock

Conversation

@tapionallanaire

@tapionallanaire tapionallanaire commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Android support: one of the two fixes that make the Xenoblade Chronicles: Definitive Edition (0100FF500E34A000, 1.1.2) profile work on Android (boot crash). The other is #640; the profile change is #642.

What

A new profile value core_preempt lock (switch DM_CORE_LOCK): the guest threads of one core take turns, as on Horizon.

  • A guest thread whose affinity is one core holds that core's lock, a first-come ticket lock, while it runs guest code. It gives the lock up at each SVC and native HLE wait.
  • A waiter of higher priority preempts the holder in guest code (SIGURG). If the holder is in host code it isn't stopped, and the waiter signals again.
  • Threads whose affinity allows several cores, and host-owned guest threads, run free.
  • It applies for the first DM_CORE_LOCK_SECS seconds (default 20; 0 = for good).
  • DM_CORE_SLICE_US adds a time slice for waiters of equal priority (default off).

Games without core_preempt lock are unchanged: on and off behave exactly as before.

Why

Xenoblade Chronicles: Definitive Edition corrupts its heap at boot when two threads of core 0 run at the same moment. About one boot in four crashed 1–4 s into nnMain at changing sites:

  • a memset of a size read from a resource block's footer;
  • null and wild pointers in the heap's free-list walk, with the same wild value in several boots.

This happened with or without a save. None of these changed it:

  • core_preempt on;
  • a barrier after file reads;
  • DM_SPIN=0, DM_AUDIO=0 or DM_VK_WORKER=0.

Running the whole process on one CPU with taskset made it worse (6 of 12). So the cause is threads of one core interleaving at arbitrary points, not running in parallel.

Held for the whole game, the lock cost 2–3 hitches of 70–100 ms in five minutes of play: one thread ran 100 ms in guest code while the main thread waited for core 0. The race is at boot, hence the 20 s window.

Tested

AYN Thor (Snapdragon 8 Gen 2), XCDE 1.1.2:

  • 0 of 60 boots crashed (24 with the lock for good, 36 with the 20 s window), against 2–6 of every 12 without it.
  • Five minutes of play afterwards: 9,026 of 9,026 frames at 30 fps (SurfaceFlinger present times).

The native build passes on this branch.

On Horizon two threads of one core never run at the same moment: a running
thread gives its core up only where it blocks, sleeps or yields, or when a
higher-priority thread of the core preempts it. The host runs every guest
thread on its own pthread, so threads of one core run side by side.

Xenoblade Chronicles: Definitive Edition corrupts its heap at boot when
they do: about one boot in four crashed 1-4 s into nnMain at changing sites
(a memset of a size read from a resource block's footer, null and wild
pointers in the heap's free-list walk), with or without a save. Not changed
by core_preempt on, a barrier after file reads, DM_SPIN=0, DM_AUDIO=0 or
DM_VK_WORKER=0; the whole process on one CPU with taskset made it worse
(6 of 12), so it is the interleaving, not parallelism.

core_preempt lock (profile) / DM_CORE_LOCK (switch): a guest thread whose
affinity is one core holds that core's lock, a first-come ticket lock,
while it runs guest code, and gives it up at each SVC and native HLE wait.
A waiter of higher priority preempts the holder in guest code (SIGURG; in
host code it is not stopped and the waiter signals again). Threads that may
run on several cores, and host-owned guest threads, run free.

It applies for the first DM_CORE_LOCK_SECS seconds (default 20; 0 = for
good): held for good it cost Xenoblade 2-3 hitches of 70-100 ms in five
minutes of play (a thread running 100 ms in guest code while the main
thread waited for core 0). DM_CORE_SLICE_US adds a time slice for waiters
of equal priority (default off; 4000 gave more hitches, not fewer).

0 of 60 boots crashed with it, on an AYN Thor (Snapdragon 8 Gen 2).
@tapionallanaire tapionallanaire changed the title host: core_preempt lock, a core's guest threads take turns at boot Make the Xenoblade Chronicles: Definitive Edition profile work on Android (2/3): core_preempt lock Nov 25, 2026
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.

1 participant