diff --git a/README.md b/README.md index 4d24653c..88b24e5e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ Defects found in the code review of 2026-10-08, not fixed yet (evidence and test |---|---|---| | Back-face culling uses opposite rules for 2D and 3D draws (PPSSPP uses one rule) ([#6](https://github.com/endercodezz/P3P3DS/issues/6)) | may hide another orientation bug; risk for scenes not yet seen | suspected | | Thread stacks and the PSP heap can overlap; stacks and freed heap memory are never reused ([#7](https://github.com/endercodezz/P3P3DS/issues/7)) | memory corruption or out-of-memory in long sessions | reproduced in a unit test; P3P's own peak not measured | -| Mutexes are not released when their owner thread exits ([#8](https://github.com/endercodezz/P3P3DS/issues/8)); event-flag result bits differ from the PSP on timeouts ([#9](https://github.com/endercodezz/P3P3DS/issues/9)) | possible deadlock or wrong wake-up | reproduced in a unit test | | `sceIoOpenAsync` returns open errors at once instead of through the async result ([#10](https://github.com/endercodezz/P3P3DS/issues/10)) | a game waiting for the async result may misread a failed open | confirmed in code | | GE finish callbacks run too early, SIGNAL handlers are never called ([#11](https://github.com/endercodezz/P3P3DS/issues/11)) | timing differs from the PSP | confirmed in code | | FPU/VFPU edge cases (`vsin`/`vcos`, `divu` by zero, NaN signs) differ from hardware ([#12](https://github.com/endercodezz/P3P3DS/issues/12)) | small numeric differences | confirmed by pspautotests and host demos | diff --git a/core/src/hle/threadman.cpp b/core/src/hle/threadman.cpp index 4be8b565..5597d885 100644 --- a/core/src/hle/threadman.cpp +++ b/core/src/hle/threadman.cpp @@ -274,6 +274,15 @@ bool ThreadManager::exit_current_thread(std::int32_t exit_status, psprecomp::All if (t.status == InternalThreadState::Waiting && t.wait.type == WaitType::ThreadEnd && t.wait.object == cur->uid) enders.push_back(uid); for (auto uid : enders) wake(runtime, uid, exit_status); + // The kernel frees the mutexes of a thread that ends, and the next waiter + // gets them (pspautotests threads/mutex/lock). An lwmutex stays locked: + // its state is in the guest's workarea (threads/lwmutex/lock). + for (auto &[uid, mutex] : mutexes_) { + if (mutex.count == 0 || mutex.owner != cur->uid) continue; + mutex.count = 0; + mutex.owner = 0; + release_mutex_waiters(runtime, mutex); + } } const bool any_ready = best_ready() != nullptr; const bool any_waiting = std::any_of(threads_.begin(), threads_.end(), [](const auto &entry) { diff --git a/docs/CURRENT_STATE.md b/docs/CURRENT_STATE.md index eb356034..e356d48b 100644 --- a/docs/CURRENT_STATE.md +++ b/docs/CURRENT_STATE.md @@ -2,6 +2,13 @@ This is the current source of truth, newest entries first. Evidence concerns ULUS-10512; each section says whether it was measured on the PC runner, in Azahar or on a real New 3DS. +## Mutexes are released when their owner exits; lwmutexes are not (#8, 2026-11-06) + +- [VERIFIED] PSP behaviour, `references/pspautotests/tests/threads`: `mutex/lock.expected` has "Woke up after other thread exited", `lwmutex/lock.expected` has "Did not wake on thread exit: OK". A mutex is freed by the kernel; an lwmutex stays locked, its state is in the guest's workarea. +- `ThreadManager::exit_current_thread` frees the mutexes of the thread and hands each to its next waiter. Lwmutexes are unchanged. `sceKernelTerminateThread` is not implemented, and `delete_thread` only accepts ended threads, so exit is the only path. +- [INFERRED] The waiter gets the mutex with result 0, as in an unlock: PPSSPP does this. The pspautotests case locks after the exit. +- [VERIFIED] `test_threadman_sync`: 3 new cases, 5 failed checks before the fix, 0 after. CTest 32/32. `new_game.txt` to vblank 9000 before and after: identical event dumps, SHA-256 `0364bf64f29b9434...`, so that route never ends a thread holding a mutex. + ## Mods run on the New 3DS build in Azahar (#23, 2026-11-06) - [VERIFIED] Azahar, the New 3DS build with no change: a `bind/` folder from `p3p-cpk bind` (1,552 files, 29 MB, the fan translation used as a test mod) in `sdmc:/p3p3ds/mods/bind/`, unattended run to vblank 9900. The first dialogue shows the mod's text. @@ -285,7 +292,7 @@ Correctness (ranked by likely impact): - **Fixed** (section above). [VERIFIED] A retry wait is skipped when the thread is preempted right after being dispatched: `ThreadManager::resume_pc` treats "pc is an import stub" as "inside an HLE call, resume at `$ra`", but a thread woken from a retry wait also sits at the stub and must re-run the call. Repro: a priority-40 thread in `sceAudioOutputBlocking` resumes at `$ra` with a stale `$v0` when a priority-32 thread's `sceKernelDelayThread(0)` expires in the same post-import hook. Affects `sceAudioOutputBlocking`/`OutputPannedBlocking`, `sceIoWaitAsync` (stale result, op left pending), `sceCtrlRead*`, module start waits. Must be fixed before NDSP audio. - [INFERRED] Back-face culling: since `72bd103` through mode and transform mode use opposite rules (`software_renderer.cpp`, `gpu_renderer.cpp`); PPSSPP uses one screen-space rule from the CULL bit in both modes (`GPU/GLES/StateMappingGLES.cpp`, `GPU/Software/TransformUnit.cpp`). The split may compensate an unidentified Y flip. Test: count through-mode draws with CULL on in `--draw-log`, then compare frame hashes of the input scripts with one uniform rule. - [VERIFIED] SysMem and thread stacks: stacks bump down from `0x09FF0000` with no check against the SysMem heap growing up to `0x09FF0000` (repro: heap block `08ED0000..09FF0000` and a new stack `09FEC000..09FF0000` overlap); stacks are never reclaimed (255 create/delete cycles of 64 KiB stacks fail with `0x80020190`); `sceKernelStartModule` leaks a 256 KiB stack; `free_partition_memory` never reuses space; `Addr` allocations do not check overlap; `High` is allocated low. [UNVERIFIED] P3P's own peak and alloc/free pattern. -- [VERIFIED] Mutexes (and lwmutexes) are not released when the owning thread exits (repro: deadlock); PPSSPP releases them. +- [VERIFIED] Mutexes are not released when the owning thread exits (repro: deadlock); fixed in #8. [WRONG] for lwmutexes: on the PSP they stay locked. - [VERIFIED] Event flag `outBits`: not written on a real timeout and written on a zero timeout; hardware does the opposite (`pspautotests/tests/threads/events/wait/wait.expected`). - [VERIFIED] `sceIoOpenAsync` returns open errors synchronously; uOFW reports them through the async result (`iofilemgr.c`, `open_iob`). - [VERIFIED] GE: finish callbacks run inside `sceGeListEnQueue`/`UpdateStallAddr` instead of later; SIGNAL handlers are never called; `EnQueueHead` runs the list at once. diff --git a/docs/NEXT_STEPS.md b/docs/NEXT_STEPS.md index eac0d00a..6cc1b85b 100644 --- a/docs/NEXT_STEPS.md +++ b/docs/NEXT_STEPS.md @@ -34,7 +34,7 @@ The PC mixer already produces correct game audio (`sceAudio`, `sceSasCore`, main Reproduced in unit tests or confirmed in code, not yet seen to change P3P's behaviour; fix one per pull request, each with a regression test and an unchanged replay: -- HLE: thread stacks vs heap and memory reuse ([#7](https://github.com/endercodezz/P3P3DS/issues/7)), mutexes on owner exit ([#8](https://github.com/endercodezz/P3P3DS/issues/8)), event-flag `outBits` ([#9](https://github.com/endercodezz/P3P3DS/issues/9)), `sceIoOpenAsync` errors ([#10](https://github.com/endercodezz/P3P3DS/issues/10)), save-name validation ([#13](https://github.com/endercodezz/P3P3DS/issues/13)). +- HLE: thread stacks vs heap and memory reuse ([#7](https://github.com/endercodezz/P3P3DS/issues/7)), event-flag `outBits` ([#9](https://github.com/endercodezz/P3P3DS/issues/9)), `sceIoOpenAsync` errors ([#10](https://github.com/endercodezz/P3P3DS/issues/10)), save-name validation ([#13](https://github.com/endercodezz/P3P3DS/issues/13)). - GE: culling rules ([#6](https://github.com/endercodezz/P3P3DS/issues/6)), callback and signal timing ([#11](https://github.com/endercodezz/P3P3DS/issues/11)); portrait fringe ([#4](https://github.com/endercodezz/P3P3DS/issues/4)). - Recompiler: FPU/VFPU edge cases ([#12](https://github.com/endercodezz/P3P3DS/issues/12)). - CI and tests: release without tests ([#25](https://github.com/endercodezz/P3P3DS/issues/25)), partial skips reported as passed ([#26](https://github.com/endercodezz/P3P3DS/issues/26)), game-specific values outside the profile ([#27](https://github.com/endercodezz/P3P3DS/issues/27)). diff --git a/tests/test_threadman_sync.cpp b/tests/test_threadman_sync.cpp index 8712f720..5f2d3e29 100644 --- a/tests/test_threadman_sync.cpp +++ b/tests/test_threadman_sync.cpp @@ -232,6 +232,55 @@ void mutexes() { CHECK(env.k.threads().get_mutex(static_cast(m))->owner == env.b); } +// references/pspautotests/tests/threads/mutex/lock.expected: "Woke up after +// other thread exited." +void mutex_released_when_owner_exits() { + Env env; + const auto m = env.tm(0xB7D098C6u, {kName, 0u, 0u, 0u}); + env.b = env.spawn(20); // higher priority: B runs at once + CHECK(env.current() == env.b); + CHECK(env.tm(0xB011B11Fu, {m, 1u, 0u}) == 0u); + env.tm(0xCEADEB47u, {100u}); // B delays -> A + CHECK(env.current() == env.a); + env.tm(0xB011B11Fu, {m, 1u, 0u}); // A blocks; B runs when its delay ends + CHECK(env.current() == env.b); + env.tm(0xAA73C935u, {0u}); // B exits holding the mutex + CHECK(!env.rt.stopped()); + CHECK(env.current() == env.a); + CHECK(env.rt.cpu().gpr[2] == 0u); + const auto *mutex = env.k.threads().get_mutex(static_cast(m)); + CHECK(mutex->owner == env.a); + CHECK(mutex->count == 1); + CHECK(env.tm(0x6B30100Fu, {m, 1u}) == 0u); +} + +void mutex_free_after_owner_exit_without_waiters() { + Env env; + const auto m = env.tm(0xB7D098C6u, {kName, 0x200u, 0u, 0u}); // recursive + env.b = env.spawn(20); + CHECK(env.tm(0xB011B11Fu, {m, 3u, 0u}) == 0u); + env.tm(0xAA73C935u, {0u}); + CHECK(env.current() == env.a); + const auto *mutex = env.k.threads().get_mutex(static_cast(m)); + CHECK(mutex->owner == 0); + CHECK(mutex->count == 0); + CHECK(env.tm(0xB011B11Fu, {m, 1u, 0u}) == 0u); +} + +// references/pspautotests/tests/threads/lwmutex/lock.expected: "Did not wake on +// thread exit: OK". The lock lives in the guest's workarea, not in the kernel. +void lw_mutex_stays_locked_when_owner_exits() { + Env env; + const std::uint32_t wa = 0x08900200u; + CHECK(env.tm(0x19CFF145u, {wa, kName, 0u, 0u, 0u}) == 0u); + env.b = env.spawn(20); + CHECK(env.kl(0xBEA46419u, {wa, 1u, 0u}) == 0u); + env.tm(0xAA73C935u, {0u}); + CHECK(env.current() == env.a); + CHECK(env.rt.memory().load32(wa + 0u) == 1u); + CHECK(env.rt.memory().load32(wa + 4u) == static_cast(env.b)); +} + void delays_sleep_and_thread_end() { Env env; env.b = env.spawn(40); @@ -363,6 +412,9 @@ int main() { event_flag_timeouts(); lw_mutexes(); mutexes(); + mutex_released_when_owner_exits(); + mutex_free_after_owner_exit_without_waiters(); + lw_mutex_stays_locked_when_owner_exits(); delays_sleep_and_thread_end(); thread_end_status(); callbacks();