[Kernel] Fix threading issues caused by direct use of exports.

This commit is contained in:
gibbed 2018-11-25 05:08:16 -06:00
parent e6dbd8905c
commit 4dfe83af6c
4 changed files with 64 additions and 51 deletions

View File

@ -350,7 +350,7 @@ dword_result_t NetDll_WSAWaitForMultipleEvents(dword_t num_events,
X_STATUS result = 0; X_STATUS result = 0;
do { do {
result = xboxkrnl::NtWaitForMultipleObjectsEx( result = xboxkrnl::xeNtWaitForMultipleObjectsEx(
num_events, events, wait_all, 1, alertable, num_events, events, wait_all, 1, alertable,
timeout != -1 ? &timeout_wait : nullptr); timeout != -1 ? &timeout_wait : nullptr);
} while (result == X_STATUS_ALERTED); } while (result == X_STATUS_ALERTED);
@ -384,7 +384,7 @@ dword_result_t NetDll_WSACloseEvent(dword_t event_handle) {
DECLARE_XAM_EXPORT1(NetDll_WSACloseEvent, kNetworking, kImplemented); DECLARE_XAM_EXPORT1(NetDll_WSACloseEvent, kNetworking, kImplemented);
dword_result_t NetDll_WSAResetEvent(dword_t event_handle) { dword_result_t NetDll_WSAResetEvent(dword_t event_handle) {
X_STATUS result = xboxkrnl::NtClearEvent(event_handle); X_STATUS result = xboxkrnl::xeNtClearEvent(event_handle);
if (XFAILED(result)) { if (XFAILED(result)) {
uint32_t error = xboxkrnl::RtlNtStatusToDosError(result); uint32_t error = xboxkrnl::RtlNtStatusToDosError(result);
XThread::SetLastError(error); XThread::SetLastError(error);
@ -395,7 +395,7 @@ dword_result_t NetDll_WSAResetEvent(dword_t event_handle) {
DECLARE_XAM_EXPORT1(NetDll_WSAResetEvent, kNetworking, kImplemented); DECLARE_XAM_EXPORT1(NetDll_WSAResetEvent, kNetworking, kImplemented);
dword_result_t NetDll_WSASetEvent(dword_t event_handle) { dword_result_t NetDll_WSASetEvent(dword_t event_handle) {
X_STATUS result = xboxkrnl::NtSetEvent(event_handle, nullptr); X_STATUS result = xboxkrnl::xeNtSetEvent(event_handle, nullptr);
if (XFAILED(result)) { if (XFAILED(result)) {
uint32_t error = xboxkrnl::RtlNtStatusToDosError(result); uint32_t error = xboxkrnl::RtlNtStatusToDosError(result);
XThread::SetLastError(error); XThread::SetLastError(error);

View File

@ -416,7 +416,7 @@ void RtlEnterCriticalSection(pointer_t<X_RTL_CRITICAL_SECTION> cs) {
if (xe::atomic_inc(&cs->lock_count) != 0) { if (xe::atomic_inc(&cs->lock_count) != 0) {
// Create a full waiter. // Create a full waiter.
KeWaitForSingleObject(reinterpret_cast<void*>(cs.host_address()), 8, 0, 0, xeKeWaitForSingleObject(reinterpret_cast<void*>(cs.host_address()), 8, 0, 0,
nullptr); nullptr);
} }
@ -465,7 +465,7 @@ void RtlLeaveCriticalSection(pointer_t<X_RTL_CRITICAL_SECTION> cs) {
cs->owning_thread = 0; cs->owning_thread = 0;
if (xe::atomic_dec(&cs->lock_count) != -1) { if (xe::atomic_dec(&cs->lock_count) != -1) {
// There were waiters - wake one of them. // There were waiters - wake one of them.
KeSetEvent(reinterpret_cast<X_KEVENT*>(cs.host_address()), 1, 0); xeKeSetEvent(reinterpret_cast<X_KEVENT*>(cs.host_address()), 1, 0);
} }
} }
DECLARE_XBOXKRNL_EXPORT2(RtlLeaveCriticalSection, kNone, kImplemented, DECLARE_XBOXKRNL_EXPORT2(RtlLeaveCriticalSection, kNone, kImplemented,

View File

@ -373,7 +373,7 @@ void KeInitializeEvent(pointer_t<X_KEVENT> event_ptr, dword_t event_type,
} }
DECLARE_XBOXKRNL_EXPORT1(KeInitializeEvent, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(KeInitializeEvent, kThreading, kImplemented);
uint32_t keSetEvent(X_KEVENT* event_ptr, uint32_t increment, uint32_t wait) { uint32_t xeKeSetEvent(X_KEVENT* event_ptr, uint32_t increment, uint32_t wait) {
auto ev = XObject::GetNativeObject<XEvent>(kernel_state(), event_ptr); auto ev = XObject::GetNativeObject<XEvent>(kernel_state(), event_ptr);
if (!ev) { if (!ev) {
assert_always(); assert_always();
@ -385,7 +385,7 @@ uint32_t keSetEvent(X_KEVENT* event_ptr, uint32_t increment, uint32_t wait) {
dword_result_t KeSetEvent(pointer_t<X_KEVENT> event_ptr, dword_t increment, dword_result_t KeSetEvent(pointer_t<X_KEVENT> event_ptr, dword_t increment,
dword_t wait) { dword_t wait) {
return keSetEvent(event_ptr, increment, wait); return xeKeSetEvent(event_ptr, increment, wait);
} }
DECLARE_XBOXKRNL_EXPORT2(KeSetEvent, kThreading, kImplemented, kHighFrequency); DECLARE_XBOXKRNL_EXPORT2(KeSetEvent, kThreading, kImplemented, kHighFrequency);
@ -446,7 +446,7 @@ dword_result_t NtCreateEvent(lpdword_t handle_ptr,
} }
DECLARE_XBOXKRNL_EXPORT1(NtCreateEvent, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(NtCreateEvent, kThreading, kImplemented);
dword_result_t NtSetEvent(dword_t handle, lpdword_t previous_state_ptr) { uint32_t xeNtSetEvent(uint32_t handle, xe::be<uint32_t>* previous_state_ptr) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
auto ev = kernel_state()->object_table()->LookupObject<XEvent>(handle); auto ev = kernel_state()->object_table()->LookupObject<XEvent>(handle);
@ -461,6 +461,10 @@ dword_result_t NtSetEvent(dword_t handle, lpdword_t previous_state_ptr) {
return result; return result;
} }
dword_result_t NtSetEvent(dword_t handle, lpdword_t previous_state_ptr) {
return xeNtSetEvent(handle, previous_state_ptr);
}
DECLARE_XBOXKRNL_EXPORT2(NtSetEvent, kThreading, kImplemented, kHighFrequency); DECLARE_XBOXKRNL_EXPORT2(NtSetEvent, kThreading, kImplemented, kHighFrequency);
dword_result_t NtPulseEvent(dword_t handle, lpdword_t previous_state_ptr) { dword_result_t NtPulseEvent(dword_t handle, lpdword_t previous_state_ptr) {
@ -481,7 +485,7 @@ dword_result_t NtPulseEvent(dword_t handle, lpdword_t previous_state_ptr) {
DECLARE_XBOXKRNL_EXPORT2(NtPulseEvent, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT2(NtPulseEvent, kThreading, kImplemented,
kHighFrequency); kHighFrequency);
dword_result_t NtClearEvent(dword_t handle) { uint32_t xeNtClearEvent(uint32_t handle) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
auto ev = kernel_state()->object_table()->LookupObject<XEvent>(handle); auto ev = kernel_state()->object_table()->LookupObject<XEvent>(handle);
@ -493,6 +497,8 @@ dword_result_t NtClearEvent(dword_t handle) {
return result; return result;
} }
dword_result_t NtClearEvent(dword_t handle) { return xeNtClearEvent(handle); }
DECLARE_XBOXKRNL_EXPORT2(NtClearEvent, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT2(NtClearEvent, kThreading, kImplemented,
kHighFrequency); kHighFrequency);
@ -512,7 +518,7 @@ void KeInitializeSemaphore(pointer_t<X_KSEMAPHORE> semaphore_ptr, dword_t count,
} }
DECLARE_XBOXKRNL_EXPORT1(KeInitializeSemaphore, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(KeInitializeSemaphore, kThreading, kImplemented);
uint32_t keReleaseSemaphore(X_KSEMAPHORE* semaphore_ptr, uint32_t increment, uint32_t xeKeReleaseSemaphore(X_KSEMAPHORE* semaphore_ptr, uint32_t increment,
uint32_t adjustment, uint32_t wait) { uint32_t adjustment, uint32_t wait) {
auto sem = auto sem =
XObject::GetNativeObject<XSemaphore>(kernel_state(), semaphore_ptr); XObject::GetNativeObject<XSemaphore>(kernel_state(), semaphore_ptr);
@ -530,7 +536,7 @@ uint32_t keReleaseSemaphore(X_KSEMAPHORE* semaphore_ptr, uint32_t increment,
dword_result_t KeReleaseSemaphore(pointer_t<X_KSEMAPHORE> semaphore_ptr, dword_result_t KeReleaseSemaphore(pointer_t<X_KSEMAPHORE> semaphore_ptr,
dword_t increment, dword_t adjustment, dword_t increment, dword_t adjustment,
dword_t wait) { dword_t wait) {
return keReleaseSemaphore(semaphore_ptr, increment, adjustment, wait); return xeKeReleaseSemaphore(semaphore_ptr, increment, adjustment, wait);
} }
DECLARE_XBOXKRNL_EXPORT1(KeReleaseSemaphore, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(KeReleaseSemaphore, kThreading, kImplemented);
@ -728,7 +734,7 @@ dword_result_t NtCancelTimer(dword_t timer_handle,
} }
DECLARE_XBOXKRNL_EXPORT1(NtCancelTimer, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(NtCancelTimer, kThreading, kImplemented);
uint32_t keWaitForSingleObject(void* object_ptr, uint32_t wait_reason, uint32_t xeKeWaitForSingleObject(void* object_ptr, uint32_t wait_reason,
uint32_t processor_mode, uint32_t alertable, uint32_t processor_mode, uint32_t alertable,
uint64_t* timeout) { uint64_t* timeout) {
auto object = XObject::GetNativeObject<XObject>(kernel_state(), object_ptr); auto object = XObject::GetNativeObject<XObject>(kernel_state(), object_ptr);
@ -749,7 +755,7 @@ dword_result_t KeWaitForSingleObject(lpvoid_t object_ptr, dword_t wait_reason,
dword_t processor_mode, dword_t alertable, dword_t processor_mode, dword_t alertable,
lpqword_t timeout_ptr) { lpqword_t timeout_ptr) {
uint64_t timeout = timeout_ptr ? static_cast<uint64_t>(*timeout_ptr) : 0u; uint64_t timeout = timeout_ptr ? static_cast<uint64_t>(*timeout_ptr) : 0u;
return keWaitForSingleObject(object_ptr, wait_reason, processor_mode, return xeKeWaitForSingleObject(object_ptr, wait_reason, processor_mode,
alertable, &timeout); alertable, &timeout);
} }
DECLARE_XBOXKRNL_EXPORT3(KeWaitForSingleObject, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT3(KeWaitForSingleObject, kThreading, kImplemented,
@ -808,10 +814,10 @@ dword_result_t KeWaitForMultipleObjects(dword_t count, lpdword_t objects_ptr,
DECLARE_XBOXKRNL_EXPORT3(KeWaitForMultipleObjects, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT3(KeWaitForMultipleObjects, kThreading, kImplemented,
kBlocking, kHighFrequency); kBlocking, kHighFrequency);
dword_result_t NtWaitForMultipleObjectsEx(dword_t count, lpdword_t handles, uint32_t xeNtWaitForMultipleObjectsEx(uint32_t count, xe::be<uint32_t>* handles,
dword_t wait_type, dword_t wait_mode, uint32_t wait_type, uint32_t wait_mode,
dword_t alertable, uint32_t alertable,
lpqword_t timeout_ptr) { uint64_t* timeout_ptr) {
assert_true(wait_type <= 1); assert_true(wait_type <= 1);
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
@ -826,13 +832,21 @@ dword_result_t NtWaitForMultipleObjectsEx(dword_t count, lpdword_t handles,
objects.push_back(std::move(object)); objects.push_back(std::move(object));
} }
uint64_t timeout = timeout_ptr ? static_cast<uint64_t>(*timeout_ptr) : 0u; result =
result = XObject::WaitMultiple( XObject::WaitMultiple(count, reinterpret_cast<XObject**>(objects.data()),
count, reinterpret_cast<XObject**>(objects.data()), wait_type, 6, wait_type, 6, wait_mode, alertable, timeout_ptr);
wait_mode, alertable, timeout_ptr ? &timeout : nullptr);
return result; return result;
} }
dword_result_t NtWaitForMultipleObjectsEx(dword_t count, lpdword_t handles,
dword_t wait_type, dword_t wait_mode,
dword_t alertable,
lpqword_t timeout_ptr) {
uint64_t timeout = timeout_ptr ? static_cast<uint64_t>(*timeout_ptr) : 0u;
return xeNtWaitForMultipleObjectsEx(count, handles, wait_type, wait_mode,
alertable, &timeout);
}
DECLARE_XBOXKRNL_EXPORT3(NtWaitForMultipleObjectsEx, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT3(NtWaitForMultipleObjectsEx, kThreading, kImplemented,
kBlocking, kHighFrequency); kBlocking, kHighFrequency);
@ -860,7 +874,7 @@ dword_result_t NtSignalAndWaitForSingleObjectEx(dword_t signal_handle,
DECLARE_XBOXKRNL_EXPORT3(NtSignalAndWaitForSingleObjectEx, kThreading, DECLARE_XBOXKRNL_EXPORT3(NtSignalAndWaitForSingleObjectEx, kThreading,
kImplemented, kBlocking, kHighFrequency); kImplemented, kBlocking, kHighFrequency);
uint32_t keKfAcquireSpinLock(uint32_t* lock) { uint32_t xeKeKfAcquireSpinLock(uint32_t* lock) {
// XELOGD( // XELOGD(
// "KfAcquireSpinLock(%.8X)", // "KfAcquireSpinLock(%.8X)",
// lock_ptr); // lock_ptr);
@ -881,12 +895,12 @@ uint32_t keKfAcquireSpinLock(uint32_t* lock) {
dword_result_t KfAcquireSpinLock(lpdword_t lock_ptr) { dword_result_t KfAcquireSpinLock(lpdword_t lock_ptr) {
auto lock = reinterpret_cast<uint32_t*>(lock_ptr.host_address()); auto lock = reinterpret_cast<uint32_t*>(lock_ptr.host_address());
return keKfAcquireSpinLock(lock); return xeKeKfAcquireSpinLock(lock);
} }
DECLARE_XBOXKRNL_EXPORT3(KfAcquireSpinLock, kThreading, kImplemented, kBlocking, DECLARE_XBOXKRNL_EXPORT3(KfAcquireSpinLock, kThreading, kImplemented, kBlocking,
kHighFrequency); kHighFrequency);
void keKfReleaseSpinLock(uint32_t* lock, dword_t old_irql) { void xeKeKfReleaseSpinLock(uint32_t* lock, dword_t old_irql) {
// Restore IRQL. // Restore IRQL.
XThread* thread = XThread::GetCurrentThread(); XThread* thread = XThread::GetCurrentThread();
thread->LowerIrql(old_irql); thread->LowerIrql(old_irql);
@ -897,7 +911,7 @@ void keKfReleaseSpinLock(uint32_t* lock, dword_t old_irql) {
void KfReleaseSpinLock(lpdword_t lock_ptr, dword_t old_irql) { void KfReleaseSpinLock(lpdword_t lock_ptr, dword_t old_irql) {
auto lock = reinterpret_cast<uint32_t*>(lock_ptr.host_address()); auto lock = reinterpret_cast<uint32_t*>(lock_ptr.host_address());
keKfReleaseSpinLock(lock, old_irql); xeKeKfReleaseSpinLock(lock, old_irql);
} }
DECLARE_XBOXKRNL_EXPORT2(KfReleaseSpinLock, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT2(KfReleaseSpinLock, kThreading, kImplemented,
kHighFrequency); kHighFrequency);
@ -1130,26 +1144,26 @@ void ExInitializeReadWriteLock(pointer_t<X_ERWLOCK> lock_ptr) {
DECLARE_XBOXKRNL_EXPORT1(ExInitializeReadWriteLock, kThreading, kImplemented); DECLARE_XBOXKRNL_EXPORT1(ExInitializeReadWriteLock, kThreading, kImplemented);
void ExAcquireReadWriteLockExclusive(pointer_t<X_ERWLOCK> lock_ptr) { void ExAcquireReadWriteLockExclusive(pointer_t<X_ERWLOCK> lock_ptr) {
auto old_irql = keKfAcquireSpinLock(&lock_ptr->spin_lock); auto old_irql = xeKeKfAcquireSpinLock(&lock_ptr->spin_lock);
lock_ptr->lock_count++; lock_ptr->lock_count++;
keKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql); xeKeKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql);
if (!lock_ptr->lock_count) { if (!lock_ptr->lock_count) {
return; return;
} }
lock_ptr->writers_waiting_count++; lock_ptr->writers_waiting_count++;
keWaitForSingleObject(&lock_ptr->writer_event, 0, 0, 0, nullptr); xeKeWaitForSingleObject(&lock_ptr->writer_event, 0, 0, 0, nullptr);
} }
DECLARE_XBOXKRNL_EXPORT4(ExAcquireReadWriteLockExclusive, kThreading, DECLARE_XBOXKRNL_EXPORT4(ExAcquireReadWriteLockExclusive, kThreading,
kImplemented, kBlocking, kHighFrequency, kSketchy); kImplemented, kBlocking, kHighFrequency, kSketchy);
void ExReleaseReadWriteLock(pointer_t<X_ERWLOCK> lock_ptr) { void ExReleaseReadWriteLock(pointer_t<X_ERWLOCK> lock_ptr) {
auto old_irql = keKfAcquireSpinLock(&lock_ptr->spin_lock); auto old_irql = xeKeKfAcquireSpinLock(&lock_ptr->spin_lock);
lock_ptr->lock_count--; lock_ptr->lock_count--;
if (lock_ptr->lock_count < 0) { if (lock_ptr->lock_count < 0) {
keKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql); xeKeKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql);
return; return;
} }
@ -1158,17 +1172,17 @@ void ExReleaseReadWriteLock(pointer_t<X_ERWLOCK> lock_ptr) {
if (readers_waiting_count) { if (readers_waiting_count) {
lock_ptr->readers_waiting_count = 0; lock_ptr->readers_waiting_count = 0;
lock_ptr->readers_entry_count = readers_waiting_count; lock_ptr->readers_entry_count = readers_waiting_count;
keKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql); xeKeKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql);
keReleaseSemaphore(&lock_ptr->reader_semaphore, 1, readers_waiting_count, xeKeReleaseSemaphore(&lock_ptr->reader_semaphore, 1,
0); readers_waiting_count, 0);
return; return;
} }
} }
auto count = lock_ptr->readers_entry_count--; auto count = lock_ptr->readers_entry_count--;
keKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql); xeKeKfReleaseSpinLock(&lock_ptr->spin_lock, old_irql);
if (!count) { if (!count) {
keSetEvent(&lock_ptr->writer_event, 1, 0); xeKeSetEvent(&lock_ptr->writer_event, 1, 0);
} }
} }
DECLARE_XBOXKRNL_EXPORT2(ExReleaseReadWriteLock, kThreading, kImplemented, DECLARE_XBOXKRNL_EXPORT2(ExReleaseReadWriteLock, kThreading, kImplemented,

View File

@ -19,19 +19,18 @@ struct X_KEVENT;
namespace xboxkrnl { namespace xboxkrnl {
dword_result_t NtSetEvent(dword_t handle, lpdword_t previous_state_ptr); uint32_t xeNtSetEvent(uint32_t handle, xe::be<uint32_t>* previous_state_ptr);
dword_result_t NtClearEvent(dword_t handle); uint32_t xeNtClearEvent(uint32_t handle);
dword_result_t NtWaitForMultipleObjectsEx(dword_t count, lpdword_t handles, uint32_t xeNtWaitForMultipleObjectsEx(uint32_t count, xe::be<uint32_t>* handles,
dword_t wait_type, dword_t wait_mode, uint32_t wait_type, uint32_t wait_mode,
dword_t alertable, uint32_t alertable,
lpqword_t timeout_ptr); uint64_t* timeout_ptr);
dword_result_t KeWaitForSingleObject(lpvoid_t object_ptr, dword_t wait_reason, uint32_t xeKeWaitForSingleObject(void* object_ptr, uint32_t wait_reason,
dword_t processor_mode, dword_t alertable, uint32_t processor_mode, uint32_t alertable,
lpqword_t timeout_ptr); uint64_t* timeout_ptr);
dword_result_t KeSetEvent(pointer_t<X_KEVENT> event_ptr, dword_t increment, uint32_t xeKeSetEvent(X_KEVENT* event_ptr, uint32_t increment, uint32_t wait);
dword_t wait);
} // namespace xboxkrnl } // namespace xboxkrnl
} // namespace kernel } // namespace kernel