Skip to content

Commit e1ecd8e

Browse files
authored
threads: initialize tls on thread creation (take 2) (#4070)
1 parent af9cbb8 commit e1ecd8e

File tree

17 files changed

+95
-69
lines changed

17 files changed

+95
-69
lines changed

src/core/libraries/avplayer/avplayer_impl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ void* PS4_SYSV_ABI AvPlayer::Allocate(void* handle, u32 alignment, u32 size) {
1212
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
1313
const auto allocate = self->m_init_data_original.memory_replacement.allocate;
1414
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
15-
return Core::ExecuteGuest(allocate, ptr, alignment, size);
15+
return allocate(ptr, alignment, size);
1616
}
1717

1818
void PS4_SYSV_ABI AvPlayer::Deallocate(void* handle, void* memory) {
1919
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
2020
const auto deallocate = self->m_init_data_original.memory_replacement.deallocate;
2121
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
22-
return Core::ExecuteGuest(deallocate, ptr, memory);
22+
return deallocate(ptr, memory);
2323
}
2424

2525
void* PS4_SYSV_ABI AvPlayer::AllocateTexture(void* handle, u32 alignment, u32 size) {
2626
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
2727
const auto allocate = self->m_init_data_original.memory_replacement.allocate_texture;
2828
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
29-
return Core::ExecuteGuest(allocate, ptr, alignment, size);
29+
return allocate(ptr, alignment, size);
3030
}
3131

3232
void PS4_SYSV_ABI AvPlayer::DeallocateTexture(void* handle, void* memory) {
3333
const auto* const self = reinterpret_cast<AvPlayer*>(handle);
3434
const auto deallocate = self->m_init_data_original.memory_replacement.deallocate_texture;
3535
const auto ptr = self->m_init_data_original.memory_replacement.object_ptr;
36-
return Core::ExecuteGuest(deallocate, ptr, memory);
36+
return deallocate(ptr, memory);
3737
}
3838

3939
int PS4_SYSV_ABI AvPlayer::OpenFile(void* handle, const char* filename) {
@@ -42,7 +42,7 @@ int PS4_SYSV_ABI AvPlayer::OpenFile(void* handle, const char* filename) {
4242

4343
const auto open = self->m_init_data_original.file_replacement.open;
4444
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
45-
return Core::ExecuteGuest(open, ptr, filename);
45+
return open(ptr, filename);
4646
}
4747

4848
int PS4_SYSV_ABI AvPlayer::CloseFile(void* handle) {
@@ -51,7 +51,7 @@ int PS4_SYSV_ABI AvPlayer::CloseFile(void* handle) {
5151

5252
const auto close = self->m_init_data_original.file_replacement.close;
5353
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
54-
return Core::ExecuteGuest(close, ptr);
54+
return close(ptr);
5555
}
5656

5757
int PS4_SYSV_ABI AvPlayer::ReadOffsetFile(void* handle, u8* buffer, u64 position, u32 length) {
@@ -60,7 +60,7 @@ int PS4_SYSV_ABI AvPlayer::ReadOffsetFile(void* handle, u8* buffer, u64 position
6060

6161
const auto read_offset = self->m_init_data_original.file_replacement.read_offset;
6262
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
63-
return Core::ExecuteGuest(read_offset, ptr, buffer, position, length);
63+
return read_offset(ptr, buffer, position, length);
6464
}
6565

6666
u64 PS4_SYSV_ABI AvPlayer::SizeFile(void* handle) {
@@ -69,7 +69,7 @@ u64 PS4_SYSV_ABI AvPlayer::SizeFile(void* handle) {
6969

7070
const auto size = self->m_init_data_original.file_replacement.size;
7171
const auto ptr = self->m_init_data_original.file_replacement.object_ptr;
72-
return Core::ExecuteGuest(size, ptr);
72+
return size(ptr);
7373
}
7474

7575
AvPlayerInitData AvPlayer::StubInitData(const AvPlayerInitData& data) {

src/core/libraries/avplayer/avplayer_state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void AvPlayerState::DefaultEventCallback(void* opaque, AvPlayerEvents event_id,
9292
const auto callback = self->m_event_replacement.event_callback;
9393
const auto ptr = self->m_event_replacement.object_ptr;
9494
if (callback != nullptr) {
95-
Core::ExecuteGuest(callback, ptr, event_id, 0, event_data);
95+
callback(ptr, event_id, 0, event_data);
9696
}
9797
}
9898

src/core/libraries/ime/ime.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ class ImeHandler {
9999
if (m_ime_mode) {
100100
OrbisImeParam param = m_param.ime;
101101
if (use_param_handler) {
102-
Core::ExecuteGuest(param.handler, param.arg, event);
102+
param.handler(param.arg, event);
103103
} else {
104-
Core::ExecuteGuest(handler, param.arg, event);
104+
handler(param.arg, event);
105105
}
106106
} else {
107107
OrbisImeKeyboardParam param = m_param.key;
108108
if (use_param_handler) {
109-
Core::ExecuteGuest(param.handler, param.arg, event);
109+
param.handler(param.arg, event);
110110
} else {
111-
Core::ExecuteGuest(handler, param.arg, event);
111+
handler(param.arg, event);
112112
}
113113
}
114114
}

src/core/libraries/ime/ime_dialog_ui.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ bool ImeDialogState::CallTextFilter() {
131131
return false;
132132
}
133133

134-
int ret =
135-
Core::ExecuteGuest(text_filter, out_text, &out_text_length, src_text, src_text_length);
134+
int ret = text_filter(out_text, &out_text_length, src_text, src_text_length);
136135

137136
if (ret != 0) {
138137
return false;
@@ -153,7 +152,7 @@ bool ImeDialogState::CallKeyboardFilter(const OrbisImeKeycode* src_keycode, u16*
153152
return true;
154153
}
155154

156-
int ret = Core::ExecuteGuest(keyboard_filter, src_keycode, out_keycode, out_status, nullptr);
155+
int ret = keyboard_filter(src_keycode, out_keycode, out_status, nullptr);
157156
return ret == 0;
158157
}
159158

src/core/libraries/kernel/threads.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88
namespace Libraries::Kernel {
99

10+
void PS4_SYSV_ABI ClearStack() {
11+
void* const stackaddr_attr = Libraries::Kernel::g_curthread->attr.stackaddr_attr;
12+
void* volatile sp;
13+
asm("mov %%rsp, %0" : "=rm"(sp));
14+
// leave a safety net of 64 bytes for memset
15+
const size_t size = ((uintptr_t)sp - (uintptr_t)stackaddr_attr) - 64;
16+
void* volatile buf = alloca(size);
17+
memset(buf, 0, size);
18+
sp = nullptr;
19+
}
20+
1021
void RegisterThreads(Core::Loader::SymbolsResolver* sym) {
1122
RegisterMutex(sym);
1223
RegisterCond(sym);

src/core/libraries/kernel/threads.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int PS4_SYSV_ABI posix_pthread_create(PthreadT* thread, const PthreadAttrT* attr
2727
PthreadEntryFunc start_routine, void* arg);
2828

2929
int PS4_SYSV_ABI posix_pthread_join(PthreadT pthread, void** thread_return);
30+
int PS4_SYSV_ABI posix_pthread_detach(PthreadT pthread);
3031

3132
int PS4_SYSV_ABI posix_pthread_mutexattr_init(PthreadMutexAttrT* attr);
3233
int PS4_SYSV_ABI posix_pthread_mutexattr_settype(PthreadMutexAttrT* attr, PthreadMutexType type);
@@ -40,6 +41,8 @@ int PS4_SYSV_ABI posix_pthread_mutex_destroy(PthreadMutexT* mutex);
4041

4142
void RegisterThreads(Core::Loader::SymbolsResolver* sym);
4243

44+
void PS4_SYSV_ABI ClearStack();
45+
4346
class Thread {
4447
public:
4548
explicit Thread() = default;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ int PS4_SYSV_ABI sceKernelRaiseException(PthreadT thread, int signum) {
317317

318318
u64 res = NtQueueApcThreadEx(reinterpret_cast<HANDLE>(thread->native_thr.GetHandle()), option,
319319
ExceptionHandler, (void*)thread->name.c_str(),
320-
(void*)native_signum, nullptr);
320+
(void*)(s64)native_signum, nullptr);
321321
ASSERT(res == 0);
322322
#endif
323323
return ORBIS_OK;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,17 @@ static void RunThread(void* arg) {
199199
g_curthread = curthread;
200200
Common::SetCurrentThreadName(curthread->name.c_str());
201201
DebugState.AddCurrentThreadToGuestList();
202+
Core::InitializeTLS();
202203

203-
/* Run the current thread's start routine with argument: */
204204
curthread->native_thr.Initialize();
205-
void* ret = Core::ExecuteGuest(curthread->start_routine, curthread->arg);
205+
206+
// Clear the stack before running the guest thread
207+
if (False(g_curthread->attr.flags & PthreadAttrFlags::StackUser)) {
208+
ClearStack();
209+
}
210+
211+
/* Run the current thread's start routine with argument: */
212+
void* ret = curthread->start_routine(curthread->arg);
206213

207214
/* Remove thread from tracking */
208215
DebugState.RemoveCurrentThreadFromGuestList();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void _thread_cleanupspecific() {
8484
* destructor:
8585
*/
8686
lk.unlock();
87-
Core::ExecuteGuest(destructor, data);
87+
destructor(data);
8888
lk.lock();
8989
}
9090
}

src/core/libraries/network/net_ctl_obj.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void NetCtlInternal::CheckCallback() {
5050
: ORBIS_NET_CTL_EVENT_TYPE_DISCONNECTED;
5151
for (const auto [func, arg] : callbacks) {
5252
if (func != nullptr) {
53-
Core::ExecuteGuest(func, event, arg);
53+
func(event, arg);
5454
}
5555
}
5656
}
@@ -61,7 +61,7 @@ void NetCtlInternal::CheckNpToolkitCallback() {
6161
: ORBIS_NET_CTL_EVENT_TYPE_DISCONNECTED;
6262
for (const auto [func, arg] : nptool_callbacks) {
6363
if (func != nullptr) {
64-
Core::ExecuteGuest(func, event, arg);
64+
func(event, arg);
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)