Skip to content

Commit 8b69305

Browse files
committed
Reverting Unity pt1 (causes crashes)
1 parent 2c4818f commit 8b69305

File tree

8 files changed

+31
-583
lines changed

8 files changed

+31
-583
lines changed

src/common/ntapi.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@
55

66
#include "ntapi.h"
77

8-
NtClose_t NtClose = nullptr;
8+
NtDelayExecution_t NtDelayExecution = nullptr;
99
NtSetInformationFile_t NtSetInformationFile = nullptr;
10-
NtCreateThread_t NtCreateThread = nullptr;
11-
NtTerminateThread_t NtTerminateThread = nullptr;
12-
NtQueueApcThreadEx_t NtQueueApcThreadEx = nullptr;
1310

1411
namespace Common::NtApi {
1512

1613
void Initialize() {
1714
HMODULE nt_handle = GetModuleHandleA("ntdll.dll");
1815

1916
// http://stackoverflow.com/a/31411628/4725495
20-
NtClose = (NtClose_t)GetProcAddress(nt_handle, "NtClose");
17+
NtDelayExecution = (NtDelayExecution_t)GetProcAddress(nt_handle, "NtDelayExecution");
2118
NtSetInformationFile =
2219
(NtSetInformationFile_t)GetProcAddress(nt_handle, "NtSetInformationFile");
23-
NtCreateThread = (NtCreateThread_t)GetProcAddress(nt_handle, "NtCreateThread");
24-
NtTerminateThread = (NtTerminateThread_t)GetProcAddress(nt_handle, "NtTerminateThread");
25-
NtQueueApcThreadEx = (NtQueueApcThreadEx_t)GetProcAddress(nt_handle, "NtQueueApcThreadEx");
2620
}
2721

2822
} // namespace Common::NtApi

src/common/ntapi.h

Lines changed: 3 additions & 433 deletions
Large diffs are not rendered by default.

src/core/libraries/kernel/threads/pthread.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ static void RunThread(void* arg) {
206206
DebugState.AddCurrentThreadToGuestList();
207207

208208
/* Run the current thread's start routine with argument: */
209-
curthread->native_thr.Initialize();
210209
void* ret = Core::ExecuteGuest(curthread->start_routine, curthread->arg);
211210

212211
/* Remove thread from tracking */
@@ -281,7 +280,7 @@ int PS4_SYSV_ABI posix_pthread_create_name_np(PthreadT* thread, const PthreadAtt
281280
(*thread) = new_thread;
282281

283282
/* Create thread */
284-
new_thread->native_thr = Core::NativeThread();
283+
new_thread->native_thr = Core::Thread();
285284
int ret = new_thread->native_thr.Create(RunThread, new_thread, &new_thread->attr);
286285
ASSERT_MSG(ret == 0, "Failed to create thread with error {}", ret);
287286
if (ret) {
@@ -414,33 +413,6 @@ int PS4_SYSV_ABI posix_pthread_getschedparam(PthreadT pthread, SchedPolicy* poli
414413
return 0;
415414
}
416415

417-
int PS4_SYSV_ABI posix_pthread_setschedparam(PthreadT pthread, SchedPolicy policy,
418-
const SchedParam* param) {
419-
if (pthread == nullptr || param == nullptr) {
420-
return POSIX_EINVAL;
421-
}
422-
423-
auto* thread_state = ThrState::Instance();
424-
if (pthread == g_curthread) {
425-
g_curthread->lock.lock();
426-
} else if (int ret = thread_state->FindThread(pthread, /*include dead*/ 0); ret != 0) {
427-
return ret;
428-
}
429-
430-
if (pthread->attr.sched_policy == policy &&
431-
(policy == SchedPolicy::Other || pthread->attr.prio == param->sched_priority)) {
432-
pthread->attr.prio = param->sched_priority;
433-
pthread->lock.unlock();
434-
return 0;
435-
}
436-
437-
// TODO: _thr_setscheduler
438-
pthread->attr.sched_policy = policy;
439-
pthread->attr.prio = param->sched_priority;
440-
pthread->lock.unlock();
441-
return 0;
442-
}
443-
444416
int PS4_SYSV_ABI scePthreadGetprio(PthreadT thread, int* priority) {
445417
SchedParam param;
446418
SchedPolicy policy;
@@ -524,7 +496,6 @@ void RegisterThread(Core::Loader::SymbolsResolver* sym) {
524496
LIB_FUNCTION("lZzFeSxPl08", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_setcancelstate);
525497
LIB_FUNCTION("a2P9wYGeZvc", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_setprio);
526498
LIB_FUNCTION("FIs3-UQT9sg", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_getschedparam);
527-
LIB_FUNCTION("Xs9hdiD7sAA", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_setschedparam);
528499
LIB_FUNCTION("6XG4B33N09g", "libScePosix", 1, "libkernel", 1, 1, sched_yield);
529500

530501
// Posix-Kernel

src/core/libraries/kernel/threads/pthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct Pthread {
261261
int refcount;
262262
PthreadEntryFunc start_routine;
263263
void* arg;
264-
Core::NativeThread native_thr;
264+
Core::Thread native_thr;
265265
PthreadAttr attr;
266266
bool cancel_enable;
267267
bool cancel_pending;

src/core/memory.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ int MemoryManager::MapFile(void** out_addr, VAddr virtual_addr, size_t size, Mem
328328
}
329329

330330
// Map the file.
331-
impl.MapFile(mapped_addr, size_aligned, offset, std::bit_cast<u32>(prot), fd);
331+
impl.MapFile(mapped_addr, size, offset, std::bit_cast<u32>(prot), fd);
332332

333333
// Add virtual memory area
334334
auto& new_vma = CarveVMA(mapped_addr, size_aligned)->second;
@@ -514,8 +514,9 @@ int MemoryManager::VirtualQuery(VAddr addr, int flags,
514514
info->is_flexible.Assign(vma.type == VMAType::Flexible);
515515
info->is_direct.Assign(vma.type == VMAType::Direct);
516516
info->is_stack.Assign(vma.type == VMAType::Stack);
517-
info->is_pooled.Assign(vma.type == VMAType::PoolReserved);
518-
info->is_committed.Assign(vma.type == VMAType::Pooled);
517+
info->is_pooled.Assign(vma.type == VMAType::Pooled);
518+
info->is_committed.Assign(vma.type != VMAType::Free && vma.type != VMAType::Reserved &&
519+
vma.type != VMAType::PoolReserved);
519520
vma.name.copy(info->name.data(), std::min(info->name.size(), vma.name.size()));
520521
if (vma.type == VMAType::Direct) {
521522
const auto dmem_it = FindDmemArea(vma.phys_base);
@@ -586,7 +587,6 @@ void MemoryManager::NameVirtualRange(VAddr virtual_addr, size_t size, std::strin
586587
"Range provided is not fully contained in vma");
587588
it->second.name = name;
588589
}
589-
590590
VAddr MemoryManager::SearchFree(VAddr virtual_addr, size_t size, u32 alignment) {
591591
// If the requested address is below the mapped range, start search from the lowest address
592592
auto min_search_address = impl.SystemManagedVirtualBase();
@@ -693,7 +693,7 @@ MemoryManager::DMemHandle MemoryManager::Split(DMemHandle dmem_handle, size_t of
693693
new_area.size -= offset_in_area;
694694

695695
return dmem_map.emplace_hint(std::next(dmem_handle), new_area.base, new_area);
696-
}
696+
};
697697

698698
int MemoryManager::GetDirectMemoryType(PAddr addr, int* directMemoryTypeOut,
699699
void** directMemoryStartOut, void** directMemoryEndOut) {

src/core/thread.cpp

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,125 +4,45 @@
44
#include "libraries/kernel/threads/pthread.h"
55
#include "thread.h"
66

7-
#include "core/libraries/kernel/threads/pthread.h"
8-
97
#ifdef _WIN64
108
#include <windows.h>
11-
#include "common/ntapi.h"
129
#else
1310
#include <pthread.h>
1411
#endif
1512

1613
namespace Core {
1714

18-
#ifdef _WIN64
19-
#define KGDT64_R3_DATA (0x28)
20-
#define KGDT64_R3_CODE (0x30)
21-
#define KGDT64_R3_CMTEB (0x50)
22-
#define RPL_MASK (0x03)
23-
24-
#define INITIAL_FPUCW (0x037f)
25-
#define INITIAL_MXCSR_MASK (0xffbf)
26-
#define EFLAGS_INTERRUPT_MASK (0x200)
27-
28-
void InitializeTeb(INITIAL_TEB* teb, const ::Libraries::Kernel::PthreadAttr* attr) {
29-
teb->StackBase = (void*)((u64)attr->stackaddr_attr + attr->stacksize_attr);
30-
teb->StackLimit = nullptr;
31-
teb->StackAllocationBase = attr->stackaddr_attr;
32-
}
33-
34-
void InitializeContext(CONTEXT* ctx, ThreadFunc func, void* arg,
35-
const ::Libraries::Kernel::PthreadAttr* attr) {
36-
/* Note: The stack has to be reversed */
37-
ctx->Rsp = (u64)attr->stackaddr_attr + attr->stacksize_attr;
38-
ctx->Rbp = (u64)attr->stackaddr_attr + attr->stacksize_attr;
39-
ctx->Rcx = (u64)arg;
40-
ctx->Rip = (u64)func;
41-
42-
ctx->SegGs = KGDT64_R3_DATA | RPL_MASK;
43-
ctx->SegEs = KGDT64_R3_DATA | RPL_MASK;
44-
ctx->SegDs = KGDT64_R3_DATA | RPL_MASK;
45-
ctx->SegCs = KGDT64_R3_CODE | RPL_MASK;
46-
ctx->SegSs = KGDT64_R3_DATA | RPL_MASK;
47-
ctx->SegFs = KGDT64_R3_CMTEB | RPL_MASK;
15+
Thread::Thread() : native_handle{0} {}
4816

49-
ctx->EFlags = 0x3000 | EFLAGS_INTERRUPT_MASK;
50-
ctx->MxCsr = INITIAL_MXCSR;
17+
Thread::~Thread() {}
5118

52-
ctx->FltSave.ControlWord = INITIAL_FPUCW;
53-
ctx->FltSave.MxCsr = INITIAL_MXCSR;
54-
ctx->FltSave.MxCsr_Mask = INITIAL_MXCSR_MASK;
55-
56-
ctx->ContextFlags =
57-
CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT;
58-
}
59-
#endif
60-
61-
NativeThread::NativeThread() : native_handle{0} {}
62-
63-
NativeThread::~NativeThread() {}
64-
65-
int NativeThread::Create(ThreadFunc func, void* arg, const ::Libraries::Kernel::PthreadAttr* attr) {
66-
#ifndef _WIN64
19+
int Thread::Create(ThreadFunc func, void* arg, const ::Libraries::Kernel::PthreadAttr* attr) {
20+
#ifdef _WIN64
21+
native_handle = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, nullptr);
22+
return native_handle ? 0 : -1;
23+
#else
6724
pthread_t* pthr = reinterpret_cast<pthread_t*>(&native_handle);
6825
pthread_attr_t pattr;
6926
pthread_attr_init(&pattr);
7027
pthread_attr_setstack(&pattr, attr->stackaddr_attr, attr->stacksize_attr);
7128
return pthread_create(pthr, &pattr, (PthreadFunc)func, arg);
72-
#else
73-
CLIENT_ID clientId{};
74-
INITIAL_TEB teb{};
75-
CONTEXT ctx{};
76-
77-
clientId.UniqueProcess = GetCurrentProcess();
78-
clientId.UniqueThread = GetCurrentThread();
79-
80-
InitializeTeb(&teb, attr);
81-
InitializeContext(&ctx, func, arg, attr);
82-
83-
return NtCreateThread(&native_handle, THREAD_ALL_ACCESS, nullptr, GetCurrentProcess(),
84-
&clientId, &ctx, &teb, false);
8529
#endif
8630
}
8731

88-
void NativeThread::Exit() {
32+
void Thread::Exit() {
8933
if (!native_handle) {
9034
return;
9135
}
9236

93-
tid = 0;
94-
9537
#ifdef _WIN64
96-
NtClose(native_handle);
38+
CloseHandle(native_handle);
9739
native_handle = nullptr;
9840

99-
/* The Windows kernel will free the stack
100-
given at thread creation via INITIAL_TEB
101-
(StackAllocationBase) upon thread termination.
102-
103-
In earlier Windows versions (NT4 to Windows Server 2003),
104-
you could get around this via disabling FreeStackOnTermination
105-
on the TEB. This has been removed since then.
106-
107-
To avoid this, we must forcefully set the TEB
108-
deallocation stack pointer to NULL so ZwFreeVirtualMemory fails
109-
in the kernel and our stack is not freed.
110-
*/
111-
auto* teb = reinterpret_cast<TEB*>(NtCurrentTeb());
112-
teb->DeallocationStack = nullptr;
113-
114-
NtTerminateThread(nullptr, 0);
41+
// We call this assuming the thread has finished execution.
42+
ExitThread(0);
11543
#else
11644
pthread_exit(nullptr);
11745
#endif
11846
}
11947

120-
void NativeThread::Initialize() {
121-
#if _WIN64
122-
tid = GetCurrentThreadId();
123-
#else
124-
tid = (u64)pthread_self();
125-
#endif
126-
}
127-
12848
} // namespace Core

src/core/thread.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,27 @@ struct PthreadAttr;
1111

1212
namespace Core {
1313

14-
using ThreadFunc = void (*)(void*);
15-
using PthreadFunc = void* (*)(void*);
16-
17-
class NativeThread {
14+
class Thread {
1815
public:
19-
NativeThread();
20-
~NativeThread();
16+
using ThreadFunc = void (*)(void*);
17+
using PthreadFunc = void* (*)(void*);
18+
19+
Thread();
20+
~Thread();
2121

2222
int Create(ThreadFunc func, void* arg, const ::Libraries::Kernel::PthreadAttr* attr);
2323
void Exit();
2424

25-
void Initialize();
26-
2725
uintptr_t GetHandle() {
2826
return reinterpret_cast<uintptr_t>(native_handle);
2927
}
3028

31-
u64 GetTid() {
32-
return tid;
33-
}
34-
3529
private:
36-
#ifdef _WIN64
30+
#if _WIN64
3731
void* native_handle;
3832
#else
3933
uintptr_t native_handle;
4034
#endif
41-
u64 tid;
4235
};
4336

4437
} // namespace Core

src/video_core/amdgpu/liverpool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Liverpool::~Liverpool() {
4646
}
4747

4848
void Liverpool::Process(std::stop_token stoken) {
49-
Common::SetCurrentThreadName("shadPS4:GpuCommandProcessor");
49+
Common::SetCurrentThreadName("shadPS4:GPU_CommandProcessor");
5050

5151
while (!stoken.stop_requested()) {
5252
{

0 commit comments

Comments
 (0)