[PR #1681] [MERGED] The way to Unity, pt.3 #2243

Closed
opened 2026-02-27 21:15:44 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/shadps4-emu/shadPS4/pull/1681
Author: @polybiusproxy
Created: 12/7/2024
Status: Merged
Merged: 12/8/2024
Merged by: @georgemoralis

Base: mainHead: unity-pt3


📝 Commits (1)

📊 Changes

17 files changed (+256 additions, -50 deletions)

View changed files

📝 src/common/ntapi.cpp (+2 -0)
📝 src/common/ntapi.h (+20 -0)
📝 src/core/libraries/ajm/ajm_context.cpp (+2 -0)
📝 src/core/libraries/kernel/kernel.cpp (+2 -1)
📝 src/core/libraries/kernel/memory.cpp (+1 -2)
📝 src/core/libraries/kernel/process.cpp (+11 -4)
📝 src/core/libraries/kernel/sync/semaphore.h (+29 -13)
📝 src/core/libraries/kernel/threads/event_flag.cpp (+29 -1)
📝 src/core/libraries/kernel/threads/exception.cpp (+40 -10)
📝 src/core/libraries/kernel/threads/pthread.cpp (+2 -0)
📝 src/core/libraries/kernel/threads/semaphore.cpp (+96 -12)
📝 src/core/libraries/save_data/save_backup.cpp (+1 -1)
📝 src/core/libraries/save_data/save_memory.cpp (+1 -1)
📝 src/core/linker.h (+9 -0)
📝 src/core/memory.cpp (+5 -3)
📝 src/core/memory.h (+2 -2)
📝 src/imgui/renderer/texture_manager.cpp (+4 -0)

📄 Description

This should be the last of my PR series regarding Unity. The last main thing to implement is exception support.

To emulate POSIX's behavior, we will use special user APCs, which will allow us to 'inject' code on a remote thread, regardless of what the thread was executing—when the APC returns, the thread goes back to what it was executing.
The only caveat is that APCs won't execute if the thread is on a non-alertable wait at the time of the APC raise. This is why we did a rework of sync primitives in part 2, using wrappers that use Win32 sync primitives and WaitForSingleObjectEx.
To make use of APCs, we use NtQueueApcThreadEx and rely on its undocumented behavior to have x86 context on the APC routine as well (thanks to red_prig and Roamic).

In addition, this PR does some additional fixes on the linker to improve Unity games further—if sceKernelLoadStartModule is called more than once for a module, a handle will be returned instead of loading and calling the module's module_start function again. This fixes both crashes and race conditions on later Unity games.
Event flag cancelling is also implemented as seen on Unity (makes no effect, but it's nice to have).

image
image


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/shadps4-emu/shadPS4/pull/1681 **Author:** [@polybiusproxy](https://github.com/polybiusproxy) **Created:** 12/7/2024 **Status:** ✅ Merged **Merged:** 12/8/2024 **Merged by:** [@georgemoralis](https://github.com/georgemoralis) **Base:** `main` ← **Head:** `unity-pt3` --- ### 📝 Commits (1) - [`df19a82`](https://github.com/shadps4-emu/shadPS4/commit/df19a8226245231a33a135a1f83c59dcedd53622) The way to Unity, pt.3 ### 📊 Changes **17 files changed** (+256 additions, -50 deletions) <details> <summary>View changed files</summary> 📝 `src/common/ntapi.cpp` (+2 -0) 📝 `src/common/ntapi.h` (+20 -0) 📝 `src/core/libraries/ajm/ajm_context.cpp` (+2 -0) 📝 `src/core/libraries/kernel/kernel.cpp` (+2 -1) 📝 `src/core/libraries/kernel/memory.cpp` (+1 -2) 📝 `src/core/libraries/kernel/process.cpp` (+11 -4) 📝 `src/core/libraries/kernel/sync/semaphore.h` (+29 -13) 📝 `src/core/libraries/kernel/threads/event_flag.cpp` (+29 -1) 📝 `src/core/libraries/kernel/threads/exception.cpp` (+40 -10) 📝 `src/core/libraries/kernel/threads/pthread.cpp` (+2 -0) 📝 `src/core/libraries/kernel/threads/semaphore.cpp` (+96 -12) 📝 `src/core/libraries/save_data/save_backup.cpp` (+1 -1) 📝 `src/core/libraries/save_data/save_memory.cpp` (+1 -1) 📝 `src/core/linker.h` (+9 -0) 📝 `src/core/memory.cpp` (+5 -3) 📝 `src/core/memory.h` (+2 -2) 📝 `src/imgui/renderer/texture_manager.cpp` (+4 -0) </details> ### 📄 Description This should be the last of my PR series regarding Unity. The last main thing to implement is exception support. To emulate POSIX's behavior, we will use special user APCs, which will allow us to 'inject' code on a remote thread, regardless of what the thread was executing—when the APC returns, the thread goes back to what it was executing. The only caveat is that APCs won't execute if the thread is on a non-alertable wait at the time of the APC raise. This is why we did a rework of sync primitives in part 2, using wrappers that use Win32 sync primitives and WaitForSingleObjectEx. To make use of APCs, we use `NtQueueApcThreadEx` and rely on its undocumented behavior to have x86 context on the APC routine as well (thanks to red_prig and Roamic). In addition, this PR does some additional fixes on the linker to improve Unity games further—if `sceKernelLoadStartModule` is called more than once for a module, a handle will be returned instead of loading and calling the module's `module_start` function again. This fixes both crashes and race conditions on later Unity games. Event flag cancelling is also implemented as seen on Unity (makes no effect, but it's nice to have). ![image](https://github.com/user-attachments/assets/debe9013-42b6-4fe5-84a5-1324f4d9acfb) ![image](https://github.com/user-attachments/assets/8f58b3a2-d746-4ede-b0d0-583455d294cd) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 21:15:44 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#2243
No description provided.