diff options
m--------- | externals/dynarmic | 0 | ||||
-rw-r--r-- | src/core/hle/kernel/svc.cpp | 157 | ||||
-rw-r--r-- | src/core/hle/service/nvdrv/devices/nvhost_ctrl.h | 88 | ||||
-rw-r--r-- | src/core/hle/service/nvdrv/devices/nvmap.cpp | 12 | ||||
-rw-r--r-- | src/core/hle/service/nvdrv/devices/nvmap.h | 46 |
5 files changed, 191 insertions, 112 deletions
diff --git a/externals/dynarmic b/externals/dynarmic -Subproject 5e20e5a44a3ab6667e640f26d088ae4f9e561f0 +Subproject 990a569b7a5f2518fe08682f5ebf8536e5388d6 diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 1ae530c90..0811a16b8 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -145,36 +145,6 @@ static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thr return true; }; -/// Wait for a kernel object to synchronize, timeout after the specified nanoseconds -static ResultCode WaitSynchronization1( - SharedPtr<WaitObject> object, Thread* thread, s64 nano_seconds = -1, - std::function<Thread::WakeupCallback> wakeup_callback = DefaultThreadWakeupCallback) { - - if (!object) { - return ERR_INVALID_HANDLE; - } - - if (object->ShouldWait(thread)) { - if (nano_seconds == 0) { - return RESULT_TIMEOUT; - } - - thread->wait_objects = {object}; - object->AddWaitingThread(thread); - thread->status = THREADSTATUS_WAIT_SYNCH_ANY; - - // Create an event to wake the thread up after the specified nanosecond delay has passed - thread->WakeAfterDelay(nano_seconds); - thread->wakeup_callback = wakeup_callback; - - Core::System::GetInstance().PrepareReschedule(); - } else { - object->Acquire(thread); - } - - return RESULT_SUCCESS; -} - /// Wait for the given handles to synchronize, timeout after the specified nanoseconds static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count, s64 nano_seconds) { @@ -232,7 +202,7 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 thread->WakeAfterDelay(nano_seconds); thread->wakeup_callback = DefaultThreadWakeupCallback; - Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_TIMEOUT; } @@ -395,7 +365,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { thread->SetPriority(priority); - Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_SUCCESS; } @@ -552,6 +522,7 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V *out_handle = thread->guest_handle; Core::System::GetInstance().PrepareReschedule(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); NGLOG_TRACE(Kernel_SVC, "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, " @@ -570,7 +541,10 @@ static ResultCode StartThread(Handle thread_handle) { return ERR_INVALID_HANDLE; } + ASSERT(thread->status == THREADSTATUS_DORMANT); + thread->ResumeFromWait(); + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); return RESULT_SUCCESS; } @@ -634,61 +608,78 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target NGLOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}", condition_variable_addr, target); - u32 processed = 0; + auto RetrieveWaitingThreads = + [](size_t core_index, std::vector<SharedPtr<Thread>>& waiting_threads, VAddr condvar_addr) { + const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); + auto& thread_list = scheduler->GetThreadList(); - auto signal_process_wide_key = [&](size_t core_index) { - const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); - for (auto& thread : scheduler->GetThreadList()) { - if (thread->condvar_wait_address != condition_variable_addr) - continue; - - // Only process up to 'target' threads, unless 'target' is -1, in which case process - // them all. - if (target != -1 && processed >= target) - break; - - // If the mutex is not yet acquired, acquire it. - u32 mutex_val = Memory::Read32(thread->mutex_wait_address); - - if (mutex_val == 0) { - // We were able to acquire the mutex, resume this thread. - Memory::Write32(thread->mutex_wait_address, thread->wait_handle); - ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX); - thread->ResumeFromWait(); - - auto lock_owner = thread->lock_owner; - if (lock_owner) - lock_owner->RemoveMutexWaiter(thread); - - thread->lock_owner = nullptr; - thread->mutex_wait_address = 0; - thread->condvar_wait_address = 0; - thread->wait_handle = 0; - } else { - // Couldn't acquire the mutex, block the thread. - Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask); - auto owner = g_handle_table.Get<Thread>(owner_handle); - ASSERT(owner); - ASSERT(thread->status != THREADSTATUS_RUNNING); - thread->status = THREADSTATUS_WAIT_MUTEX; - thread->wakeup_callback = nullptr; - - // Signal that the mutex now has a waiting thread. - Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag); - - owner->AddMutexWaiter(thread); - - Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); + for (auto& thread : thread_list) { + if (thread->condvar_wait_address == condvar_addr) + waiting_threads.push_back(thread); } + }; + + // Retrieve a list of all threads that are waiting for this condition variable. + std::vector<SharedPtr<Thread>> waiting_threads; + RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr); + RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr); + // Sort them by priority, such that the highest priority ones come first. + std::sort(waiting_threads.begin(), waiting_threads.end(), + [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) { + return lhs->current_priority < rhs->current_priority; + }); + + // Only process up to 'target' threads, unless 'target' is -1, in which case process + // them all. + size_t last = waiting_threads.size(); + if (target != -1) + last = target; + + // If there are no threads waiting on this condition variable, just exit + if (last > waiting_threads.size()) + return RESULT_SUCCESS; - ++processed; - } - }; + for (size_t index = 0; index < last; ++index) { + auto& thread = waiting_threads[index]; + + ASSERT(thread->condvar_wait_address == condition_variable_addr); + + // If the mutex is not yet acquired, acquire it. + u32 mutex_val = Memory::Read32(thread->mutex_wait_address); - signal_process_wide_key(0); - signal_process_wide_key(1); - signal_process_wide_key(2); - signal_process_wide_key(3); + if (mutex_val == 0) { + // We were able to acquire the mutex, resume this thread. + Memory::Write32(thread->mutex_wait_address, thread->wait_handle); + ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX); + thread->ResumeFromWait(); + + auto lock_owner = thread->lock_owner; + if (lock_owner) + lock_owner->RemoveMutexWaiter(thread); + + thread->lock_owner = nullptr; + thread->mutex_wait_address = 0; + thread->condvar_wait_address = 0; + thread->wait_handle = 0; + } else { + // Couldn't acquire the mutex, block the thread. + Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask); + auto owner = g_handle_table.Get<Thread>(owner_handle); + ASSERT(owner); + ASSERT(thread->status != THREADSTATUS_RUNNING); + thread->status = THREADSTATUS_WAIT_MUTEX; + thread->wakeup_callback = nullptr; + + // Signal that the mutex now has a waiting thread. + Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag); + + owner->AddMutexWaiter(thread); + + Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule(); + } + } return RESULT_SUCCESS; } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h index 76a8b33c2..aa9b5a14b 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h @@ -26,12 +26,64 @@ private: IocSyncptIncrCommand = 0x40040015, IocSyncptWaitCommand = 0xC00C0016, IocModuleMutexCommand = 0x40080017, - IocModuleRegRDWRCommand = 0xC008010E, + IocModuleRegRDWRCommand = 0xC0180018, IocSyncptWaitexCommand = 0xC0100019, IocSyncptReadMaxCommand = 0xC008001A, - IocCtrlEventWaitCommand = 0xC010001D, IocGetConfigCommand = 0xC183001B, + IocCtrlEventSignalCommand = 0xC004001C, + IocCtrlEventWaitCommand = 0xC010001D, + IocCtrlEventWaitAsyncCommand = 0xC010001E, + IocCtrlEventRegisterCommand = 0xC004001F, + IocCtrlEventUnregisterCommand = 0xC0040020, + IocCtrlEventKillCommand = 0x40080021, + }; + struct IocSyncptReadParams { + u32_le id; + u32_le value; + }; + static_assert(sizeof(IocSyncptReadParams) == 8, "IocSyncptReadParams is incorrect size"); + + struct IocSyncptIncrParams { + u32_le id; + }; + static_assert(sizeof(IocSyncptIncrParams) == 4, "IocSyncptIncrParams is incorrect size"); + + struct IocSyncptWaitParams { + u32_le id; + u32_le thresh; + s32_le timeout; + }; + static_assert(sizeof(IocSyncptWaitParams) == 12, "IocSyncptWaitParams is incorrect size"); + + struct IocModuleMutexParams { + u32_le id; + u32_le lock; // (0 = unlock and 1 = lock) + }; + static_assert(sizeof(IocModuleMutexParams) == 8, "IocModuleMutexParams is incorrect size"); + + struct IocModuleRegRDWRParams { + u32_le id; + u32_le num_offsets; + u32_le block_size; + u32_le offsets; + u32_le values; + u32_le write; + }; + static_assert(sizeof(IocModuleRegRDWRParams) == 24, "IocModuleRegRDWRParams is incorrect size"); + + struct IocSyncptWaitexParams { + u32_le id; + u32_le thresh; + s32_le timeout; + u32_le value; }; + static_assert(sizeof(IocSyncptWaitexParams) == 16, "IocSyncptWaitexParams is incorrect size"); + + struct IocSyncptReadMaxParams { + u32_le id; + u32_le value; + }; + static_assert(sizeof(IocSyncptReadMaxParams) == 8, "IocSyncptReadMaxParams is incorrect size"); struct IocGetConfigParams { std::array<char, 0x41> domain_str; @@ -40,6 +92,12 @@ private: }; static_assert(sizeof(IocGetConfigParams) == 387, "IocGetConfigParams is incorrect size"); + struct IocCtrlEventSignalParams { + u32_le user_event_id; + }; + static_assert(sizeof(IocCtrlEventSignalParams) == 4, + "IocCtrlEventSignalParams is incorrect size"); + struct IocCtrlEventWaitParams { u32_le syncpt_id; u32_le threshold; @@ -48,6 +106,32 @@ private: }; static_assert(sizeof(IocCtrlEventWaitParams) == 16, "IocCtrlEventWaitParams is incorrect size"); + struct IocCtrlEventWaitAsyncParams { + u32_le syncpt_id; + u32_le threshold; + u32_le timeout; + u32_le value; + }; + static_assert(sizeof(IocCtrlEventWaitAsyncParams) == 16, + "IocCtrlEventWaitAsyncParams is incorrect size"); + + struct IocCtrlEventRegisterParams { + u32_le user_event_id; + }; + static_assert(sizeof(IocCtrlEventRegisterParams) == 4, + "IocCtrlEventRegisterParams is incorrect size"); + + struct IocCtrlEventUnregisterParams { + u32_le user_event_id; + }; + static_assert(sizeof(IocCtrlEventUnregisterParams) == 4, + "IocCtrlEventUnregisterParams is incorrect size"); + + struct IocCtrlEventKill { + u64_le user_events; + }; + static_assert(sizeof(IocCtrlEventKill) == 8, "IocCtrlEventKill is incorrect size"); + u32 NvOsGetConfigU32(const std::vector<u8>& input, std::vector<u8>& output); u32 IocCtrlEventWait(const std::vector<u8>& input, std::vector<u8>& output); diff --git a/src/core/hle/service/nvdrv/devices/nvmap.cpp b/src/core/hle/service/nvdrv/devices/nvmap.cpp index d66fb3a9c..23fe98190 100644 --- a/src/core/hle/service/nvdrv/devices/nvmap.cpp +++ b/src/core/hle/service/nvdrv/devices/nvmap.cpp @@ -119,25 +119,25 @@ u32 nvmap::IocParam(const std::vector<u8>& input, std::vector<u8>& output) { IocParamParams params; std::memcpy(¶ms, input.data(), sizeof(params)); - NGLOG_WARNING(Service_NVDRV, "(STUBBED) called type={}", params.type); + NGLOG_WARNING(Service_NVDRV, "(STUBBED) called type={}", params.param); auto object = GetObject(params.handle); ASSERT(object); ASSERT(object->status == Object::Status::Allocated); - switch (static_cast<ParamTypes>(params.type)) { + switch (static_cast<ParamTypes>(params.param)) { case ParamTypes::Size: - params.value = object->size; + params.result = object->size; break; case ParamTypes::Alignment: - params.value = object->align; + params.result = object->align; break; case ParamTypes::Heap: // TODO(Subv): Seems to be a hardcoded value? - params.value = 0x40000000; + params.result = 0x40000000; break; case ParamTypes::Kind: - params.value = object->kind; + params.result = object->kind; break; default: UNIMPLEMENTED(); diff --git a/src/core/hle/service/nvdrv/devices/nvmap.h b/src/core/hle/service/nvdrv/devices/nvmap.h index 5a3044167..39fafaa7c 100644 --- a/src/core/hle/service/nvdrv/devices/nvmap.h +++ b/src/core/hle/service/nvdrv/devices/nvmap.h @@ -59,17 +59,25 @@ private: Create = 0xC0080101, FromId = 0xC0080103, Alloc = 0xC0200104, + Free = 0xC0180105, Param = 0xC00C0109, GetId = 0xC008010E, - Free = 0xC0180105, }; - struct IocCreateParams { // Input u32_le size; // Output u32_le handle; }; + static_assert(sizeof(IocCreateParams) == 8, "IocCreateParams has wrong size"); + + struct IocFromIdParams { + // Input + u32_le id; + // Output + u32_le handle; + }; + static_assert(sizeof(IocFromIdParams) == 8, "IocFromIdParams has wrong size"); struct IocAllocParams { // Input @@ -81,37 +89,33 @@ private: INSERT_PADDING_BYTES(7); u64_le addr; }; + static_assert(sizeof(IocAllocParams) == 32, "IocAllocParams has wrong size"); - struct IocGetIdParams { - // Output - u32_le id; - // Input - u32_le handle; - }; - - struct IocFromIdParams { - // Input - u32_le id; - // Output + struct IocFreeParams { u32_le handle; + INSERT_PADDING_BYTES(4); + u64_le refcount; + u32_le size; + u32_le flags; }; + static_assert(sizeof(IocFreeParams) == 24, "IocFreeParams has wrong size"); struct IocParamParams { // Input u32_le handle; - u32_le type; + u32_le param; // Output - u32_le value; + u32_le result; }; + static_assert(sizeof(IocParamParams) == 12, "IocParamParams has wrong size"); - struct IocFreeParams { + struct IocGetIdParams { + // Output + u32_le id; + // Input u32_le handle; - INSERT_PADDING_BYTES(4); - u64_le refcount; - u32_le size; - u32_le flags; }; - static_assert(sizeof(IocFreeParams) == 24, "IocFreeParams has wrong size"); + static_assert(sizeof(IocGetIdParams) == 8, "IocGetIdParams has wrong size"); u32 IocCreate(const std::vector<u8>& input, std::vector<u8>& output); u32 IocAlloc(const std::vector<u8>& input, std::vector<u8>& output); |