From 4251eb26eca3f6df8360f9b6aa6762340817c48a Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 1 Jan 2017 19:07:37 -0500 Subject: [PATCH 1/3] Kernel/Semaphore: Fixed a regression in semaphore waits. The regression was caused by a missing check in #2260. The new behavior is consistent with the real kernel. --- src/core/hle/kernel/semaphore.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index 5e6139265..8bda2f75d 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -35,7 +35,8 @@ bool Semaphore::ShouldWait(Thread* thread) const { } void Semaphore::Acquire(Thread* thread) { - ASSERT_MSG(!ShouldWait(thread), "object unavailable!"); + if (available_count <= 0) + return; --available_count; } From fc2266130b450952777cd3a9e47adb36960de8e7 Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 5 Jan 2017 14:14:22 -0500 Subject: [PATCH 2/3] Kernel: Don't attempt to yield execution in SleepThread(0) if there are no available threads to run. With this we avoid an useless temporary deschedule of the current thread. --- src/core/hle/kernel/thread.cpp | 4 ++++ src/core/hle/kernel/thread.h | 5 +++++ src/core/hle/svc.cpp | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 9109bd10b..6cd9ff327 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -556,6 +556,10 @@ SharedPtr SetupMainThread(u32 entry_point, s32 priority) { return thread; } +bool HaveReadyThreads() { + return ready_queue.get_first() != nullptr; +} + void Reschedule() { PriorityBoostStarvedThreads(); diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index af72b76ea..13a29a044 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -218,6 +218,11 @@ private: */ SharedPtr SetupMainThread(u32 entry_point, s32 priority); +/** + * Returns whether there are any threads that are ready to run. + */ +bool HaveReadyThreads(); + /** * Reschedules to the next available thread (call after current thread is suspended) */ diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 855f3af82..50d35ab97 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -849,6 +849,11 @@ static ResultCode CancelTimer(Kernel::Handle handle) { static void SleepThread(s64 nanoseconds) { LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds); + // Don't attempt to yield execution if there are no available threads to run, + // this way we avoid a useless reschedule to the idle thread. + if (nanoseconds == 0 && !Kernel::HaveReadyThreads()) + return; + // Sleep current thread and check for next thread to schedule Kernel::WaitCurrentThread_Sleep(); From f0199a17f6cee85bd9e88501c0961f001c396687 Mon Sep 17 00:00:00 2001 From: Hyper Date: Sat, 7 Jan 2017 02:21:22 +1300 Subject: [PATCH 3/3] Kernel: Fix SharedMemory objects always returning error when addr = 0 (#2404) Closes #2400 --- src/core/hle/svc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 855f3af82..843d697ec 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -897,7 +897,11 @@ static ResultCode CreateMemoryBlock(Kernel::Handle* out_handle, u32 addr, u32 si return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); - if (addr < Memory::PROCESS_IMAGE_VADDR || addr + size > Memory::SHARED_MEMORY_VADDR_END) { + // TODO(Subv): Processes with memory type APPLICATION are not allowed + // to create memory blocks with addr = 0, any attempts to do so + // should return error 0xD92007EA. + if ((addr < Memory::PROCESS_IMAGE_VADDR || addr + size > Memory::SHARED_MEMORY_VADDR_END) && + addr != 0) { return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); }