2014-04-11 03:58:28 +04:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 08:38:14 +03:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 11:49:13 +03:00
|
|
|
// Refer to the license.txt file included.
|
2014-04-11 03:58:28 +04:00
|
|
|
|
2014-04-12 02:44:21 +04:00
|
|
|
#include <map>
|
2015-05-06 10:06:12 +03:00
|
|
|
#include "common/logging/log.h"
|
2015-08-18 00:25:21 +03:00
|
|
|
#include "common/microprofile.h"
|
2016-05-21 17:45:18 +03:00
|
|
|
#include "common/scope_exit.h"
|
2014-08-17 22:28:36 +04:00
|
|
|
#include "common/string_util.h"
|
2014-05-19 01:52:02 +04:00
|
|
|
#include "common/symbols.h"
|
2015-03-16 19:16:59 +03:00
|
|
|
#include "core/arm/arm_interface.h"
|
2016-09-18 03:38:01 +03:00
|
|
|
#include "core/core_timing.h"
|
2016-09-20 18:21:23 +03:00
|
|
|
#include "core/hle/function_wrappers.h"
|
2014-07-07 07:15:40 +04:00
|
|
|
#include "core/hle/kernel/address_arbiter.h"
|
2016-05-22 20:30:13 +03:00
|
|
|
#include "core/hle/kernel/client_port.h"
|
2014-05-28 06:41:09 +04:00
|
|
|
#include "core/hle/kernel/event.h"
|
2015-07-29 18:08:00 +03:00
|
|
|
#include "core/hle/kernel/memory.h"
|
2014-05-21 06:57:54 +04:00
|
|
|
#include "core/hle/kernel/mutex.h"
|
2015-05-11 17:15:10 +03:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 23:25:15 +03:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2014-12-04 02:49:51 +03:00
|
|
|
#include "core/hle/kernel/semaphore.h"
|
2016-05-22 20:30:13 +03:00
|
|
|
#include "core/hle/kernel/server_port.h"
|
2014-07-05 08:59:58 +04:00
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
2014-05-17 21:47:44 +04:00
|
|
|
#include "core/hle/kernel/thread.h"
|
2014-12-04 22:45:47 +03:00
|
|
|
#include "core/hle/kernel/timer.h"
|
2015-07-17 09:23:22 +03:00
|
|
|
#include "core/hle/kernel/vm_manager.h"
|
2014-10-23 07:20:01 +04:00
|
|
|
#include "core/hle/result.h"
|
2014-04-13 05:55:36 +04:00
|
|
|
#include "core/hle/service/service.h"
|
2014-04-11 03:58:28 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-05-21 02:28:38 +04:00
|
|
|
// Namespace SVC
|
2014-04-11 03:58:28 +04:00
|
|
|
|
2014-12-29 15:55:30 +03:00
|
|
|
using Kernel::SharedPtr;
|
2015-01-23 08:44:52 +03:00
|
|
|
using Kernel::ERR_INVALID_HANDLE;
|
2014-12-29 15:55:30 +03:00
|
|
|
|
2014-05-21 02:28:38 +04:00
|
|
|
namespace SVC {
|
2014-04-11 07:26:12 +04:00
|
|
|
|
2015-01-30 21:07:04 +03:00
|
|
|
const ResultCode ERR_NOT_FOUND(ErrorDescription::NotFound, ErrorModule::Kernel,
|
2016-09-18 03:38:01 +03:00
|
|
|
ErrorSummary::NotFound, ErrorLevel::Permanent); // 0xD88007FA
|
2015-01-30 21:07:04 +03:00
|
|
|
const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS,
|
2016-09-18 03:38:01 +03:00
|
|
|
ErrorSummary::InvalidArgument,
|
|
|
|
ErrorLevel::Usage); // 0xE0E0181E
|
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
const ResultCode ERR_SYNC_TIMEOUT(ErrorDescription::Timeout, ErrorModule::OS,
|
|
|
|
ErrorSummary::StatusChanged, ErrorLevel::Info);
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
const ResultCode ERR_MISALIGNED_ADDRESS{// 0xE0E01BF1
|
|
|
|
ErrorDescription::MisalignedAddress, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage};
|
|
|
|
const ResultCode ERR_MISALIGNED_SIZE{// 0xE0E01BF2
|
|
|
|
ErrorDescription::MisalignedSize, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage};
|
|
|
|
const ResultCode ERR_INVALID_COMBINATION{// 0xE0E01BEE
|
|
|
|
ErrorDescription::InvalidCombination, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage};
|
2015-07-18 05:19:16 +03:00
|
|
|
|
2014-04-25 07:56:06 +04:00
|
|
|
enum ControlMemoryOperation {
|
2016-09-18 03:38:01 +03:00
|
|
|
MEMOP_FREE = 1,
|
2015-07-18 05:19:16 +03:00
|
|
|
MEMOP_RESERVE = 2, // This operation seems to be unsupported in the kernel
|
2016-09-18 03:38:01 +03:00
|
|
|
MEMOP_COMMIT = 3,
|
|
|
|
MEMOP_MAP = 4,
|
|
|
|
MEMOP_UNMAP = 5,
|
2015-07-18 05:19:16 +03:00
|
|
|
MEMOP_PROTECT = 6,
|
|
|
|
MEMOP_OPERATION_MASK = 0xFF,
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
MEMOP_REGION_APP = 0x100,
|
2015-07-18 05:19:16 +03:00
|
|
|
MEMOP_REGION_SYSTEM = 0x200,
|
2016-09-18 03:38:01 +03:00
|
|
|
MEMOP_REGION_BASE = 0x300,
|
|
|
|
MEMOP_REGION_MASK = 0xF00,
|
2015-07-18 05:19:16 +03:00
|
|
|
|
|
|
|
MEMOP_LINEAR = 0x10000,
|
2014-04-25 07:56:06 +04:00
|
|
|
};
|
|
|
|
|
2014-04-18 07:05:31 +04:00
|
|
|
/// Map application or GSP heap memory
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size,
|
|
|
|
u32 permissions) {
|
2015-07-18 05:19:16 +03:00
|
|
|
using namespace Kernel;
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
LOG_DEBUG(Kernel_SVC,
|
|
|
|
"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=0x%X, permissions=0x%08X",
|
|
|
|
operation, addr0, addr1, size, permissions);
|
2014-05-07 07:34:20 +04:00
|
|
|
|
2015-07-18 05:19:16 +03:00
|
|
|
if ((addr0 & Memory::PAGE_MASK) != 0 || (addr1 & Memory::PAGE_MASK) != 0) {
|
|
|
|
return ERR_MISALIGNED_ADDRESS;
|
|
|
|
}
|
|
|
|
if ((size & Memory::PAGE_MASK) != 0) {
|
|
|
|
return ERR_MISALIGNED_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 region = operation & MEMOP_REGION_MASK;
|
|
|
|
operation &= ~MEMOP_REGION_MASK;
|
|
|
|
|
|
|
|
if (region != 0) {
|
2016-09-18 03:38:01 +03:00
|
|
|
LOG_WARNING(Kernel_SVC, "ControlMemory with specified region not supported, region=%X",
|
|
|
|
region);
|
2015-07-18 05:19:16 +03:00
|
|
|
}
|
2014-04-18 07:05:31 +04:00
|
|
|
|
2015-07-18 05:19:16 +03:00
|
|
|
if ((permissions & (u32)MemoryPermission::ReadWrite) != permissions) {
|
|
|
|
return ERR_INVALID_COMBINATION;
|
|
|
|
}
|
|
|
|
VMAPermission vma_permissions = (VMAPermission)permissions;
|
|
|
|
|
|
|
|
auto& process = *g_current_process;
|
|
|
|
|
|
|
|
switch (operation & MEMOP_OPERATION_MASK) {
|
2016-09-18 03:38:01 +03:00
|
|
|
case MEMOP_FREE: {
|
|
|
|
// TODO(Subv): What happens if an application tries to FREE a block of memory that has a
|
|
|
|
// SharedMemory pointing to it?
|
2015-07-18 05:19:16 +03:00
|
|
|
if (addr0 >= Memory::HEAP_VADDR && addr0 < Memory::HEAP_VADDR_END) {
|
|
|
|
ResultCode result = process.HeapFree(addr0, size);
|
2016-09-18 03:38:01 +03:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-08-06 03:26:52 +03:00
|
|
|
} else if (addr0 >= process.GetLinearHeapBase() && addr0 < process.GetLinearHeapLimit()) {
|
2015-07-18 05:19:16 +03:00
|
|
|
ResultCode result = process.LinearFree(addr0, size);
|
2016-09-18 03:38:01 +03:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-07-18 05:19:16 +03:00
|
|
|
} else {
|
|
|
|
return ERR_INVALID_ADDRESS;
|
|
|
|
}
|
|
|
|
*out_addr = addr0;
|
2014-04-25 07:56:06 +04:00
|
|
|
break;
|
2015-07-18 05:19:16 +03:00
|
|
|
}
|
2014-04-25 07:56:06 +04:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
case MEMOP_COMMIT: {
|
2015-07-18 05:19:16 +03:00
|
|
|
if (operation & MEMOP_LINEAR) {
|
|
|
|
CASCADE_RESULT(*out_addr, process.LinearAllocate(addr0, size, vma_permissions));
|
|
|
|
} else {
|
|
|
|
CASCADE_RESULT(*out_addr, process.HeapAllocate(addr0, size, vma_permissions));
|
|
|
|
}
|
2014-04-18 07:05:31 +04:00
|
|
|
break;
|
2015-07-18 05:19:16 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
case MEMOP_MAP: // TODO: This is just a hack to avoid regressions until memory aliasing is
|
|
|
|
// implemented
|
|
|
|
{
|
|
|
|
CASCADE_RESULT(*out_addr, process.HeapAllocate(addr0, size, vma_permissions));
|
|
|
|
break;
|
|
|
|
}
|
2015-07-18 05:19:16 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
case MEMOP_UNMAP: // TODO: This is just a hack to avoid regressions until memory aliasing is
|
|
|
|
// implemented
|
|
|
|
{
|
|
|
|
ResultCode result = process.HeapFree(addr0, size);
|
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-18 05:19:16 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
case MEMOP_PROTECT: {
|
2015-07-18 05:19:16 +03:00
|
|
|
ResultCode result = process.vm_manager.ReprotectRange(addr0, size, vma_permissions);
|
2016-09-18 03:38:01 +03:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-07-18 05:19:16 +03:00
|
|
|
break;
|
|
|
|
}
|
2014-04-18 07:05:31 +04:00
|
|
|
|
|
|
|
default:
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown operation=0x%08X", operation);
|
2015-07-18 05:19:16 +03:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2014-04-18 07:05:31 +04:00
|
|
|
}
|
2015-07-18 05:19:16 +03:00
|
|
|
|
|
|
|
process.vm_manager.LogLayout(Log::Level::Trace);
|
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-04-25 07:56:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps a memory block to specified address
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) {
|
2015-01-11 08:43:29 +03:00
|
|
|
using Kernel::SharedMemory;
|
|
|
|
using Kernel::MemoryPermission;
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
LOG_TRACE(Kernel_SVC,
|
|
|
|
"called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d",
|
|
|
|
handle, addr, permissions, other_permissions);
|
2014-07-05 18:22:03 +04:00
|
|
|
|
2015-01-11 08:43:29 +03:00
|
|
|
SharedPtr<SharedMemory> shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
|
|
|
|
if (shared_memory == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 08:43:29 +03:00
|
|
|
|
|
|
|
MemoryPermission permissions_type = static_cast<MemoryPermission>(permissions);
|
2014-07-05 18:22:03 +04:00
|
|
|
switch (permissions_type) {
|
2015-01-11 08:43:29 +03:00
|
|
|
case MemoryPermission::Read:
|
|
|
|
case MemoryPermission::Write:
|
|
|
|
case MemoryPermission::ReadWrite:
|
|
|
|
case MemoryPermission::Execute:
|
|
|
|
case MemoryPermission::ReadExecute:
|
|
|
|
case MemoryPermission::WriteExecute:
|
|
|
|
case MemoryPermission::ReadWriteExecute:
|
|
|
|
case MemoryPermission::DontCare:
|
2016-04-18 05:07:52 +03:00
|
|
|
return shared_memory->Map(Kernel::g_current_process.get(), addr, permissions_type,
|
2016-09-18 03:38:01 +03:00
|
|
|
static_cast<MemoryPermission>(other_permissions));
|
2014-04-25 07:56:06 +04:00
|
|
|
default:
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown permissions=0x%08X", permissions);
|
2014-04-25 07:56:06 +04:00
|
|
|
}
|
2015-12-31 17:46:32 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
2015-12-31 17:46:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static ResultCode UnmapMemoryBlock(Handle handle, u32 addr) {
|
|
|
|
using Kernel::SharedMemory;
|
|
|
|
|
|
|
|
LOG_TRACE(Kernel_SVC, "called memblock=0x%08X, addr=0x%08X", handle, addr);
|
|
|
|
|
|
|
|
// TODO(Subv): Return E0A01BF5 if the address is not in the application's heap
|
|
|
|
|
|
|
|
SharedPtr<SharedMemory> shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
|
|
|
|
if (shared_memory == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2016-04-18 05:07:52 +03:00
|
|
|
return shared_memory->Unmap(Kernel::g_current_process.get(), addr);
|
2014-04-18 07:05:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 05:55:36 +04:00
|
|
|
/// Connect to an OS service given the port name, returns the handle to the port to out
|
2015-01-30 21:07:04 +03:00
|
|
|
static ResultCode ConnectToPort(Handle* out_handle, const char* port_name) {
|
|
|
|
if (port_name == nullptr)
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
if (std::strlen(port_name) > 11)
|
|
|
|
return ERR_PORT_NAME_TOO_LONG;
|
2014-06-02 04:48:29 +04:00
|
|
|
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called port_name=%s", port_name);
|
2014-06-02 04:48:29 +04:00
|
|
|
|
2015-01-30 21:07:04 +03:00
|
|
|
auto it = Service::g_kernel_named_ports.find(port_name);
|
|
|
|
if (it == Service::g_kernel_named_ports.end()) {
|
|
|
|
LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: %s", port_name);
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
}
|
2014-06-02 04:48:29 +04:00
|
|
|
|
2015-01-30 21:07:04 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(it->second));
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-04-13 05:55:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Synchronize to an OS service
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode SendSyncRequest(Handle handle) {
|
2014-12-29 15:55:30 +03:00
|
|
|
SharedPtr<Kernel::Session> session = Kernel::g_handle_table.Get<Kernel::Session>(handle);
|
2014-12-14 08:30:11 +03:00
|
|
|
if (session == nullptr) {
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-10-23 07:20:01 +04:00
|
|
|
}
|
2014-05-27 06:12:46 +04:00
|
|
|
|
2014-12-14 08:30:11 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str());
|
2014-05-27 06:12:46 +04:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return session->SyncRequest().Code();
|
2014-04-11 03:58:28 +04:00
|
|
|
}
|
|
|
|
|
2014-04-17 04:41:33 +04:00
|
|
|
/// Close a handle
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode CloseHandle(Handle handle) {
|
2015-01-31 21:12:54 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "Closing handle 0x%08X", handle);
|
|
|
|
return Kernel::g_handle_table.Close(handle);
|
2014-04-17 04:41:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a handle to synchronize, timeout after the specified nanoseconds
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
|
2015-01-18 22:33:11 +03:00
|
|
|
auto object = Kernel::g_handle_table.GetWaitObject(handle);
|
2015-06-08 06:39:37 +03:00
|
|
|
Kernel::Thread* thread = Kernel::GetCurrentThread();
|
|
|
|
|
2014-12-21 15:04:08 +03:00
|
|
|
if (object == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-05-28 06:41:09 +04:00
|
|
|
|
2014-12-29 15:55:30 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
|
2016-09-18 03:38:01 +03:00
|
|
|
object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
|
2014-06-06 06:35:36 +04:00
|
|
|
|
2015-01-21 02:16:45 +03:00
|
|
|
if (object->ShouldWait()) {
|
2015-01-18 21:25:51 +03:00
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
if (nano_seconds == 0)
|
|
|
|
return ERR_SYNC_TIMEOUT;
|
2016-12-04 06:38:14 +03:00
|
|
|
|
2015-06-08 06:39:37 +03:00
|
|
|
object->AddWaitingThread(thread);
|
2016-12-04 17:58:36 +03:00
|
|
|
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
|
|
|
|
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
|
2016-12-04 06:38:14 +03:00
|
|
|
thread->status = THREADSTATUS_WAIT_SYNCH;
|
2015-01-18 21:25:51 +03:00
|
|
|
|
2015-01-10 19:34:50 +03:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2015-06-08 06:39:37 +03:00
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2015-01-17 10:03:44 +03:00
|
|
|
|
2016-12-04 17:58:36 +03:00
|
|
|
// Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in its wait objects.
|
2016-12-04 06:38:14 +03:00
|
|
|
// Otherwise we retain the default value of timeout.
|
2016-12-08 18:34:53 +03:00
|
|
|
return ERR_SYNC_TIMEOUT;
|
2014-05-28 06:41:09 +04:00
|
|
|
}
|
|
|
|
|
2015-01-22 02:27:01 +03:00
|
|
|
object->Acquire();
|
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-18 07:37:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all,
|
|
|
|
s64 nano_seconds) {
|
2015-06-08 06:39:37 +03:00
|
|
|
Kernel::Thread* thread = Kernel::GetCurrentThread();
|
2014-05-23 03:32:45 +04:00
|
|
|
|
2015-01-18 10:16:54 +03:00
|
|
|
// Check if 'handles' is invalid
|
2015-01-17 22:04:54 +03:00
|
|
|
if (handles == nullptr)
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::InvalidPointer, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
2015-01-17 22:04:54 +03:00
|
|
|
|
2015-01-18 10:16:54 +03:00
|
|
|
// NOTE: on real hardware, there is no nullptr check for 'out' (tested with firmware 4.4). If
|
|
|
|
// this happens, the running application will crash.
|
2015-01-21 04:16:47 +03:00
|
|
|
ASSERT_MSG(out != nullptr, "invalid output pointer specified!");
|
2015-01-18 10:16:54 +03:00
|
|
|
|
|
|
|
// Check if 'handle_count' is invalid
|
2015-01-17 20:35:55 +03:00
|
|
|
if (handle_count < 0)
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::OutOfRange, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
2015-01-17 20:35:55 +03:00
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
using ObjectPtr = Kernel::SharedPtr<Kernel::WaitObject>;
|
|
|
|
std::vector<ObjectPtr> objects(handle_count);
|
|
|
|
|
|
|
|
for (int i = 0; i < handle_count; ++i) {
|
|
|
|
auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
|
|
|
|
if (object == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
objects[i] = object;
|
2014-05-28 06:41:09 +04:00
|
|
|
}
|
|
|
|
|
2016-12-04 17:58:36 +03:00
|
|
|
// Clear the mapping of wait object indices.
|
|
|
|
// We don't want any lingering state in this map.
|
|
|
|
// It will be repopulated later in the wait_all = false case.
|
2016-12-04 06:38:14 +03:00
|
|
|
thread->wait_objects_index.clear();
|
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
if (wait_all) {
|
|
|
|
bool all_available = std::all_of(objects.begin(), objects.end(), [](const ObjectPtr& object) {
|
2016-12-04 06:38:14 +03:00
|
|
|
return !object->ShouldWait();
|
|
|
|
});
|
2016-12-08 18:34:53 +03:00
|
|
|
if (all_available) {
|
|
|
|
// We can acquire all objects right now, do so.
|
|
|
|
for (auto object : objects)
|
|
|
|
object->Acquire();
|
|
|
|
// Note: In this case, the `out` parameter is not set, and retains whatever value it had before.
|
2016-12-04 06:38:14 +03:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// Not all objects were available right now, prepare to suspend the thread.
|
2016-12-04 06:38:14 +03:00
|
|
|
|
|
|
|
// If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread.
|
2016-12-08 18:34:53 +03:00
|
|
|
if (nano_seconds == 0)
|
|
|
|
return ERR_SYNC_TIMEOUT;
|
2015-02-10 06:05:39 +03:00
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
// Put the thread to sleep
|
|
|
|
thread->status = THREADSTATUS_WAIT_SYNCH;
|
2015-01-18 21:25:51 +03:00
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
// Add the thread to each of the objects' waiting threads.
|
2016-12-08 18:34:53 +03:00
|
|
|
for (auto& object : objects) {
|
2016-12-04 06:38:14 +03:00
|
|
|
object->AddWaitingThread(thread);
|
|
|
|
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
|
2016-12-04 17:58:36 +03:00
|
|
|
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
|
2015-01-18 21:25:51 +03:00
|
|
|
}
|
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// Set the thread's waitlist to the list of objects passed to WaitSynchronizationN
|
|
|
|
thread->wait_objects = std::move(objects);
|
2015-01-26 09:56:17 +03:00
|
|
|
|
2015-01-17 10:03:44 +03:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2016-12-04 06:38:14 +03:00
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2015-01-17 10:03:44 +03:00
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// This value gets set to -1 by default in this case, it is not modified after this.
|
2016-12-04 06:38:14 +03:00
|
|
|
*out = -1;
|
2016-12-08 18:34:53 +03:00
|
|
|
// Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects.
|
|
|
|
return ERR_SYNC_TIMEOUT;
|
2016-12-04 06:38:14 +03:00
|
|
|
} else {
|
2016-12-08 18:34:53 +03:00
|
|
|
// Find the first object that is acquirable in the provided list of objects
|
|
|
|
auto itr = std::find_if(objects.begin(), objects.end(), [](const ObjectPtr& object) {
|
2016-12-04 06:38:14 +03:00
|
|
|
return !object->ShouldWait();
|
|
|
|
});
|
2016-12-08 18:34:53 +03:00
|
|
|
|
|
|
|
if (itr != objects.end()) {
|
|
|
|
// We found a ready object, acquire it and set the result value
|
|
|
|
Kernel::WaitObject* object = itr->get();
|
|
|
|
object->Acquire();
|
|
|
|
*out = std::distance(objects.begin(), itr);
|
2016-12-04 06:38:14 +03:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2014-05-28 06:41:09 +04:00
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// No objects were ready to be acquired, prepare to suspend the thread.
|
2015-01-17 10:03:44 +03:00
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
// If a timeout value of 0 was provided, just return the Timeout error code instead of suspending the thread.
|
2016-12-08 18:34:53 +03:00
|
|
|
if (nano_seconds == 0)
|
|
|
|
return ERR_SYNC_TIMEOUT;
|
2016-12-04 06:38:14 +03:00
|
|
|
|
|
|
|
// Put the thread to sleep
|
|
|
|
thread->status = THREADSTATUS_WAIT_SYNCH;
|
2015-01-17 10:03:44 +03:00
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// Clear the thread's waitlist, we won't use it for wait_all = false
|
|
|
|
thread->wait_objects.clear();
|
2016-12-04 06:38:14 +03:00
|
|
|
|
|
|
|
// Add the thread to each of the objects' waiting threads.
|
2016-12-08 18:34:53 +03:00
|
|
|
for (size_t i = 0; i < objects.size(); ++i) {
|
|
|
|
Kernel::WaitObject* object = objects[i].get();
|
|
|
|
// Set the index of this object in the mapping of Objects -> index for this thread.
|
|
|
|
thread->wait_objects_index[object->GetObjectId()] = static_cast<int>(i);
|
2016-12-04 06:38:14 +03:00
|
|
|
object->AddWaitingThread(thread);
|
|
|
|
// TODO(Subv): Perform things like update the mutex lock owner's priority to prevent priority inversion.
|
2016-12-04 17:58:36 +03:00
|
|
|
// Currently this is done in Mutex::ShouldWait, but it should be moved to a function that is called from here.
|
2015-01-17 10:03:44 +03:00
|
|
|
}
|
|
|
|
|
2016-12-08 18:34:53 +03:00
|
|
|
// Note: If no handles and no timeout were given, then the thread will deadlock, this is consistent with hardware behavior.
|
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2014-05-28 06:41:09 +04:00
|
|
|
|
2016-12-04 06:38:14 +03:00
|
|
|
// Note: The output of this SVC will be set to RESULT_SUCCESS if the thread resumes due to a signal in one of its wait objects.
|
2016-12-08 18:34:53 +03:00
|
|
|
// Otherwise we retain the default value of timeout, and -1 in the out parameter
|
|
|
|
thread->wait_set_output = true;
|
|
|
|
*out = -1;
|
|
|
|
return ERR_SYNC_TIMEOUT;
|
2016-12-04 06:38:14 +03:00
|
|
|
}
|
2014-04-17 04:41:33 +04:00
|
|
|
}
|
|
|
|
|
2014-05-02 02:50:36 +04:00
|
|
|
/// Create an address arbiter (to allocate access to shared resources)
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateAddressArbiter(Handle* out_handle) {
|
2015-01-11 19:46:49 +03:00
|
|
|
using Kernel::AddressArbiter;
|
|
|
|
|
2015-02-01 05:14:40 +03:00
|
|
|
SharedPtr<AddressArbiter> arbiter = AddressArbiter::Create();
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(arbiter)));
|
|
|
|
LOG_TRACE(Kernel_SVC, "returned handle=0x%08X", *out_handle);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-02 02:50:36 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:24:51 +04:00
|
|
|
/// Arbitrate address
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode ArbitrateAddress(Handle handle, u32 address, u32 type, u32 value,
|
|
|
|
s64 nanoseconds) {
|
2015-01-11 19:46:49 +03:00
|
|
|
using Kernel::AddressArbiter;
|
|
|
|
|
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x%08X, address=0x%08X, type=0x%08X, value=0x%08X", handle,
|
2016-09-18 03:38:01 +03:00
|
|
|
address, type, value);
|
2015-01-11 19:46:49 +03:00
|
|
|
|
|
|
|
SharedPtr<AddressArbiter> arbiter = Kernel::g_handle_table.Get<AddressArbiter>(handle);
|
|
|
|
if (arbiter == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 19:46:49 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
auto res = arbiter->ArbitrateAddress(static_cast<Kernel::ArbitrationType>(type), address, value,
|
|
|
|
nanoseconds);
|
2015-04-07 04:58:05 +03:00
|
|
|
|
|
|
|
return res;
|
2014-05-30 04:24:51 +04:00
|
|
|
}
|
|
|
|
|
2015-05-17 08:06:59 +03:00
|
|
|
static void Break(u8 break_reason) {
|
|
|
|
LOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
|
|
|
|
std::string reason_str;
|
|
|
|
switch (break_reason) {
|
2016-09-18 03:38:01 +03:00
|
|
|
case 0:
|
|
|
|
reason_str = "PANIC";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
reason_str = "ASSERT";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
reason_str = "USER";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reason_str = "UNKNOWN";
|
|
|
|
break;
|
2015-05-17 08:06:59 +03:00
|
|
|
}
|
|
|
|
LOG_CRITICAL(Debug_Emulated, "Break reason: %s", reason_str.c_str());
|
|
|
|
}
|
|
|
|
|
2014-05-02 02:50:36 +04:00
|
|
|
/// Used to output a message on a debug hardware unit - does nothing on a retail unit
|
2014-11-17 06:58:39 +03:00
|
|
|
static void OutputDebugString(const char* string) {
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_DEBUG(Debug_Emulated, "%s", string);
|
2014-05-07 07:34:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit
|
2015-05-12 23:25:15 +03:00
|
|
|
static ResultCode GetResourceLimit(Handle* resource_limit, Handle process_handle) {
|
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x%08X", process_handle);
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
SharedPtr<Kernel::Process> process =
|
|
|
|
Kernel::g_handle_table.Get<Kernel::Process>(process_handle);
|
2015-05-12 23:25:15 +03:00
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
CASCADE_RESULT(*resource_limit, Kernel::g_handle_table.Create(process->resource_limit));
|
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-07 07:34:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit current values
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode GetResourceLimitCurrentValues(s64* values, Handle resource_limit_handle,
|
|
|
|
u32* names, u32 name_count) {
|
2015-05-12 23:25:15 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called resource_limit=%08X, names=%p, name_count=%d",
|
2016-09-18 03:38:01 +03:00
|
|
|
resource_limit_handle, names, name_count);
|
2015-05-12 23:25:15 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
SharedPtr<Kernel::ResourceLimit> resource_limit =
|
|
|
|
Kernel::g_handle_table.Get<Kernel::ResourceLimit>(resource_limit_handle);
|
2015-05-12 23:25:15 +03:00
|
|
|
if (resource_limit == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < name_count; ++i)
|
|
|
|
values[i] = resource_limit->GetCurrentResourceValue(names[i]);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit max values
|
|
|
|
static ResultCode GetResourceLimitLimitValues(s64* values, Handle resource_limit_handle, u32* names,
|
2016-09-18 03:38:01 +03:00
|
|
|
u32 name_count) {
|
2015-05-12 23:25:15 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called resource_limit=%08X, names=%p, name_count=%d",
|
2016-09-18 03:38:01 +03:00
|
|
|
resource_limit_handle, names, name_count);
|
2015-05-12 23:25:15 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
SharedPtr<Kernel::ResourceLimit> resource_limit =
|
|
|
|
Kernel::g_handle_table.Get<Kernel::ResourceLimit>(resource_limit_handle);
|
2015-05-12 23:25:15 +03:00
|
|
|
if (resource_limit == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < name_count; ++i)
|
|
|
|
values[i] = resource_limit->GetMaxResourceValue(names[i]);
|
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-02 02:50:36 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 06:57:54 +04:00
|
|
|
/// Creates a new thread
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode CreateThread(Handle* out_handle, s32 priority, u32 entry_point, u32 arg,
|
|
|
|
u32 stack_top, s32 processor_id) {
|
2014-12-29 15:55:30 +03:00
|
|
|
using Kernel::Thread;
|
|
|
|
|
2014-05-17 21:47:44 +04:00
|
|
|
std::string name;
|
2014-05-16 02:25:56 +04:00
|
|
|
if (Symbols::HasSymbol(entry_point)) {
|
|
|
|
TSymbol symbol = Symbols::GetSymbol(entry_point);
|
2014-05-17 21:47:44 +04:00
|
|
|
name = symbol.name;
|
2014-05-16 02:25:56 +04:00
|
|
|
} else {
|
2014-09-07 22:50:43 +04:00
|
|
|
name = Common::StringFromFormat("unknown-%08x", entry_point);
|
2014-05-14 05:58:26 +04:00
|
|
|
}
|
2014-05-17 21:47:44 +04:00
|
|
|
|
2015-03-24 06:55:21 +03:00
|
|
|
// TODO(bunnei): Implement resource limits to return an error code instead of the below assert.
|
|
|
|
// The error code should be: Description::NotAuthorized, Module::OS, Summary::WrongArgument,
|
|
|
|
// Level::Permanent
|
|
|
|
ASSERT_MSG(priority >= THREADPRIO_USERLAND_MAX, "Unexpected thread priority!");
|
|
|
|
|
|
|
|
if (priority > THREADPRIO_LOWEST) {
|
|
|
|
return ResultCode(ErrorDescription::OutOfRange, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
|
|
|
|
2015-04-10 05:55:18 +03:00
|
|
|
switch (processor_id) {
|
2016-03-01 14:03:32 +03:00
|
|
|
case THREADPROCESSORID_ALL:
|
2015-04-10 05:55:18 +03:00
|
|
|
case THREADPROCESSORID_DEFAULT:
|
|
|
|
case THREADPROCESSORID_0:
|
|
|
|
case THREADPROCESSORID_1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO(bunnei): Implement support for other processor IDs
|
|
|
|
ASSERT_MSG(false, "Unsupported thread processor ID: %d", processor_id);
|
|
|
|
break;
|
2015-03-24 06:55:21 +03:00
|
|
|
}
|
|
|
|
|
2016-04-17 22:01:40 +03:00
|
|
|
if (processor_id == THREADPROCESSORID_1 || processor_id == THREADPROCESSORID_ALL ||
|
2016-09-18 03:38:01 +03:00
|
|
|
(processor_id == THREADPROCESSORID_DEFAULT &&
|
|
|
|
Kernel::g_current_process->ideal_processor == THREADPROCESSORID_1)) {
|
|
|
|
LOG_WARNING(Kernel_SVC,
|
|
|
|
"Newly created thread is allowed to be run in the SysCore, unimplemented.");
|
2016-04-17 22:01:40 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
CASCADE_RESULT(SharedPtr<Thread> thread, Kernel::Thread::Create(name, entry_point, priority,
|
|
|
|
arg, processor_id, stack_top));
|
2016-05-14 16:45:04 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
thread->context.fpscr =
|
|
|
|
FPSCR_DEFAULT_NAN | FPSCR_FLUSH_TO_ZERO | FPSCR_ROUND_TOZERO; // 0x03C00000
|
2016-05-14 16:45:04 +03:00
|
|
|
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(thread)));
|
2014-05-22 05:41:40 +04:00
|
|
|
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
|
2016-09-18 03:38:01 +03:00
|
|
|
"threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X",
|
|
|
|
entry_point, name.c_str(), arg, stack_top, priority, processor_id, *out_handle);
|
2014-11-19 11:49:13 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-14 05:58:26 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 06:35:36 +04:00
|
|
|
/// Called when a thread exits
|
2014-12-22 16:07:22 +03:00
|
|
|
static void ExitThread() {
|
|
|
|
LOG_TRACE(Kernel_SVC, "called, pc=0x%08X", Core::g_app_core->GetPC());
|
2014-06-06 06:35:36 +04:00
|
|
|
|
2015-01-26 09:56:17 +03:00
|
|
|
Kernel::GetCurrentThread()->Stop();
|
2014-06-06 06:35:36 +04:00
|
|
|
}
|
|
|
|
|
2014-06-02 06:12:54 +04:00
|
|
|
/// Gets the priority for the specified thread
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode GetThreadPriority(s32* priority, Handle handle) {
|
2014-12-29 15:55:30 +03:00
|
|
|
const SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
|
2014-12-22 16:07:22 +03:00
|
|
|
if (thread == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 16:07:22 +03:00
|
|
|
|
|
|
|
*priority = thread->GetPriority();
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-02 06:12:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the priority for the specified thread
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode SetThreadPriority(Handle handle, s32 priority) {
|
2014-12-29 15:55:30 +03:00
|
|
|
SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
|
2014-12-22 16:07:22 +03:00
|
|
|
if (thread == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 16:07:22 +03:00
|
|
|
|
|
|
|
thread->SetPriority(priority);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-02 06:12:54 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 06:57:54 +04:00
|
|
|
/// Create a mutex
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateMutex(Handle* out_handle, u32 initial_locked) {
|
2015-01-23 04:12:19 +03:00
|
|
|
using Kernel::Mutex;
|
|
|
|
|
2015-02-01 05:14:40 +03:00
|
|
|
SharedPtr<Mutex> mutex = Mutex::Create(initial_locked != 0);
|
2016-04-08 21:53:37 +03:00
|
|
|
mutex->name = Common::StringFromFormat("mutex-%08x", Core::g_app_core->GetReg(14));
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(mutex)));
|
2015-01-23 04:12:19 +03:00
|
|
|
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called initial_locked=%s : created handle=0x%08X",
|
2016-09-18 03:38:01 +03:00
|
|
|
initial_locked ? "true" : "false", *out_handle);
|
2015-05-20 03:24:30 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-14 05:58:26 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 06:57:54 +04:00
|
|
|
/// Release a mutex
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode ReleaseMutex(Handle handle) {
|
2015-01-23 04:12:19 +03:00
|
|
|
using Kernel::Mutex;
|
|
|
|
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x%08X", handle);
|
2015-01-23 04:12:19 +03:00
|
|
|
|
|
|
|
SharedPtr<Mutex> mutex = Kernel::g_handle_table.Get<Mutex>(handle);
|
|
|
|
if (mutex == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 04:12:19 +03:00
|
|
|
|
|
|
|
mutex->Release();
|
2015-04-07 04:58:05 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-14 05:58:26 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 17:15:10 +03:00
|
|
|
/// Get the ID of the specified process
|
2015-05-12 02:23:45 +03:00
|
|
|
static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
|
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x%08X", process_handle);
|
2015-05-11 17:15:10 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
const SharedPtr<Kernel::Process> process =
|
|
|
|
Kernel::g_handle_table.Get<Kernel::Process>(process_handle);
|
2015-05-11 17:15:10 +03:00
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2015-05-12 02:23:45 +03:00
|
|
|
*process_id = process->process_id;
|
2015-05-11 17:15:10 +03:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the ID of the process that owns the specified thread
|
2015-05-12 02:23:45 +03:00
|
|
|
static ResultCode GetProcessIdOfThread(u32* process_id, Handle thread_handle) {
|
|
|
|
LOG_TRACE(Kernel_SVC, "called thread=0x%08X", thread_handle);
|
2015-05-11 17:15:10 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
const SharedPtr<Kernel::Thread> thread =
|
|
|
|
Kernel::g_handle_table.Get<Kernel::Thread>(thread_handle);
|
2015-05-11 17:15:10 +03:00
|
|
|
if (thread == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
const SharedPtr<Kernel::Process> process = thread->owner_process;
|
2015-05-25 21:34:09 +03:00
|
|
|
|
2015-05-12 02:23:45 +03:00
|
|
|
ASSERT_MSG(process != nullptr, "Invalid parent process for thread=0x%08X", thread_handle);
|
2015-05-11 17:15:10 +03:00
|
|
|
|
|
|
|
*process_id = process->process_id;
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-12-04 07:22:06 +03:00
|
|
|
/// Get the ID for the specified thread.
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode GetThreadId(u32* thread_id, Handle handle) {
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called thread=0x%08X", handle);
|
2014-12-22 16:07:22 +03:00
|
|
|
|
2014-12-29 15:55:30 +03:00
|
|
|
const SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
|
2014-12-22 16:07:22 +03:00
|
|
|
if (thread == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 16:07:22 +03:00
|
|
|
|
|
|
|
*thread_id = thread->GetThreadId();
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-16 04:17:30 +04:00
|
|
|
}
|
|
|
|
|
2014-12-04 02:49:51 +03:00
|
|
|
/// Creates a semaphore
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateSemaphore(Handle* out_handle, s32 initial_count, s32 max_count) {
|
2015-01-11 18:53:11 +03:00
|
|
|
using Kernel::Semaphore;
|
|
|
|
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(SharedPtr<Semaphore> semaphore, Semaphore::Create(initial_count, max_count));
|
2016-04-08 21:53:37 +03:00
|
|
|
semaphore->name = Common::StringFromFormat("semaphore-%08x", Core::g_app_core->GetReg(14));
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(semaphore)));
|
2015-01-11 18:53:11 +03:00
|
|
|
|
2014-12-13 21:43:01 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X",
|
2016-09-18 03:38:01 +03:00
|
|
|
initial_count, max_count, *out_handle);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 19:40:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Releases a certain number of slots in a semaphore
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
|
2015-01-11 18:53:11 +03:00
|
|
|
using Kernel::Semaphore;
|
|
|
|
|
|
|
|
LOG_TRACE(Kernel_SVC, "called release_count=%d, handle=0x%08X", release_count, handle);
|
|
|
|
|
|
|
|
SharedPtr<Semaphore> semaphore = Kernel::g_handle_table.Get<Semaphore>(handle);
|
|
|
|
if (semaphore == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 18:53:11 +03:00
|
|
|
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*count, semaphore->Release(release_count));
|
2015-04-07 04:58:05 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 02:49:51 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 22:45:12 +03:00
|
|
|
/// Query process memory
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* page_info,
|
|
|
|
Handle process_handle, u32 addr) {
|
2015-07-17 22:45:12 +03:00
|
|
|
using Kernel::Process;
|
|
|
|
Kernel::SharedPtr<Process> process = Kernel::g_handle_table.Get<Process>(process_handle);
|
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2015-07-18 05:19:16 +03:00
|
|
|
auto vma = process->vm_manager.FindVMA(addr);
|
2015-07-17 09:23:22 +03:00
|
|
|
|
2015-07-18 05:19:16 +03:00
|
|
|
if (vma == Kernel::g_current_process->vm_manager.vma_map.end())
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
2015-07-17 09:23:22 +03:00
|
|
|
|
|
|
|
memory_info->base_address = vma->second.base;
|
|
|
|
memory_info->permission = static_cast<u32>(vma->second.permissions);
|
|
|
|
memory_info->size = vma->second.size;
|
|
|
|
memory_info->state = static_cast<u32>(vma->second.meminfo_state);
|
|
|
|
|
|
|
|
page_info->flags = 0;
|
2015-07-17 22:45:12 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x%08X addr=0x%08X", process_handle, addr);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-16 04:17:30 +04:00
|
|
|
}
|
|
|
|
|
2015-07-17 22:45:12 +03:00
|
|
|
/// Query memory
|
|
|
|
static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, u32 addr) {
|
|
|
|
return QueryProcessMemory(memory_info, page_info, Kernel::CurrentProcess, addr);
|
|
|
|
}
|
|
|
|
|
2014-05-21 06:57:54 +04:00
|
|
|
/// Create an event
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateEvent(Handle* out_handle, u32 reset_type) {
|
2015-02-01 05:14:40 +03:00
|
|
|
using Kernel::Event;
|
|
|
|
|
2016-03-13 05:47:41 +03:00
|
|
|
SharedPtr<Event> evt = Event::Create(static_cast<Kernel::ResetType>(reset_type));
|
2016-04-08 21:53:37 +03:00
|
|
|
evt->name = Common::StringFromFormat("event-%08x", Core::g_app_core->GetReg(14));
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(evt)));
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called reset_type=0x%08X : created handle=0x%08X", reset_type,
|
|
|
|
*out_handle);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-19 01:52:02 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 04:55:48 +04:00
|
|
|
/// Duplicates a kernel handle
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode DuplicateHandle(Handle* out, Handle handle) {
|
2015-01-31 21:12:54 +03:00
|
|
|
CASCADE_RESULT(*out, Kernel::g_handle_table.Duplicate(handle));
|
|
|
|
LOG_TRACE(Kernel_SVC, "duplicated 0x%08X to 0x%08X", handle, *out);
|
|
|
|
return RESULT_SUCCESS;
|
2014-05-27 04:55:48 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 06:35:36 +04:00
|
|
|
/// Signals an event
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode SignalEvent(Handle handle) {
|
2015-02-01 05:14:40 +03:00
|
|
|
using Kernel::Event;
|
2015-01-23 08:11:25 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called event=0x%08X", handle);
|
|
|
|
|
2015-02-01 05:14:40 +03:00
|
|
|
SharedPtr<Event> evt = Kernel::g_handle_table.Get<Kernel::Event>(handle);
|
2015-01-23 08:11:25 +03:00
|
|
|
if (evt == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 08:11:25 +03:00
|
|
|
|
|
|
|
evt->Signal();
|
2015-05-20 03:24:30 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-06 06:35:36 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:24:51 +04:00
|
|
|
/// Clears an event
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode ClearEvent(Handle handle) {
|
2015-02-01 05:14:40 +03:00
|
|
|
using Kernel::Event;
|
2015-01-23 08:11:25 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called event=0x%08X", handle);
|
|
|
|
|
2015-02-01 05:14:40 +03:00
|
|
|
SharedPtr<Event> evt = Kernel::g_handle_table.Get<Kernel::Event>(handle);
|
2015-01-23 08:11:25 +03:00
|
|
|
if (evt == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 08:11:25 +03:00
|
|
|
|
|
|
|
evt->Clear();
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-30 04:24:51 +04:00
|
|
|
}
|
|
|
|
|
2014-12-04 22:45:47 +03:00
|
|
|
/// Creates a timer
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateTimer(Handle* out_handle, u32 reset_type) {
|
2015-01-23 07:19:33 +03:00
|
|
|
using Kernel::Timer;
|
|
|
|
|
2016-03-13 05:47:41 +03:00
|
|
|
SharedPtr<Timer> timer = Timer::Create(static_cast<Kernel::ResetType>(reset_type));
|
2016-04-08 21:53:37 +03:00
|
|
|
timer->name = Common::StringFromFormat("timer-%08x", Core::g_app_core->GetReg(14));
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(timer)));
|
2015-01-23 07:19:33 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called reset_type=0x%08X : created handle=0x%08X", reset_type,
|
|
|
|
*out_handle);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears a timer
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode ClearTimer(Handle handle) {
|
2015-01-23 07:19:33 +03:00
|
|
|
using Kernel::Timer;
|
|
|
|
|
2014-12-04 22:45:47 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
|
|
|
|
if (timer == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
timer->Clear();
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a timer
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode SetTimer(Handle handle, s64 initial, s64 interval) {
|
2015-01-23 07:19:33 +03:00
|
|
|
using Kernel::Timer;
|
|
|
|
|
2014-12-04 22:45:47 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
|
|
|
|
if (timer == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
timer->Set(initial, interval);
|
2015-04-07 04:58:05 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancels a timer
|
2015-01-23 08:36:58 +03:00
|
|
|
static ResultCode CancelTimer(Handle handle) {
|
2015-01-23 07:19:33 +03:00
|
|
|
using Kernel::Timer;
|
|
|
|
|
2014-12-04 22:45:47 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
|
|
|
|
if (timer == nullptr)
|
2015-01-23 08:44:52 +03:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 07:19:33 +03:00
|
|
|
|
|
|
|
timer->Cancel();
|
2015-04-07 04:58:05 +03:00
|
|
|
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
2014-06-01 18:37:19 +04:00
|
|
|
/// Sleep the current thread
|
2014-11-17 06:58:39 +03:00
|
|
|
static void SleepThread(s64 nanoseconds) {
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds);
|
2014-11-26 08:34:14 +03:00
|
|
|
|
2014-12-20 10:32:19 +03:00
|
|
|
// Sleep current thread and check for next thread to schedule
|
2015-01-18 21:56:40 +03:00
|
|
|
Kernel::WaitCurrentThread_Sleep();
|
2015-01-08 00:40:08 +03:00
|
|
|
|
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2015-01-31 04:07:54 +03:00
|
|
|
Kernel::GetCurrentThread()->WakeAfterDelay(nanoseconds);
|
2014-06-01 18:37:19 +04:00
|
|
|
}
|
|
|
|
|
2014-08-15 04:01:02 +04:00
|
|
|
/// This returns the total CPU ticks elapsed since the CPU was powered-on
|
2014-11-17 06:58:39 +03:00
|
|
|
static s64 GetSystemTick() {
|
2015-08-26 05:47:29 +03:00
|
|
|
s64 result = CoreTiming::GetTicks();
|
|
|
|
// Advance time to defeat dumb games (like Cubic Ninja) that busy-wait for the frame to end.
|
2016-09-18 03:38:01 +03:00
|
|
|
Core::g_app_core->AddTicks(
|
|
|
|
150); // Measured time between two calls on a 9.2 o3DS with Ninjhax 1.1b
|
2015-08-26 05:47:29 +03:00
|
|
|
return result;
|
2014-08-15 04:01:02 +04:00
|
|
|
}
|
|
|
|
|
2014-12-09 07:52:27 +03:00
|
|
|
/// Creates a memory block at the specified address with the specified permissions and size
|
2015-01-23 09:00:39 +03:00
|
|
|
static ResultCode CreateMemoryBlock(Handle* out_handle, u32 addr, u32 size, u32 my_permission,
|
2016-09-18 03:38:01 +03:00
|
|
|
u32 other_permission) {
|
2015-01-11 08:43:29 +03:00
|
|
|
using Kernel::SharedMemory;
|
2015-12-31 17:46:32 +03:00
|
|
|
|
|
|
|
if (size % Memory::PAGE_SIZE != 0)
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::MisalignedSize, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
2015-12-31 17:46:32 +03:00
|
|
|
|
2016-04-18 05:07:52 +03:00
|
|
|
SharedPtr<SharedMemory> shared_memory = nullptr;
|
2014-12-09 07:52:27 +03:00
|
|
|
|
2015-05-11 01:47:07 +03:00
|
|
|
using Kernel::MemoryPermission;
|
2016-04-18 05:07:52 +03:00
|
|
|
auto VerifyPermissions = [](MemoryPermission permission) {
|
|
|
|
// SharedMemory blocks can not be created with Execute permissions
|
|
|
|
switch (permission) {
|
|
|
|
case MemoryPermission::None:
|
|
|
|
case MemoryPermission::Read:
|
|
|
|
case MemoryPermission::Write:
|
|
|
|
case MemoryPermission::ReadWrite:
|
2016-04-18 05:58:51 +03:00
|
|
|
case MemoryPermission::DontCare:
|
2016-04-18 05:07:52 +03:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!VerifyPermissions(static_cast<MemoryPermission>(my_permission)) ||
|
|
|
|
!VerifyPermissions(static_cast<MemoryPermission>(other_permission)))
|
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
|
|
|
|
if (addr < Memory::PROCESS_IMAGE_VADDR || addr + size > Memory::SHARED_MEMORY_VADDR_END) {
|
2016-09-18 03:38:01 +03:00
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
2016-04-18 05:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// When trying to create a memory block with address = 0,
|
|
|
|
// if the process has the Shared Device Memory flag in the exheader,
|
2016-09-18 03:38:01 +03:00
|
|
|
// then we have to allocate from the same region as the caller process instead of the BASE
|
|
|
|
// region.
|
2016-04-18 05:07:52 +03:00
|
|
|
Kernel::MemoryRegion region = Kernel::MemoryRegion::BASE;
|
|
|
|
if (addr == 0 && Kernel::g_current_process->flags.shared_device_mem)
|
|
|
|
region = Kernel::g_current_process->flags.memory_region;
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
shared_memory = SharedMemory::Create(
|
|
|
|
Kernel::g_current_process, size, static_cast<MemoryPermission>(my_permission),
|
|
|
|
static_cast<MemoryPermission>(other_permission), addr, region);
|
2015-01-23 09:00:39 +03:00
|
|
|
CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(shared_memory)));
|
2015-01-11 08:43:29 +03:00
|
|
|
|
2016-04-18 05:07:52 +03:00
|
|
|
LOG_WARNING(Kernel_SVC, "called addr=0x%08X", addr);
|
2015-01-23 08:36:58 +03:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-09 07:52:27 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
static ResultCode CreatePort(Handle* server_port, Handle* client_port, const char* name,
|
|
|
|
u32 max_sessions) {
|
2016-05-22 20:30:13 +03:00
|
|
|
// TODO(Subv): Implement named ports.
|
|
|
|
ASSERT_MSG(name == nullptr, "Named ports are currently unimplemented");
|
|
|
|
|
|
|
|
using Kernel::ServerPort;
|
|
|
|
using Kernel::ClientPort;
|
|
|
|
using Kernel::SharedPtr;
|
|
|
|
|
|
|
|
auto ports = ServerPort::CreatePortPair(max_sessions);
|
2016-09-18 03:38:01 +03:00
|
|
|
CASCADE_RESULT(*client_port, Kernel::g_handle_table.Create(
|
|
|
|
std::move(std::get<SharedPtr<ClientPort>>(ports))));
|
|
|
|
// Note: The 3DS kernel also leaks the client port handle if the server port handle fails to be
|
|
|
|
// created.
|
|
|
|
CASCADE_RESULT(*server_port, Kernel::g_handle_table.Create(
|
|
|
|
std::move(std::get<SharedPtr<ServerPort>>(ports))));
|
2016-05-22 20:30:13 +03:00
|
|
|
|
|
|
|
LOG_TRACE(Kernel_SVC, "called max_sessions=%u", max_sessions);
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-11-27 06:00:16 +03:00
|
|
|
static ResultCode GetSystemInfo(s64* out, u32 type, s32 param) {
|
|
|
|
using Kernel::MemoryRegion;
|
|
|
|
|
2015-12-13 02:14:36 +03:00
|
|
|
LOG_TRACE(Kernel_SVC, "called type=%u param=%d", type, param);
|
2015-11-27 06:00:16 +03:00
|
|
|
|
|
|
|
switch ((SystemInfoType)type) {
|
|
|
|
case SystemInfoType::REGION_MEMORY_USAGE:
|
|
|
|
switch ((SystemInfoMemUsageRegion)param) {
|
|
|
|
case SystemInfoMemUsageRegion::ALL:
|
2016-09-18 03:38:01 +03:00
|
|
|
*out = Kernel::GetMemoryRegion(Kernel::MemoryRegion::APPLICATION)->used +
|
|
|
|
Kernel::GetMemoryRegion(Kernel::MemoryRegion::SYSTEM)->used +
|
|
|
|
Kernel::GetMemoryRegion(Kernel::MemoryRegion::BASE)->used;
|
2015-11-27 06:00:16 +03:00
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::APPLICATION:
|
|
|
|
*out = Kernel::GetMemoryRegion(Kernel::MemoryRegion::APPLICATION)->used;
|
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::SYSTEM:
|
|
|
|
*out = Kernel::GetMemoryRegion(Kernel::MemoryRegion::SYSTEM)->used;
|
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::BASE:
|
|
|
|
*out = Kernel::GetMemoryRegion(Kernel::MemoryRegion::BASE)->used;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetSystemInfo type=0 region: param=%d", param);
|
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SystemInfoType::KERNEL_ALLOCATED_PAGES:
|
2015-12-25 22:21:45 +03:00
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented GetSystemInfo type=2 param=%d", param);
|
2015-11-27 06:00:16 +03:00
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
case SystemInfoType::KERNEL_SPAWNED_PIDS:
|
|
|
|
*out = 5;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetSystemInfo type=%u param=%d", type, param);
|
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function never returns an error, even if invalid parameters were passed.
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-08-06 03:39:53 +03:00
|
|
|
static ResultCode GetProcessInfo(s64* out, Handle process_handle, u32 type) {
|
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x%08X type=%u", process_handle, type);
|
|
|
|
|
|
|
|
using Kernel::Process;
|
|
|
|
Kernel::SharedPtr<Process> process = Kernel::g_handle_table.Get<Process>(process_handle);
|
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
// TODO(yuriks): Type 0 returns a slightly higher number than type 2, but I'm not sure
|
|
|
|
// what's the difference between them.
|
|
|
|
*out = process->heap_used + process->linear_heap_used + process->misc_memory_used;
|
2016-09-18 03:38:01 +03:00
|
|
|
if (*out % Memory::PAGE_SIZE != 0) {
|
2016-04-07 11:26:12 +03:00
|
|
|
LOG_ERROR(Kernel_SVC, "called, memory size not page-aligned");
|
|
|
|
return ERR_MISALIGNED_SIZE;
|
|
|
|
}
|
2015-08-06 03:39:53 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
// These are valid, but not implemented yet
|
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented GetProcessInfo type=%u", type);
|
|
|
|
break;
|
|
|
|
case 20:
|
|
|
|
*out = Memory::FCRAM_PADDR - process->GetLinearHeapBase();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetProcessInfo type=%u", type);
|
|
|
|
|
|
|
|
if (type >= 21 && type <= 23) {
|
|
|
|
return ResultCode( // 0xE0E01BF4
|
2016-09-18 03:38:01 +03:00
|
|
|
ErrorDescription::NotImplemented, ErrorModule::OS, ErrorSummary::InvalidArgument,
|
|
|
|
ErrorLevel::Usage);
|
2015-08-06 03:39:53 +03:00
|
|
|
} else {
|
|
|
|
return ResultCode( // 0xD8E007ED
|
2016-09-18 03:38:01 +03:00
|
|
|
ErrorDescription::InvalidEnumValue, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
2015-08-06 03:39:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-06 06:04:25 +03:00
|
|
|
namespace {
|
2016-09-18 03:38:01 +03:00
|
|
|
struct FunctionDef {
|
|
|
|
using Func = void();
|
2015-05-06 06:04:25 +03:00
|
|
|
|
2016-09-18 03:38:01 +03:00
|
|
|
u32 id;
|
|
|
|
Func* func;
|
|
|
|
const char* name;
|
|
|
|
};
|
2015-05-06 06:04:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const FunctionDef SVC_Table[] = {
|
2016-09-18 03:38:01 +03:00
|
|
|
{0x00, nullptr, "Unknown"},
|
|
|
|
{0x01, HLE::Wrap<ControlMemory>, "ControlMemory"},
|
|
|
|
{0x02, HLE::Wrap<QueryMemory>, "QueryMemory"},
|
|
|
|
{0x03, nullptr, "ExitProcess"},
|
|
|
|
{0x04, nullptr, "GetProcessAffinityMask"},
|
|
|
|
{0x05, nullptr, "SetProcessAffinityMask"},
|
|
|
|
{0x06, nullptr, "GetProcessIdealProcessor"},
|
|
|
|
{0x07, nullptr, "SetProcessIdealProcessor"},
|
|
|
|
{0x08, HLE::Wrap<CreateThread>, "CreateThread"},
|
|
|
|
{0x09, ExitThread, "ExitThread"},
|
|
|
|
{0x0A, HLE::Wrap<SleepThread>, "SleepThread"},
|
|
|
|
{0x0B, HLE::Wrap<GetThreadPriority>, "GetThreadPriority"},
|
|
|
|
{0x0C, HLE::Wrap<SetThreadPriority>, "SetThreadPriority"},
|
|
|
|
{0x0D, nullptr, "GetThreadAffinityMask"},
|
|
|
|
{0x0E, nullptr, "SetThreadAffinityMask"},
|
|
|
|
{0x0F, nullptr, "GetThreadIdealProcessor"},
|
|
|
|
{0x10, nullptr, "SetThreadIdealProcessor"},
|
|
|
|
{0x11, nullptr, "GetCurrentProcessorNumber"},
|
|
|
|
{0x12, nullptr, "Run"},
|
|
|
|
{0x13, HLE::Wrap<CreateMutex>, "CreateMutex"},
|
|
|
|
{0x14, HLE::Wrap<ReleaseMutex>, "ReleaseMutex"},
|
|
|
|
{0x15, HLE::Wrap<CreateSemaphore>, "CreateSemaphore"},
|
|
|
|
{0x16, HLE::Wrap<ReleaseSemaphore>, "ReleaseSemaphore"},
|
|
|
|
{0x17, HLE::Wrap<CreateEvent>, "CreateEvent"},
|
|
|
|
{0x18, HLE::Wrap<SignalEvent>, "SignalEvent"},
|
|
|
|
{0x19, HLE::Wrap<ClearEvent>, "ClearEvent"},
|
|
|
|
{0x1A, HLE::Wrap<CreateTimer>, "CreateTimer"},
|
|
|
|
{0x1B, HLE::Wrap<SetTimer>, "SetTimer"},
|
|
|
|
{0x1C, HLE::Wrap<CancelTimer>, "CancelTimer"},
|
|
|
|
{0x1D, HLE::Wrap<ClearTimer>, "ClearTimer"},
|
|
|
|
{0x1E, HLE::Wrap<CreateMemoryBlock>, "CreateMemoryBlock"},
|
|
|
|
{0x1F, HLE::Wrap<MapMemoryBlock>, "MapMemoryBlock"},
|
|
|
|
{0x20, HLE::Wrap<UnmapMemoryBlock>, "UnmapMemoryBlock"},
|
2014-06-10 06:30:39 +04:00
|
|
|
{0x21, HLE::Wrap<CreateAddressArbiter>, "CreateAddressArbiter"},
|
2016-09-18 03:38:01 +03:00
|
|
|
{0x22, HLE::Wrap<ArbitrateAddress>, "ArbitrateAddress"},
|
|
|
|
{0x23, HLE::Wrap<CloseHandle>, "CloseHandle"},
|
2014-06-10 06:30:39 +04:00
|
|
|
{0x24, HLE::Wrap<WaitSynchronization1>, "WaitSynchronization1"},
|
|
|
|
{0x25, HLE::Wrap<WaitSynchronizationN>, "WaitSynchronizationN"},
|
2016-09-18 03:38:01 +03:00
|
|
|
{0x26, nullptr, "SignalAndWait"},
|
|
|
|
{0x27, HLE::Wrap<DuplicateHandle>, "DuplicateHandle"},
|
|
|
|
{0x28, HLE::Wrap<GetSystemTick>, "GetSystemTick"},
|
|
|
|
{0x29, nullptr, "GetHandleInfo"},
|
|
|
|
{0x2A, HLE::Wrap<GetSystemInfo>, "GetSystemInfo"},
|
|
|
|
{0x2B, HLE::Wrap<GetProcessInfo>, "GetProcessInfo"},
|
|
|
|
{0x2C, nullptr, "GetThreadInfo"},
|
|
|
|
{0x2D, HLE::Wrap<ConnectToPort>, "ConnectToPort"},
|
|
|
|
{0x2E, nullptr, "SendSyncRequest1"},
|
|
|
|
{0x2F, nullptr, "SendSyncRequest2"},
|
|
|
|
{0x30, nullptr, "SendSyncRequest3"},
|
|
|
|
{0x31, nullptr, "SendSyncRequest4"},
|
|
|
|
{0x32, HLE::Wrap<SendSyncRequest>, "SendSyncRequest"},
|
|
|
|
{0x33, nullptr, "OpenProcess"},
|
|
|
|
{0x34, nullptr, "OpenThread"},
|
|
|
|
{0x35, HLE::Wrap<GetProcessId>, "GetProcessId"},
|
2015-05-11 17:15:10 +03:00
|
|
|
{0x36, HLE::Wrap<GetProcessIdOfThread>, "GetProcessIdOfThread"},
|
2016-09-18 03:38:01 +03:00
|
|
|
{0x37, HLE::Wrap<GetThreadId>, "GetThreadId"},
|
|
|
|
{0x38, HLE::Wrap<GetResourceLimit>, "GetResourceLimit"},
|
2015-05-12 23:25:15 +03:00
|
|
|
{0x39, HLE::Wrap<GetResourceLimitLimitValues>, "GetResourceLimitLimitValues"},
|
2014-06-10 06:30:39 +04:00
|
|
|
{0x3A, HLE::Wrap<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
|
2016-09-18 03:38:01 +03:00
|
|
|
{0x3B, nullptr, "GetThreadContext"},
|
|
|
|
{0x3C, HLE::Wrap<Break>, "Break"},
|
|
|
|
{0x3D, HLE::Wrap<OutputDebugString>, "OutputDebugString"},
|
|
|
|
{0x3E, nullptr, "ControlPerformanceCounter"},
|
|
|
|
{0x3F, nullptr, "Unknown"},
|
|
|
|
{0x40, nullptr, "Unknown"},
|
|
|
|
{0x41, nullptr, "Unknown"},
|
|
|
|
{0x42, nullptr, "Unknown"},
|
|
|
|
{0x43, nullptr, "Unknown"},
|
|
|
|
{0x44, nullptr, "Unknown"},
|
|
|
|
{0x45, nullptr, "Unknown"},
|
|
|
|
{0x46, nullptr, "Unknown"},
|
|
|
|
{0x47, HLE::Wrap<CreatePort>, "CreatePort"},
|
|
|
|
{0x48, nullptr, "CreateSessionToPort"},
|
|
|
|
{0x49, nullptr, "CreateSession"},
|
|
|
|
{0x4A, nullptr, "AcceptSession"},
|
|
|
|
{0x4B, nullptr, "ReplyAndReceive1"},
|
|
|
|
{0x4C, nullptr, "ReplyAndReceive2"},
|
|
|
|
{0x4D, nullptr, "ReplyAndReceive3"},
|
|
|
|
{0x4E, nullptr, "ReplyAndReceive4"},
|
|
|
|
{0x4F, nullptr, "ReplyAndReceive"},
|
|
|
|
{0x50, nullptr, "BindInterrupt"},
|
|
|
|
{0x51, nullptr, "UnbindInterrupt"},
|
|
|
|
{0x52, nullptr, "InvalidateProcessDataCache"},
|
|
|
|
{0x53, nullptr, "StoreProcessDataCache"},
|
|
|
|
{0x54, nullptr, "FlushProcessDataCache"},
|
|
|
|
{0x55, nullptr, "StartInterProcessDma"},
|
|
|
|
{0x56, nullptr, "StopDma"},
|
|
|
|
{0x57, nullptr, "GetDmaState"},
|
|
|
|
{0x58, nullptr, "RestartDma"},
|
|
|
|
{0x59, nullptr, "Unknown"},
|
|
|
|
{0x5A, nullptr, "Unknown"},
|
|
|
|
{0x5B, nullptr, "Unknown"},
|
|
|
|
{0x5C, nullptr, "Unknown"},
|
|
|
|
{0x5D, nullptr, "Unknown"},
|
|
|
|
{0x5E, nullptr, "Unknown"},
|
|
|
|
{0x5F, nullptr, "Unknown"},
|
|
|
|
{0x60, nullptr, "DebugActiveProcess"},
|
|
|
|
{0x61, nullptr, "BreakDebugProcess"},
|
|
|
|
{0x62, nullptr, "TerminateDebugProcess"},
|
|
|
|
{0x63, nullptr, "GetProcessDebugEvent"},
|
|
|
|
{0x64, nullptr, "ContinueDebugEvent"},
|
|
|
|
{0x65, nullptr, "GetProcessList"},
|
|
|
|
{0x66, nullptr, "GetThreadList"},
|
|
|
|
{0x67, nullptr, "GetDebugThreadContext"},
|
|
|
|
{0x68, nullptr, "SetDebugThreadContext"},
|
|
|
|
{0x69, nullptr, "QueryDebugProcessMemory"},
|
|
|
|
{0x6A, nullptr, "ReadProcessMemory"},
|
|
|
|
{0x6B, nullptr, "WriteProcessMemory"},
|
|
|
|
{0x6C, nullptr, "SetHardwareBreakPoint"},
|
|
|
|
{0x6D, nullptr, "GetDebugThreadParam"},
|
|
|
|
{0x6E, nullptr, "Unknown"},
|
|
|
|
{0x6F, nullptr, "Unknown"},
|
|
|
|
{0x70, nullptr, "ControlProcessMemory"},
|
|
|
|
{0x71, nullptr, "MapProcessMemory"},
|
|
|
|
{0x72, nullptr, "UnmapProcessMemory"},
|
|
|
|
{0x73, nullptr, "CreateCodeSet"},
|
|
|
|
{0x74, nullptr, "RandomStub"},
|
|
|
|
{0x75, nullptr, "CreateProcess"},
|
|
|
|
{0x76, nullptr, "TerminateProcess"},
|
|
|
|
{0x77, nullptr, "SetProcessResourceLimits"},
|
|
|
|
{0x78, nullptr, "CreateResourceLimit"},
|
|
|
|
{0x79, nullptr, "SetResourceLimitValues"},
|
|
|
|
{0x7A, nullptr, "AddCodeSegment"},
|
|
|
|
{0x7B, nullptr, "Backdoor"},
|
|
|
|
{0x7C, nullptr, "KernelSetState"},
|
|
|
|
{0x7D, HLE::Wrap<QueryProcessMemory>, "QueryProcessMemory"},
|
2014-04-11 03:58:28 +04:00
|
|
|
};
|
|
|
|
|
2015-07-21 10:51:36 +03:00
|
|
|
static const FunctionDef* GetSVCInfo(u32 func_num) {
|
2015-05-06 06:04:25 +03:00
|
|
|
if (func_num >= ARRAY_SIZE(SVC_Table)) {
|
|
|
|
LOG_ERROR(Kernel_SVC, "unknown svc=0x%02X", func_num);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &SVC_Table[func_num];
|
|
|
|
}
|
|
|
|
|
2015-08-18 00:25:21 +03:00
|
|
|
MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
|
|
|
|
|
2015-07-21 10:51:36 +03:00
|
|
|
void CallSVC(u32 immediate) {
|
2015-08-18 00:25:21 +03:00
|
|
|
MICROPROFILE_SCOPE(Kernel_SVC);
|
2015-05-06 06:04:25 +03:00
|
|
|
|
2015-07-21 10:51:36 +03:00
|
|
|
const FunctionDef* info = GetSVCInfo(immediate);
|
2015-05-06 06:04:25 +03:00
|
|
|
if (info) {
|
|
|
|
if (info->func) {
|
|
|
|
info->func();
|
2016-12-04 17:58:36 +03:00
|
|
|
// TODO(Subv): Not all service functions should cause a reschedule in all cases.
|
2016-12-04 06:38:14 +03:00
|
|
|
HLE::Reschedule(__func__);
|
2015-05-06 06:04:25 +03:00
|
|
|
} else {
|
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name);
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 03:58:28 +04:00
|
|
|
}
|
2014-04-12 02:44:21 +04:00
|
|
|
|
|
|
|
} // namespace
|