kernel: Rewrite pthread emulation (#1440)

* libkernel: Cleanup some function places

* kernel: Refactor thread functions

* kernel: It builds

* kernel: Fix a bunch of bugs, kernel thread heap

* kernel: File cleanup pt1

* File cleanup pt2

* File cleanup pt3

* File cleanup pt4

* kernel: Add missing funcs

* kernel: Add basic exceptions for linux

* gnmdriver: Add workload functions

* kernel: Fix new pthreads code on macOS. (#1441)

* kernel: Downgrade edeadlk to log

* gnmdriver: Add sceGnmSubmitCommandBuffersForWorkload

* exception: Add context register population for macOS. (#1444)

* kernel: Pthread rewrite touchups for Windows

* kernel: Multiplatform thread implementation

* mutex: Remove spamming log

* pthread_spec: Make assert into a log

* pthread_spec: Zero initialize array

* Attempt to fix non-Windows builds

* hotfix: change incorrect NID for scePthreadAttrSetaffinity

* scePthreadAttrSetaffinity implementation

* Attempt to fix Linux

* windows: Address a bunch of address space problems

* address_space: Fix unmap of region surrounded by placeholders

* libs: Reduce logging

* pthread: Implement condvar with waitable atomics and sleepqueue

* sleepq: Separate and make faster

* time: Remove delay execution

* Causes high cpu usage in Tohou Luna Nights

* kernel: Cleanup files again

* pthread: Add missing include

* semaphore: Use binary_semaphore instead of condvar

* Seems more reliable

* libraries/sysmodule: log module on `sceSysmoduleIsLoaded`

* libraries/kernel: implement `scePthreadSetPrio`

---------

Co-authored-by: squidbus <175574877+squidbus@users.noreply.github.com>
Co-authored-by: Daniel R. <47796739+polybiusproxy@users.noreply.github.com>
This commit is contained in:
TheTurtle 2024-11-21 22:59:38 +02:00 committed by GitHub
parent 6904764aab
commit c4506da0ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
104 changed files with 5554 additions and 3979 deletions

View file

@ -6,6 +6,7 @@
#include <algorithm>
#include <mutex>
#include <vector>
#include "core/libraries/kernel/threads.h"
#include "core/module.h"
namespace Core {
@ -40,10 +41,15 @@ struct OrbisProcParam {
u64 unknown1;
};
using ExitFunc = PS4_SYSV_ABI void (*)();
class Linker;
struct EntryParams {
int argc;
u32 padding;
const char* argv[3];
VAddr entry_addr;
};
struct HeapAPI {
@ -79,6 +85,18 @@ public:
return m_modules.at(index).get();
}
u32 MaxTlsIndex() const {
return max_tls_index;
}
u32 GenerationCounter() const {
return dtv_generation_counter;
}
size_t StaticTlsSize() const noexcept {
return static_tls_size;
}
void RelocateAnyImports(Module* m) {
Relocate(m);
for (auto& module : m_modules) {
@ -89,6 +107,14 @@ public:
}
}
void LoadSharedLibraries() {
for (auto& module : m_modules) {
if (module->IsSharedLib()) {
module->Start(0, nullptr, nullptr);
}
}
}
void SetHeapAPI(void* func[]) {
heap_api = reinterpret_cast<AppHeapAPI>(func);
}
@ -98,6 +124,8 @@ public:
}
void* TlsGetAddr(u64 module_index, u64 offset);
void* AllocateTlsForThread(bool is_primary);
void FreeTlsForNonPrimaryThread(void* pointer);
s32 LoadModule(const std::filesystem::path& elf_name, bool is_dynamic = false);
Module* FindByAddress(VAddr address);
@ -108,26 +136,11 @@ public:
void Execute();
void DebugDump();
template <class ReturnType, class... FuncArgs, class... CallArgs>
ReturnType ExecuteGuest(PS4_SYSV_ABI ReturnType (*func)(FuncArgs...),
CallArgs&&... args) const {
// Make sure TLS is initialized for the thread before entering guest.
EnsureThreadInitialized();
return ExecuteGuestWithoutTls(func, args...);
}
private:
const Module* FindExportedModule(const ModuleInfo& m, const LibraryInfo& l);
void EnsureThreadInitialized(bool is_primary = false) const;
void InitTlsForThread(bool is_primary) const;
template <class ReturnType, class... FuncArgs, class... CallArgs>
ReturnType ExecuteGuestWithoutTls(PS4_SYSV_ABI ReturnType (*func)(FuncArgs...),
CallArgs&&... args) const {
return func(std::forward<CallArgs>(args)...);
}
MemoryManager* memory;
Libraries::Kernel::Thread main_thread;
std::mutex mutex;
u32 dtv_generation_counter{1};
size_t static_tls_size{};