From a2cc3b10bb6115b17d980fdb83ed5c561835eb3b Mon Sep 17 00:00:00 2001 From: David Marcec Date: Mon, 26 Nov 2018 17:06:13 +1100 Subject: Changed logging to be "Log before execution", Added more error logging, all services should now log on some level --- src/core/hle/kernel/svc.cpp | 101 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 11 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index f287f7c97..775afccf6 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -69,37 +69,45 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr, u64 size) { if (!Common::Is4KBAligned(dst_addr) || !Common::Is4KBAligned(src_addr)) { + LOG_ERROR(Kernel_SVC, "Invalid address"); return ERR_INVALID_ADDRESS; } if (size == 0 || !Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Invalid size"); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(dst_addr, size)) { + LOG_ERROR(Kernel_SVC, "size is out of range"); return ERR_INVALID_ADDRESS_STATE; } if (!IsValidAddressRange(src_addr, size)) { + LOG_ERROR(Kernel_SVC, "size is out of range"); return ERR_INVALID_ADDRESS_STATE; } if (!IsInsideAddressSpace(vm_manager, src_addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is out of the address space"); return ERR_INVALID_ADDRESS_STATE; } if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is out of the address space"); return ERR_INVALID_MEMORY_RANGE; } const VAddr dst_end_address = dst_addr + size; if (dst_end_address > vm_manager.GetHeapRegionBaseAddress() && vm_manager.GetHeapRegionEndAddress() > dst_addr) { + LOG_ERROR(Kernel_SVC, "Region is not in the correct address space"); return ERR_INVALID_MEMORY_RANGE; } if (dst_end_address > vm_manager.GetMapRegionBaseAddress() && vm_manager.GetMapRegionEndAddress() > dst_addr) { + LOG_ERROR(Kernel_SVC, "Region is not in the correct address space"); return ERR_INVALID_MEMORY_RANGE; } @@ -113,6 +121,7 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB. if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) { + LOG_ERROR(Kernel_SVC, "Invalid heap size"); return ERR_INVALID_SIZE; } @@ -127,20 +136,24 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { LOG_TRACE(Kernel_SVC, "called, addr=0x{:X}, size=0x{:X}, prot=0x{:X}", addr, size, prot); if (!Common::Is4KBAligned(addr)) { + LOG_ERROR(Kernel_SVC, "Address is not aligned"); return ERR_INVALID_ADDRESS; } if (size == 0 || !Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Invalid size"); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is out of the address space"); return ERR_INVALID_ADDRESS_STATE; } const auto permission = static_cast(prot); if (permission != MemoryPermission::None && permission != MemoryPermission::Read && permission != MemoryPermission::ReadWrite) { + LOG_ERROR(Kernel_SVC, "Incorrect memory permissions"); return ERR_INVALID_MEMORY_PERMISSIONS; } @@ -148,11 +161,13 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { auto& vm_manager = current_process->VMManager(); if (!IsInsideAddressSpace(vm_manager, addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is not inside the address space"); return ERR_INVALID_ADDRESS_STATE; } const VMManager::VMAHandle iter = vm_manager.FindVMA(addr); if (iter == vm_manager.vma_map.end()) { + LOG_ERROR(Kernel_SVC, "Unable to find VMA"); return ERR_INVALID_ADDRESS_STATE; } @@ -182,6 +197,7 @@ static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result != RESULT_SUCCESS) { + LOG_ERROR(Kernel_SVC, "Map Memory failed"); return result; } @@ -198,6 +214,7 @@ static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result != RESULT_SUCCESS) { + LOG_ERROR(Kernel_SVC, "UnmapMemory failed"); return result; } @@ -207,6 +224,7 @@ static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { /// Connect to an OS service given the port name, returns the handle to the port to out static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) { if (!Memory::IsValidVirtualAddress(port_name_address)) { + LOG_ERROR(Kernel_SVC, "Invalid port name address"); return ERR_NOT_FOUND; } @@ -214,6 +232,7 @@ static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address // Read 1 char beyond the max allowed port name to detect names that are too long. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1); if (port_name.size() > PortNameMaxLength) { + LOG_ERROR(Kernel_SVC, "Port name is too long"); return ERR_OUT_OF_RANGE; } @@ -262,6 +281,7 @@ static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Invalid thread handle"); return ERR_INVALID_HANDLE; } @@ -276,6 +296,7 @@ static ResultCode GetProcessId(u32* process_id, Handle process_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr process = handle_table.Get(process_handle); if (!process) { + LOG_ERROR(Kernel_SVC, "Invalid process"); return ERR_INVALID_HANDLE; } @@ -305,12 +326,15 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}", handles_address, handle_count, nano_seconds); - if (!Memory::IsValidVirtualAddress(handles_address)) + if (!Memory::IsValidVirtualAddress(handles_address)) { + LOG_ERROR(Kernel_SVC, "Invalid handle address"); return ERR_INVALID_POINTER; + } static constexpr u64 MaxHandles = 0x40; if (handle_count > MaxHandles) { + LOG_ERROR(Kernel_SVC, "Handle count is too big"); return ERR_OUT_OF_RANGE; } @@ -325,6 +349,7 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 const auto object = handle_table.Get(handle); if (object == nullptr) { + LOG_ERROR(Kernel_SVC, "Object is a nullptr"); return ERR_INVALID_HANDLE; } @@ -348,11 +373,13 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 // If a timeout value of 0 was provided, just return the Timeout error code instead of // suspending the thread. - if (nano_seconds == 0) + if (nano_seconds == 0) { return RESULT_TIMEOUT; + } - for (auto& object : objects) + for (auto& object : objects) { object->AddWaitingThread(thread); + } thread->SetWaitObjects(std::move(objects)); thread->SetStatus(ThreadStatus::WaitSynchAny); @@ -373,6 +400,7 @@ static ResultCode CancelSynchronization(Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Invalid thread handle"); return ERR_INVALID_HANDLE; } @@ -391,10 +419,12 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr, holding_thread_handle, mutex_addr, requesting_thread_handle); if (Memory::IsKernelVirtualAddress(mutex_addr)) { + LOG_ERROR(Kernel_SVC, "Invalid mutex address"); return ERR_INVALID_ADDRESS_STATE; } if (!Common::IsWordAligned(mutex_addr)) { + LOG_ERROR(Kernel_SVC, "Mutex address is not aligned"); return ERR_INVALID_ADDRESS; } @@ -408,10 +438,12 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) { LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr); if (Memory::IsKernelVirtualAddress(mutex_addr)) { + LOG_ERROR(Kernel_SVC, "Invalid size"); return ERR_INVALID_ADDRESS_STATE; } if (!Common::IsWordAligned(mutex_addr)) { + LOG_ERROR(Kernel_SVC, "Mutex address is not aligned"); return ERR_INVALID_ADDRESS; } @@ -602,10 +634,12 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) break; case GetInfoType::RandomEntropy: if (handle != 0) { + LOG_ERROR(Kernel_SVC, "Non zero handle specified"); return ERR_INVALID_HANDLE; } if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) { + LOG_ERROR(Kernel_SVC, "Entropy size is too big"); return ERR_INVALID_COMBINATION; } @@ -643,12 +677,14 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) case GetInfoType::ThreadTickCount: { constexpr u64 num_cpus = 4; if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) { + LOG_ERROR(Kernel_SVC, "Incorrect info_sub_id"); return ERR_INVALID_COMBINATION; } const auto thread = current_process->GetHandleTable().Get(static_cast(handle)); if (!thread) { + LOG_ERROR(Kernel_SVC, "Thread is not a valid handle"); return ERR_INVALID_HANDLE; } @@ -691,14 +727,17 @@ static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { const auto* current_process = Core::CurrentProcess(); const SharedPtr thread = current_process->GetHandleTable().Get(handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Thread is not a valid handle"); return ERR_INVALID_HANDLE; } if (thread->GetOwnerProcess() != current_process) { + LOG_ERROR(Kernel_SVC, "Thread owner process is not the current process"); return ERR_INVALID_HANDLE; } if (thread == GetCurrentThread()) { + LOG_ERROR(Kernel_SVC, "Thread is already registered"); return ERR_ALREADY_REGISTERED; } @@ -719,9 +758,12 @@ static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { /// Gets the priority for the specified thread static ResultCode GetThreadPriority(u32* priority, Handle handle) { + LOG_TRACE(Kernel_SVC, "called"); + const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Invalid thread handle"); return ERR_INVALID_HANDLE; } @@ -731,7 +773,10 @@ static ResultCode GetThreadPriority(u32* priority, Handle handle) { /// Sets the priority for the specified thread static ResultCode SetThreadPriority(Handle handle, u32 priority) { + LOG_TRACE(Kernel_SVC, "called"); + if (priority > THREADPRIO_LOWEST) { + LOG_ERROR(Kernel_SVC, "Priority is out of range"); return ERR_INVALID_THREAD_PRIORITY; } @@ -739,6 +784,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { SharedPtr thread = current_process->GetHandleTable().Get(handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Invalid thread handle"); return ERR_INVALID_HANDLE; } @@ -761,14 +807,17 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s shared_memory_handle, addr, size, permissions); if (!Common::Is4KBAligned(addr)) { + LOG_ERROR(Kernel_SVC, "Address is not aligned"); return ERR_INVALID_ADDRESS; } if (size == 0 || !Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Invalid size"); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is not in the address space"); return ERR_INVALID_ADDRESS_STATE; } @@ -782,11 +831,13 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s auto* const current_process = Core::CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get(shared_memory_handle); if (!shared_memory) { + LOG_ERROR(Kernel_SVC, "Invalid shared memory handle"); return ERR_INVALID_HANDLE; } const auto& vm_manager = current_process->VMManager(); if (!vm_manager.IsWithinASLRRegion(addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region"); return ERR_INVALID_MEMORY_RANGE; } @@ -798,25 +849,30 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 shared_memory_handle, addr, size); if (!Common::Is4KBAligned(addr)) { + LOG_ERROR(Kernel_SVC, "Address is not aligned"); return ERR_INVALID_ADDRESS; } if (size == 0 || !Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is invalid"); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is not in the valid address range"); return ERR_INVALID_ADDRESS_STATE; } auto* const current_process = Core::CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get(shared_memory_handle); if (!shared_memory) { + LOG_ERROR(Kernel_SVC, "Shared memory is an invalid handle"); return ERR_INVALID_HANDLE; } const auto& vm_manager = current_process->VMManager(); if (!vm_manager.IsWithinASLRRegion(addr, size)) { + LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region"); return ERR_INVALID_MEMORY_RANGE; } @@ -826,9 +882,11 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 /// Query process memory static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/, Handle process_handle, u64 addr) { + LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr); const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); SharedPtr process = handle_table.Get(process_handle); if (!process) { + LOG_ERROR(Kernel_SVC, "Invalid process handle"); return ERR_INVALID_HANDLE; } auto vma = process->VMManager().FindVMA(addr); @@ -844,8 +902,6 @@ static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_i memory_info->size = vma->second.size; memory_info->type = static_cast(vma->second.meminfo_state); } - - LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr); return RESULT_SUCCESS; } @@ -874,7 +930,13 @@ static void ExitProcess() { /// Creates a new thread static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top, u32 priority, s32 processor_id) { + LOG_TRACE(Kernel_SVC, + "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, " + "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}", + entry_point, name, arg, stack_top, priority, processor_id, *out_handle); + if (priority > THREADPRIO_LOWEST) { + LOG_ERROR(Kernel_SVC, "Invalid thread priority"); return ERR_INVALID_THREAD_PRIORITY; } @@ -905,6 +967,8 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V const auto new_guest_handle = current_process->GetHandleTable().Create(thread); if (new_guest_handle.Failed()) { + LOG_ERROR(Kernel_SVC, "Failed to create handle with error=0x{:X}", + new_guest_handle.Code().raw); return new_guest_handle.Code(); } thread->SetGuestHandle(*new_guest_handle); @@ -912,11 +976,6 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); - LOG_TRACE(Kernel_SVC, - "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, " - "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}", - entry_point, name, arg, stack_top, priority, processor_id, *out_handle); - return RESULT_SUCCESS; } @@ -927,6 +986,7 @@ static ResultCode StartThread(Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); return ERR_INVALID_HANDLE; } @@ -1106,10 +1166,12 @@ static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout address, type, value, timeout); // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { + LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address"); return ERR_INVALID_ADDRESS_STATE; } // If the address is not properly aligned to 4 bytes, return invalid address. if (address % sizeof(u32) != 0) { + LOG_ERROR(Kernel_SVC, "Address is not aligned"); return ERR_INVALID_ADDRESS; } @@ -1121,6 +1183,7 @@ static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout case AddressArbiter::ArbitrationType::WaitIfEqual: return AddressArbiter::WaitForAddressIfEqual(address, value, timeout); default: + LOG_ERROR(Kernel_SVC, "Invalid arbitration type"); return ERR_INVALID_ENUM_VALUE; } } @@ -1131,10 +1194,12 @@ static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to address, type, value, num_to_wake); // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { + LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address"); return ERR_INVALID_ADDRESS_STATE; } // If the address is not properly aligned to 4 bytes, return invalid address. if (address % sizeof(u32) != 0) { + LOG_ERROR(Kernel_SVC, "Address is not aligned"); return ERR_INVALID_ADDRESS; } @@ -1147,12 +1212,15 @@ static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to return AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(address, value, num_to_wake); default: + LOG_ERROR(Kernel_SVC, "Invalid arbitration type"); return ERR_INVALID_ENUM_VALUE; } } /// This returns the total CPU ticks elapsed since the CPU was powered-on static u64 GetSystemTick() { + LOG_TRACE(Kernel_SVC, "called"); + const u64 result{CoreTiming::GetTicks()}; // Advance time to defeat dumb games that busy-wait for the frame to end. @@ -1226,6 +1294,7 @@ static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); return ERR_INVALID_HANDLE; } @@ -1236,12 +1305,13 @@ static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) } static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { - LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:16X}, core=0x{:X}", thread_handle, + LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:016X}, core=0x{:X}", thread_handle, mask, core); const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { + LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); return ERR_INVALID_HANDLE; } @@ -1256,6 +1326,7 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { } if (mask == 0) { + LOG_ERROR(Kernel_SVC, "Mask is 0"); return ERR_INVALID_COMBINATION; } @@ -1265,11 +1336,13 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { if (core == OnlyChangeMask) { core = thread->GetIdealCore(); } else if (core >= Core::NUM_CPU_CORES && core != static_cast(-1)) { + LOG_ERROR(Kernel_SVC, "Invalid processor ID specified"); return ERR_INVALID_PROCESSOR_ID; } // Error out if the input core isn't enabled in the input mask. if (core < Core::NUM_CPU_CORES && (mask & (1ull << core)) == 0) { + LOG_ERROR(Kernel_SVC, "Invalid core and mask"); return ERR_INVALID_COMBINATION; } @@ -1286,17 +1359,20 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss // Size must be a multiple of 4KB and be less than or equal to // approx. 8 GB (actually (1GB - 512B) * 8) if (size == 0 || (size & 0xFFFFFFFE00000FFF) != 0) { + LOG_ERROR(Kernel_SVC, "Invalid size"); return ERR_INVALID_SIZE; } const auto local_perms = static_cast(local_permissions); if (local_perms != MemoryPermission::Read && local_perms != MemoryPermission::ReadWrite) { + LOG_ERROR(Kernel_SVC, "Invalid memory permissions"); return ERR_INVALID_MEMORY_PERMISSIONS; } const auto remote_perms = static_cast(remote_permissions); if (remote_perms != MemoryPermission::Read && remote_perms != MemoryPermission::ReadWrite && remote_perms != MemoryPermission::DontCare) { + LOG_ERROR(Kernel_SVC, "Invalid memory permissions"); return ERR_INVALID_MEMORY_PERMISSIONS; } @@ -1316,6 +1392,7 @@ static ResultCode ClearEvent(Handle handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); SharedPtr evt = handle_table.Get(handle); if (evt == nullptr) { + LOG_ERROR(Kernel_SVC, "Invalid event handle"); return ERR_INVALID_HANDLE; } @@ -1334,11 +1411,13 @@ static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const auto process = handle_table.Get(process_handle); if (!process) { + LOG_ERROR(Kernel_SVC, "Invalid process handle"); return ERR_INVALID_HANDLE; } const auto info_type = static_cast(type); if (info_type != InfoType::Status) { + LOG_ERROR(Kernel_SVC, "Info type is not status"); return ERR_INVALID_ENUM_VALUE; } -- cgit v1.2.3 From 9662ca918de8fec60a13b26bcfca1a949e550b31 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Mon, 26 Nov 2018 19:47:39 +1100 Subject: Improved error messages for SVCs --- src/core/hle/kernel/svc.cpp | 246 ++++++++++++++++++++++++++++++-------------- 1 file changed, 170 insertions(+), 76 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 775afccf6..7f5640adb 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -68,46 +68,68 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { // in the same order. ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr, u64 size) { - if (!Common::Is4KBAligned(dst_addr) || !Common::Is4KBAligned(src_addr)) { - LOG_ERROR(Kernel_SVC, "Invalid address"); + if (!Common::Is4KBAligned(dst_addr)) { + LOG_ERROR(Kernel_SVC, "Destination address is not aligned to 4KB, 0x{:016X}", dst_addr); return ERR_INVALID_ADDRESS; } - if (size == 0 || !Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Invalid size"); + if (!Common::Is4KBAligned(src_addr)) { + LOG_ERROR(Kernel_SVC, "Source address is not aligned to 4KB, 0x{:016X}", src_addr); + } + + if (size == 0) { + LOG_ERROR(Kernel_SVC, "Size is 0"); + return ERR_INVALID_SIZE; + } + + if (!Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:016X}", size); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(dst_addr, size)) { - LOG_ERROR(Kernel_SVC, "size is out of range"); + LOG_ERROR(Kernel_SVC, + "Destination is not a valid address range, addr=0x{:016X}, size=0x{:016X}", + dst_addr, size); return ERR_INVALID_ADDRESS_STATE; } if (!IsValidAddressRange(src_addr, size)) { - LOG_ERROR(Kernel_SVC, "size is out of range"); + LOG_ERROR(Kernel_SVC, "Source is not a valid address range, addr=0x{:016X}, size=0x{:016X}", + src_addr, size); return ERR_INVALID_ADDRESS_STATE; } if (!IsInsideAddressSpace(vm_manager, src_addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is out of the address space"); + LOG_ERROR(Kernel_SVC, + "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", + src_addr, size); return ERR_INVALID_ADDRESS_STATE; } if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is out of the address space"); + LOG_ERROR(Kernel_SVC, + "Destination is not within the new map region, addr=0x{:016X}, size=0x{:016X}", + dst_addr, size); return ERR_INVALID_MEMORY_RANGE; } const VAddr dst_end_address = dst_addr + size; if (dst_end_address > vm_manager.GetHeapRegionBaseAddress() && vm_manager.GetHeapRegionEndAddress() > dst_addr) { - LOG_ERROR(Kernel_SVC, "Region is not in the correct address space"); + LOG_ERROR(Kernel_SVC, + "Destination does not fit within the heap region, addr=0x{:016X}, " + "size=0x{:016X}, end_addr=0x{:016X}", + dst_addr, size, dst_end_address); return ERR_INVALID_MEMORY_RANGE; } if (dst_end_address > vm_manager.GetMapRegionBaseAddress() && vm_manager.GetMapRegionEndAddress() > dst_addr) { - LOG_ERROR(Kernel_SVC, "Region is not in the correct address space"); + LOG_ERROR(Kernel_SVC, + "Destination does not fit within the map region, addr=0x{:016X}, " + "size=0x{:016X}, end_addr=0x{:016X}", + dst_addr, size, dst_end_address); return ERR_INVALID_MEMORY_RANGE; } @@ -121,7 +143,10 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB. if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) { - LOG_ERROR(Kernel_SVC, "Invalid heap size"); + LOG_ERROR( + Kernel_SVC, + "The heap size is not a multiple of 2mb or is greater than 4GB, heap_size=0x{:016X}", + heap_size); return ERR_INVALID_SIZE; } @@ -136,24 +161,31 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { LOG_TRACE(Kernel_SVC, "called, addr=0x{:X}, size=0x{:X}, prot=0x{:X}", addr, size, prot); if (!Common::Is4KBAligned(addr)) { - LOG_ERROR(Kernel_SVC, "Address is not aligned"); + LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr); return ERR_INVALID_ADDRESS; } - if (size == 0 || !Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Invalid size"); + if (size == 0) { + LOG_ERROR(Kernel_SVC, "Size is 0"); + return ERR_INVALID_SIZE; + } + + if (!Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is out of the address space"); + LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}", + addr, size); return ERR_INVALID_ADDRESS_STATE; } const auto permission = static_cast(prot); if (permission != MemoryPermission::None && permission != MemoryPermission::Read && permission != MemoryPermission::ReadWrite) { - LOG_ERROR(Kernel_SVC, "Incorrect memory permissions"); + LOG_ERROR(Kernel_SVC, "Invalid memory permission specified, Got memory permission=0x{:08X}", + static_cast(permission)); return ERR_INVALID_MEMORY_PERMISSIONS; } @@ -161,13 +193,15 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { auto& vm_manager = current_process->VMManager(); if (!IsInsideAddressSpace(vm_manager, addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not inside the address space"); + LOG_ERROR(Kernel_SVC, + "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, + size); return ERR_INVALID_ADDRESS_STATE; } const VMManager::VMAHandle iter = vm_manager.FindVMA(addr); if (iter == vm_manager.vma_map.end()) { - LOG_ERROR(Kernel_SVC, "Unable to find VMA"); + LOG_ERROR(Kernel_SVC, "Unable to find VMA for address=0x{:016X}", addr); return ERR_INVALID_ADDRESS_STATE; } @@ -197,7 +231,6 @@ static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result != RESULT_SUCCESS) { - LOG_ERROR(Kernel_SVC, "Map Memory failed"); return result; } @@ -214,7 +247,6 @@ static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result != RESULT_SUCCESS) { - LOG_ERROR(Kernel_SVC, "UnmapMemory failed"); return result; } @@ -224,7 +256,9 @@ static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { /// Connect to an OS service given the port name, returns the handle to the port to out static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) { if (!Memory::IsValidVirtualAddress(port_name_address)) { - LOG_ERROR(Kernel_SVC, "Invalid port name address"); + LOG_ERROR(Kernel_SVC, + "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}", + port_name_address); return ERR_NOT_FOUND; } @@ -232,7 +266,8 @@ static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address // Read 1 char beyond the max allowed port name to detect names that are too long. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1); if (port_name.size() > PortNameMaxLength) { - LOG_ERROR(Kernel_SVC, "Port name is too long"); + LOG_ERROR(Kernel_SVC, "Port name is too long, expected {} but got {}", PortNameMaxLength, + port_name.size()); return ERR_OUT_OF_RANGE; } @@ -281,7 +316,7 @@ static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", thread_handle); return ERR_INVALID_HANDLE; } @@ -296,7 +331,8 @@ static ResultCode GetProcessId(u32* process_id, Handle process_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr process = handle_table.Get(process_handle); if (!process) { - LOG_ERROR(Kernel_SVC, "Invalid process"); + LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", + process_handle); return ERR_INVALID_HANDLE; } @@ -327,14 +363,17 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handles_address, handle_count, nano_seconds); if (!Memory::IsValidVirtualAddress(handles_address)) { - LOG_ERROR(Kernel_SVC, "Invalid handle address"); + LOG_ERROR(Kernel_SVC, + "Handle address is not a valid virtual address, handle_address=0x{:016X}", + handles_address); return ERR_INVALID_POINTER; } static constexpr u64 MaxHandles = 0x40; if (handle_count > MaxHandles) { - LOG_ERROR(Kernel_SVC, "Handle count is too big"); + LOG_ERROR(Kernel_SVC, "Handle count specified is too large, expected {} but got {}", + MaxHandles, handle_count); return ERR_OUT_OF_RANGE; } @@ -400,7 +439,8 @@ static ResultCode CancelSynchronization(Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", + thread_handle); return ERR_INVALID_HANDLE; } @@ -419,12 +459,13 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr, holding_thread_handle, mutex_addr, requesting_thread_handle); if (Memory::IsKernelVirtualAddress(mutex_addr)) { - LOG_ERROR(Kernel_SVC, "Invalid mutex address"); + LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}", + mutex_addr); return ERR_INVALID_ADDRESS_STATE; } if (!Common::IsWordAligned(mutex_addr)) { - LOG_ERROR(Kernel_SVC, "Mutex address is not aligned"); + LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr); return ERR_INVALID_ADDRESS; } @@ -438,12 +479,13 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) { LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr); if (Memory::IsKernelVirtualAddress(mutex_addr)) { - LOG_ERROR(Kernel_SVC, "Invalid size"); + LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}", + mutex_addr); return ERR_INVALID_ADDRESS_STATE; } if (!Common::IsWordAligned(mutex_addr)) { - LOG_ERROR(Kernel_SVC, "Mutex address is not aligned"); + LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr); return ERR_INVALID_ADDRESS; } @@ -634,12 +676,14 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) break; case GetInfoType::RandomEntropy: if (handle != 0) { - LOG_ERROR(Kernel_SVC, "Non zero handle specified"); + LOG_ERROR(Kernel_SVC, "Process Handle is non zero, expected 0 result but got {:016X}", + handle); return ERR_INVALID_HANDLE; } if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) { - LOG_ERROR(Kernel_SVC, "Entropy size is too big"); + LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}", + Process::RANDOM_ENTROPY_SIZE, info_sub_id); return ERR_INVALID_COMBINATION; } @@ -677,14 +721,16 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) case GetInfoType::ThreadTickCount: { constexpr u64 num_cpus = 4; if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) { - LOG_ERROR(Kernel_SVC, "Incorrect info_sub_id"); + LOG_ERROR(Kernel_SVC, "Core count is out of range, expected {} but got {}", num_cpus, + info_sub_id); return ERR_INVALID_COMBINATION; } const auto thread = current_process->GetHandleTable().Get(static_cast(handle)); if (!thread) { - LOG_ERROR(Kernel_SVC, "Thread is not a valid handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", + static_cast(handle)); return ERR_INVALID_HANDLE; } @@ -727,17 +773,22 @@ static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { const auto* current_process = Core::CurrentProcess(); const SharedPtr thread = current_process->GetHandleTable().Get(handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Thread is not a valid handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); return ERR_INVALID_HANDLE; } if (thread->GetOwnerProcess() != current_process) { - LOG_ERROR(Kernel_SVC, "Thread owner process is not the current process"); + LOG_ERROR(Kernel_SVC, + "The current process does not own the current thread, thread_handle={:08X} " + "thread_pid={}, " + "current_process_pid={}", + handle, thread->GetOwnerProcess()->GetProcessID(), + current_process->GetProcessID()); return ERR_INVALID_HANDLE; } if (thread == GetCurrentThread()) { - LOG_ERROR(Kernel_SVC, "Thread is already registered"); + LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread"); return ERR_ALREADY_REGISTERED; } @@ -763,7 +814,7 @@ static ResultCode GetThreadPriority(u32* priority, Handle handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); return ERR_INVALID_HANDLE; } @@ -776,7 +827,10 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { LOG_TRACE(Kernel_SVC, "called"); if (priority > THREADPRIO_LOWEST) { - LOG_ERROR(Kernel_SVC, "Priority is out of range"); + LOG_ERROR( + Kernel_SVC, + "An invalid priority was specified, expected {} but got {} for thread_handle={:08X}", + THREADPRIO_LOWEST, priority, handle); return ERR_INVALID_THREAD_PRIORITY; } @@ -784,7 +838,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { SharedPtr thread = current_process->GetHandleTable().Get(handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); return ERR_INVALID_HANDLE; } @@ -807,37 +861,46 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s shared_memory_handle, addr, size, permissions); if (!Common::Is4KBAligned(addr)) { - LOG_ERROR(Kernel_SVC, "Address is not aligned"); + LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr); return ERR_INVALID_ADDRESS; } - if (size == 0 || !Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Invalid size"); + if (size == 0) { + LOG_ERROR(Kernel_SVC, "Size is 0"); + return ERR_INVALID_SIZE; + } + + if (!Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not in the address space"); + LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}", + addr, size); return ERR_INVALID_ADDRESS_STATE; } const auto permissions_type = static_cast(permissions); if (permissions_type != MemoryPermission::Read && permissions_type != MemoryPermission::ReadWrite) { - LOG_ERROR(Kernel_SVC, "Invalid permissions=0x{:08X}", permissions); + LOG_ERROR(Kernel_SVC, "Expected Read or ReadWrite permission but got permissions=0x{:08X}", + permissions); return ERR_INVALID_MEMORY_PERMISSIONS; } auto* const current_process = Core::CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get(shared_memory_handle); if (!shared_memory) { - LOG_ERROR(Kernel_SVC, "Invalid shared memory handle"); + LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}", + shared_memory_handle); return ERR_INVALID_HANDLE; } const auto& vm_manager = current_process->VMManager(); if (!vm_manager.IsWithinASLRRegion(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region"); + LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region. addr=0x{:016X}, size={:016X}", + addr, size); return ERR_INVALID_MEMORY_RANGE; } @@ -849,30 +912,38 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 shared_memory_handle, addr, size); if (!Common::Is4KBAligned(addr)) { - LOG_ERROR(Kernel_SVC, "Address is not aligned"); + LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr); return ERR_INVALID_ADDRESS; } - if (size == 0 || !Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Size is invalid"); + if (size == 0) { + LOG_ERROR(Kernel_SVC, "Size is 0"); + return ERR_INVALID_SIZE; + } + + if (!Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size); return ERR_INVALID_SIZE; } if (!IsValidAddressRange(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not in the valid address range"); + LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}", + addr, size); return ERR_INVALID_ADDRESS_STATE; } auto* const current_process = Core::CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get(shared_memory_handle); if (!shared_memory) { - LOG_ERROR(Kernel_SVC, "Shared memory is an invalid handle"); + LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}", + shared_memory_handle); return ERR_INVALID_HANDLE; } const auto& vm_manager = current_process->VMManager(); if (!vm_manager.IsWithinASLRRegion(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region"); + LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region. addr=0x{:016X}, size={:016X}", + addr, size); return ERR_INVALID_MEMORY_RANGE; } @@ -886,7 +957,8 @@ static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_i const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); SharedPtr process = handle_table.Get(process_handle); if (!process) { - LOG_ERROR(Kernel_SVC, "Invalid process handle"); + LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", + process_handle); return ERR_INVALID_HANDLE; } auto vma = process->VMManager().FindVMA(addr); @@ -936,7 +1008,8 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V entry_point, name, arg, stack_top, priority, processor_id, *out_handle); if (priority > THREADPRIO_LOWEST) { - LOG_ERROR(Kernel_SVC, "Invalid thread priority"); + LOG_ERROR(Kernel_SVC, "An invalid priority was specified, expected {} but got {}", + THREADPRIO_LOWEST, priority); return ERR_INVALID_THREAD_PRIORITY; } @@ -986,7 +1059,8 @@ static ResultCode StartThread(Handle thread_handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", + thread_handle); return ERR_INVALID_HANDLE; } @@ -1166,12 +1240,12 @@ static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout address, type, value, timeout); // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { - LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address"); + LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address); return ERR_INVALID_ADDRESS_STATE; } // If the address is not properly aligned to 4 bytes, return invalid address. - if (address % sizeof(u32) != 0) { - LOG_ERROR(Kernel_SVC, "Address is not aligned"); + if (!Common::IsWordAligned(address)) { + LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address); return ERR_INVALID_ADDRESS; } @@ -1183,7 +1257,10 @@ static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout case AddressArbiter::ArbitrationType::WaitIfEqual: return AddressArbiter::WaitForAddressIfEqual(address, value, timeout); default: - LOG_ERROR(Kernel_SVC, "Invalid arbitration type"); + LOG_ERROR(Kernel_SVC, + "Invalid arbitration type, expected WaitIfLessThan, DecrementAndWaitIfLessThan " + "or WaitIfEqual but got {}", + type); return ERR_INVALID_ENUM_VALUE; } } @@ -1194,12 +1271,12 @@ static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to address, type, value, num_to_wake); // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { - LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address"); + LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address); return ERR_INVALID_ADDRESS_STATE; } // If the address is not properly aligned to 4 bytes, return invalid address. - if (address % sizeof(u32) != 0) { - LOG_ERROR(Kernel_SVC, "Address is not aligned"); + if (!Common::IsWordAligned(address)) { + LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address); return ERR_INVALID_ADDRESS; } @@ -1212,7 +1289,10 @@ static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to return AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(address, value, num_to_wake); default: - LOG_ERROR(Kernel_SVC, "Invalid arbitration type"); + LOG_ERROR(Kernel_SVC, + "Invalid signal type, expected Signal, IncrementAndSignalIfEqual " + "or ModifyByWaitingCountAndSignalIfEqual but got {}", + type); return ERR_INVALID_ENUM_VALUE; } } @@ -1294,7 +1374,8 @@ static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", + thread_handle); return ERR_INVALID_HANDLE; } @@ -1311,7 +1392,8 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const SharedPtr thread = handle_table.Get(thread_handle); if (!thread) { - LOG_ERROR(Kernel_SVC, "Thread is an invalid handle"); + LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", + thread_handle); return ERR_INVALID_HANDLE; } @@ -1336,13 +1418,14 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { if (core == OnlyChangeMask) { core = thread->GetIdealCore(); } else if (core >= Core::NUM_CPU_CORES && core != static_cast(-1)) { - LOG_ERROR(Kernel_SVC, "Invalid processor ID specified"); + LOG_ERROR(Kernel_SVC, "Invalid core specified, got {}", core); return ERR_INVALID_PROCESSOR_ID; } // Error out if the input core isn't enabled in the input mask. if (core < Core::NUM_CPU_CORES && (mask & (1ull << core)) == 0) { - LOG_ERROR(Kernel_SVC, "Invalid core and mask"); + LOG_ERROR(Kernel_SVC, "Core is not enabled for the current mask, core={}, mask={:016X}", + core, mask); return ERR_INVALID_COMBINATION; } @@ -1358,21 +1441,31 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss // Size must be a multiple of 4KB and be less than or equal to // approx. 8 GB (actually (1GB - 512B) * 8) - if (size == 0 || (size & 0xFFFFFFFE00000FFF) != 0) { - LOG_ERROR(Kernel_SVC, "Invalid size"); + if (size == 0) { + LOG_ERROR(Kernel_SVC, "Size is 0"); + } + if ((size & 0xFFFFFFFE00000FFF) != 0) { + LOG_ERROR(Kernel_SVC, "Size is not a multiple of 4KB or is greater than 8GB, size={:016X}", + size); return ERR_INVALID_SIZE; } const auto local_perms = static_cast(local_permissions); if (local_perms != MemoryPermission::Read && local_perms != MemoryPermission::ReadWrite) { - LOG_ERROR(Kernel_SVC, "Invalid memory permissions"); + LOG_ERROR(Kernel_SVC, + "Invalid local memory permissions, expected Read or ReadWrite but got " + "local_permissions={}", + static_cast(local_permissions)); return ERR_INVALID_MEMORY_PERMISSIONS; } const auto remote_perms = static_cast(remote_permissions); if (remote_perms != MemoryPermission::Read && remote_perms != MemoryPermission::ReadWrite && remote_perms != MemoryPermission::DontCare) { - LOG_ERROR(Kernel_SVC, "Invalid memory permissions"); + LOG_ERROR(Kernel_SVC, + "Invalid remote memory permissions, expected Read, ReadWrite or DontCare but got " + "remote_permissions={}", + static_cast(remote_permissions)); return ERR_INVALID_MEMORY_PERMISSIONS; } @@ -1392,7 +1485,7 @@ static ResultCode ClearEvent(Handle handle) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); SharedPtr evt = handle_table.Get(handle); if (evt == nullptr) { - LOG_ERROR(Kernel_SVC, "Invalid event handle"); + LOG_ERROR(Kernel_SVC, "Event handle does not exist, handle=0x{:08X}", handle); return ERR_INVALID_HANDLE; } @@ -1411,13 +1504,14 @@ static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) { const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); const auto process = handle_table.Get(process_handle); if (!process) { - LOG_ERROR(Kernel_SVC, "Invalid process handle"); + LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", + process_handle); return ERR_INVALID_HANDLE; } const auto info_type = static_cast(type); if (info_type != InfoType::Status) { - LOG_ERROR(Kernel_SVC, "Info type is not status"); + LOG_ERROR(Kernel_SVC, "Expected info_type to be Status but got {} instead", type); return ERR_INVALID_ENUM_VALUE; } -- cgit v1.2.3 From f271316822c8887b1b6dfeff0803077f5ec2c0a8 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Tue, 27 Nov 2018 12:29:06 +1100 Subject: Reworked svcs slightly, improved error messages in AM and fsp_srv --- src/core/hle/kernel/svc.cpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 7f5640adb..16e9704ff 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -63,6 +63,8 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { vm.GetNewMapRegionEndAddress()); } +const u64 SZ_8GB = 0x200000000; + // Helper function that performs the common sanity checks for svcMapMemory // and svcUnmapMemory. This is doable, as both functions perform their sanitizing // in the same order. @@ -75,6 +77,7 @@ ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_add if (!Common::Is4KBAligned(src_addr)) { LOG_ERROR(Kernel_SVC, "Source address is not aligned to 4KB, 0x{:016X}", src_addr); + return ERR_INVALID_SIZE; } if (size == 0) { @@ -141,12 +144,15 @@ ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_add static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size); - // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB. - if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) { - LOG_ERROR( - Kernel_SVC, - "The heap size is not a multiple of 2mb or is greater than 4GB, heap_size=0x{:016X}", - heap_size); + // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 8GB. + if ((heap_size & 0x1FFFFF) != 0) { + LOG_ERROR(Kernel_SVC, "The heap size is not a multiple of 2MB, heap_size=0x{:016X}", + heap_size); + return ERR_INVALID_SIZE; + } + + if (heap_size >= SZ_8GB) { + LOG_ERROR(Kernel_SVC, "The heap size is not less than 8GB, heap_size=0x{:016X}", heap_size); return ERR_INVALID_SIZE; } @@ -1438,15 +1444,17 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss u32 remote_permissions) { LOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size, local_permissions, remote_permissions); - - // Size must be a multiple of 4KB and be less than or equal to - // approx. 8 GB (actually (1GB - 512B) * 8) if (size == 0) { LOG_ERROR(Kernel_SVC, "Size is 0"); + return ERR_INVALID_SIZE; } - if ((size & 0xFFFFFFFE00000FFF) != 0) { - LOG_ERROR(Kernel_SVC, "Size is not a multiple of 4KB or is greater than 8GB, size={:016X}", - size); + if (!Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:016X}", size); + return ERR_INVALID_SIZE; + } + + if (size >= SZ_8GB) { + LOG_ERROR(Kernel_SVC, "Size is not less than 8GB, 0x{:016X}", size); return ERR_INVALID_SIZE; } -- cgit v1.2.3 From f058de337e523fb9916155bc4c739660cc521923 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Tue, 27 Nov 2018 12:53:18 +1100 Subject: Made svcSetHeapSize and svcCreateSharedMemory more readable --- src/core/hle/kernel/svc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 16e9704ff..35eee8127 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -63,7 +63,7 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { vm.GetNewMapRegionEndAddress()); } -const u64 SZ_8GB = 0x200000000; +constexpr u64 MAIN_MEMORY_SIZE = 0x200000000; // Helper function that performs the common sanity checks for svcMapMemory // and svcUnmapMemory. This is doable, as both functions perform their sanitizing @@ -145,13 +145,13 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size); // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 8GB. - if ((heap_size & 0x1FFFFF) != 0) { + if ((heap_size % 0x200000) != 0) { LOG_ERROR(Kernel_SVC, "The heap size is not a multiple of 2MB, heap_size=0x{:016X}", heap_size); return ERR_INVALID_SIZE; } - if (heap_size >= SZ_8GB) { + if (heap_size >= 0x200000000) { LOG_ERROR(Kernel_SVC, "The heap size is not less than 8GB, heap_size=0x{:016X}", heap_size); return ERR_INVALID_SIZE; } @@ -1453,7 +1453,7 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss return ERR_INVALID_SIZE; } - if (size >= SZ_8GB) { + if (size >= MAIN_MEMORY_SIZE) { LOG_ERROR(Kernel_SVC, "Size is not less than 8GB, 0x{:016X}", size); return ERR_INVALID_SIZE; } -- cgit v1.2.3 From cc4521fc703e39e216efd57481cb4dbc76778264 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Tue, 27 Nov 2018 12:56:50 +1100 Subject: Added comment on Main memory size for more clarity --- src/core/hle/kernel/svc.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 35eee8127..b5c644ede 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -63,6 +63,7 @@ bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) { vm.GetNewMapRegionEndAddress()); } +// 8 GiB constexpr u64 MAIN_MEMORY_SIZE = 0x200000000; // Helper function that performs the common sanity checks for svcMapMemory -- cgit v1.2.3