diff options
Diffstat (limited to 'src/core/hle/svc.cpp')
-rw-r--r-- | src/core/hle/svc.cpp | 151 |
1 files changed, 81 insertions, 70 deletions
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index 1eda36c5..43a3cbe0 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp @@ -1,6 +1,6 @@ // Copyright 2014 Citra Emulator Project // Licensed under GPLv2 -// Refer to the license.txt file included. +// Refer to the license.txt file included. #include <map> @@ -16,6 +16,7 @@ #include "core/hle/kernel/thread.h" #include "core/hle/function_wrappers.h" +#include "core/hle/result.h" #include "core/hle/service/service.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -29,8 +30,8 @@ enum ControlMemoryOperation { }; /// Map application or GSP heap memory -Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) { - DEBUG_LOG(SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X", +static Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) { + DEBUG_LOG(SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X", operation, addr0, addr1, size, permissions); switch (operation) { @@ -53,8 +54,8 @@ Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 siz } /// Maps a memory block to specified address -Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) { - DEBUG_LOG(SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d", +static Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) { + DEBUG_LOG(SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d", handle, addr, permissions, other_permissions); Kernel::MemoryPermission permissions_type = static_cast<Kernel::MemoryPermission>(permissions); @@ -63,7 +64,7 @@ Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permis case Kernel::MemoryPermission::Write: case Kernel::MemoryPermission::ReadWrite: case Kernel::MemoryPermission::DontCare: - Kernel::MapSharedMemory(handle, addr, permissions_type, + Kernel::MapSharedMemory(handle, addr, permissions_type, static_cast<Kernel::MemoryPermission>(other_permissions)); break; default: @@ -73,7 +74,7 @@ Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permis } /// Connect to an OS service given the port name, returns the handle to the port to out -Result ConnectToPort(Handle* out, const char* port_name) { +static Result ConnectToPort(Handle* out, const char* port_name) { Service::Interface* service = Service::g_manager->FetchFromPortName(port_name); DEBUG_LOG(SVC, "called port_name=%s", port_name); @@ -85,78 +86,83 @@ Result ConnectToPort(Handle* out, const char* port_name) { } /// Synchronize to an OS service -Result SendSyncRequest(Handle handle) { +static Result SendSyncRequest(Handle handle) { + // TODO(yuriks): ObjectPool::Get tries to check the Object type, which fails since this is a generic base Object, + // so we are forced to use GetFast and manually verify the handle. + if (!Kernel::g_object_pool.IsValid(handle)) { + return InvalidHandle(ErrorModule::Kernel).raw; + } Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle); _assert_msg_(KERNEL, (object != nullptr), "called, but kernel object is nullptr!"); DEBUG_LOG(SVC, "called handle=0x%08X(%s)", handle, object->GetTypeName().c_str()); - bool wait = false; - Result res = object->SyncRequest(&wait); - if (wait) { + ResultVal<bool> wait = object->SyncRequest(); + if (wait.Succeeded() && *wait) { Kernel::WaitCurrentThread(WAITTYPE_SYNCH); // TODO(bunnei): Is this correct? } - return res; + return wait.Code().raw; } /// Close a handle -Result CloseHandle(Handle handle) { +static Result CloseHandle(Handle handle) { // ImplementMe ERROR_LOG(SVC, "(UNIMPLEMENTED) called handle=0x%08X", handle); return 0; } /// Wait for a handle to synchronize, timeout after the specified nanoseconds -Result WaitSynchronization1(Handle handle, s64 nano_seconds) { +static Result WaitSynchronization1(Handle handle, s64 nano_seconds) { // TODO(bunnei): Do something with nano_seconds, currently ignoring this - bool wait = false; bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated + if (!Kernel::g_object_pool.IsValid(handle)) { + return InvalidHandle(ErrorModule::Kernel).raw; + } Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle); + _dbg_assert_(KERNEL, object != nullptr); - DEBUG_LOG(SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle, object->GetTypeName().c_str(), + DEBUG_LOG(SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle, object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds); - _assert_msg_(KERNEL, (object != nullptr), "called, but kernel object is nullptr!"); - - Result res = object->WaitSynchronization(&wait); + ResultVal<bool> wait = object->WaitSynchronization(); // Check for next thread to schedule - if (wait) { + if (wait.Succeeded() && *wait) { HLE::Reschedule(__func__); - return 0; } - return res; + return wait.Code().raw; } /// Wait for the given handles to synchronize, timeout after the specified nanoseconds -Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all, +static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all, s64 nano_seconds) { // TODO(bunnei): Do something with nano_seconds, currently ignoring this bool unlock_all = true; bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated - DEBUG_LOG(SVC, "called handle_count=%d, wait_all=%s, nanoseconds=%lld", + DEBUG_LOG(SVC, "called handle_count=%d, wait_all=%s, nanoseconds=%lld", handle_count, (wait_all ? "true" : "false"), nano_seconds); // Iterate through each handle, synchronize kernel object for (s32 i = 0; i < handle_count; i++) { - bool wait = false; + if (!Kernel::g_object_pool.IsValid(handles[i])) { + return InvalidHandle(ErrorModule::Kernel).raw; + } Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handles[i]); - _assert_msg_(KERNEL, (object != nullptr), "called handle=0x%08X, but kernel object " - "is nullptr!", handles[i]); - - DEBUG_LOG(SVC, "\thandle[%d] = 0x%08X(%s:%s)", i, handles[i], object->GetTypeName().c_str(), + DEBUG_LOG(SVC, "\thandle[%d] = 0x%08X(%s:%s)", i, handles[i], object->GetTypeName().c_str(), object->GetName().c_str()); - Result res = object->WaitSynchronization(&wait); + // TODO(yuriks): Verify how the real function behaves when an error happens here + ResultVal<bool> wait_result = object->WaitSynchronization(); + bool wait = wait_result.Succeeded() && *wait_result; if (!wait && !wait_all) { *out = i; - return 0; + return RESULT_SUCCESS.raw; } else { unlock_all = false; } @@ -164,17 +170,17 @@ Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wa if (wait_all && unlock_all) { *out = handle_count; - return 0; + return RESULT_SUCCESS.raw; } // Check for next thread to schedule HLE::Reschedule(__func__); - return 0; + return RESULT_SUCCESS.raw; } /// Create an address arbiter (to allocate access to shared resources) -Result CreateAddressArbiter(u32* arbiter) { +static Result CreateAddressArbiter(u32* arbiter) { DEBUG_LOG(SVC, "called"); Handle handle = Kernel::CreateAddressArbiter(); *arbiter = handle; @@ -182,20 +188,22 @@ Result CreateAddressArbiter(u32* arbiter) { } /// Arbitrate address -Result ArbitrateAddress(Handle arbiter, u32 address, u32 type, u32 value, s64 nanoseconds) { - return Kernel::ArbitrateAddress(arbiter, static_cast<Kernel::ArbitrationType>(type), address, - value); +static Result ArbitrateAddress(Handle arbiter, u32 address, u32 type, u32 value, s64 nanoseconds) { + DEBUG_LOG(SVC, "called handle=0x%08X, address=0x%08X, type=0x%08X, value=0x%08X", arbiter, + address, type, value); + return Kernel::ArbitrateAddress(arbiter, static_cast<Kernel::ArbitrationType>(type), + address, value).raw; } /// Used to output a message on a debug hardware unit - does nothing on a retail unit -void OutputDebugString(const char* string) { +static void OutputDebugString(const char* string) { OS_LOG(SVC, "%s", string); } /// Get resource limit -Result GetResourceLimit(Handle* resource_limit, Handle process) { +static Result GetResourceLimit(Handle* resource_limit, Handle process) { // With regards to proceess values: - // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for + // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for // the current KThread. *resource_limit = 0xDEADBEEF; ERROR_LOG(SVC, "(UNIMPLEMENTED) called process=0x%08X", process); @@ -203,7 +211,7 @@ Result GetResourceLimit(Handle* resource_limit, Handle process) { } /// Get resource limit current values -Result GetResourceLimitCurrentValues(s64* values, Handle resource_limit, void* names, +static Result GetResourceLimitCurrentValues(s64* values, Handle resource_limit, void* names, s32 name_count) { ERROR_LOG(SVC, "(UNIMPLEMENTED) called resource_limit=%08X, names=%s, name_count=%d", resource_limit, names, name_count); @@ -212,7 +220,7 @@ Result GetResourceLimitCurrentValues(s64* values, Handle resource_limit, void* n } /// Creates a new thread -Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) { +static Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) { std::string name; if (Symbols::HasSymbol(entry_point)) { TSymbol symbol = Symbols::GetSymbol(entry_point); @@ -227,14 +235,14 @@ Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 p Core::g_app_core->SetReg(1, thread); DEBUG_LOG(SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, " - "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point, + "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point, name.c_str(), arg, stack_top, priority, processor_id, thread); - + return 0; } /// Called when a thread exits -u32 ExitThread() { +static u32 ExitThread() { Handle thread = Kernel::GetCurrentThreadHandle(); DEBUG_LOG(SVC, "called, pc=0x%08X", Core::g_app_core->GetPC()); // PC = 0x0010545C @@ -245,54 +253,56 @@ u32 ExitThread() { } /// Gets the priority for the specified thread -Result GetThreadPriority(s32* priority, Handle handle) { - *priority = Kernel::GetThreadPriority(handle); - return 0; +static Result GetThreadPriority(s32* priority, Handle handle) { + ResultVal<u32> priority_result = Kernel::GetThreadPriority(handle); + if (priority_result.Succeeded()) { + *priority = *priority_result; + } + return priority_result.Code().raw; } /// Sets the priority for the specified thread -Result SetThreadPriority(Handle handle, s32 priority) { - return Kernel::SetThreadPriority(handle, priority); +static Result SetThreadPriority(Handle handle, s32 priority) { + return Kernel::SetThreadPriority(handle, priority).raw; } /// Create a mutex -Result CreateMutex(Handle* mutex, u32 initial_locked) { +static Result CreateMutex(Handle* mutex, u32 initial_locked) { *mutex = Kernel::CreateMutex((initial_locked != 0)); - DEBUG_LOG(SVC, "called initial_locked=%s : created handle=0x%08X", + DEBUG_LOG(SVC, "called initial_locked=%s : created handle=0x%08X", initial_locked ? "true" : "false", *mutex); return 0; } /// Release a mutex -Result ReleaseMutex(Handle handle) { +static Result ReleaseMutex(Handle handle) { DEBUG_LOG(SVC, "called handle=0x%08X", handle); - _assert_msg_(KERNEL, (handle != 0), "called, but handle is nullptr!"); - Kernel::ReleaseMutex(handle); - return 0; + ResultCode res = Kernel::ReleaseMutex(handle); + return res.raw; } /// Get current thread ID -Result GetThreadId(u32* thread_id, Handle thread) { +static Result GetThreadId(u32* thread_id, Handle thread) { ERROR_LOG(SVC, "(UNIMPLEMENTED) called thread=0x%08X", thread); return 0; } /// Query memory -Result QueryMemory(void* info, void* out, u32 addr) { +static Result QueryMemory(void* info, void* out, u32 addr) { ERROR_LOG(SVC, "(UNIMPLEMENTED) called addr=0x%08X", addr); return 0; } /// Create an event -Result CreateEvent(Handle* evt, u32 reset_type) { +static Result CreateEvent(Handle* evt, u32 reset_type) { *evt = Kernel::CreateEvent((ResetType)reset_type); - DEBUG_LOG(SVC, "called reset_type=0x%08X : created handle=0x%08X", + DEBUG_LOG(SVC, "called reset_type=0x%08X : created handle=0x%08X", reset_type, *evt); return 0; } /// Duplicates a kernel handle -Result DuplicateHandle(Handle* out, Handle handle) { +static Result DuplicateHandle(Handle* out, Handle handle) { DEBUG_LOG(SVC, "called handle=0x%08X", handle); // Translate kernel handles -> real handles @@ -301,7 +311,7 @@ Result DuplicateHandle(Handle* out, Handle handle) { } _assert_msg_(KERNEL, (handle != Kernel::CurrentProcess), "(UNIMPLEMENTED) process handle duplication!"); - + // TODO(bunnei): FixMe - This is a hack to return the handle that we were asked to duplicate. *out = handle; @@ -309,26 +319,27 @@ Result DuplicateHandle(Handle* out, Handle handle) { } /// Signals an event -Result SignalEvent(Handle evt) { - Result res = Kernel::SignalEvent(evt); +static Result SignalEvent(Handle evt) { DEBUG_LOG(SVC, "called event=0x%08X", evt); - return res; + return Kernel::SignalEvent(evt).raw; } /// Clears an event -Result ClearEvent(Handle evt) { - Result res = Kernel::ClearEvent(evt); +static Result ClearEvent(Handle evt) { DEBUG_LOG(SVC, "called event=0x%08X", evt); - return res; + return Kernel::ClearEvent(evt).raw; } /// Sleep the current thread -void SleepThread(s64 nanoseconds) { +static void SleepThread(s64 nanoseconds) { DEBUG_LOG(SVC, "called nanoseconds=%lld", nanoseconds); + + // Check for next thread to schedule + HLE::Reschedule(__func__); } /// This returns the total CPU ticks elapsed since the CPU was powered-on -s64 GetSystemTick() { +static s64 GetSystemTick() { return (s64)Core::g_app_core->GetTicks(); } |