2014-04-11 01:58:28 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 09:49:13 +01:00
|
|
|
// Refer to the license.txt file included.
|
2014-04-11 01:58:28 +02:00
|
|
|
|
2017-05-30 01:45:42 +02:00
|
|
|
#include <algorithm>
|
2017-05-07 23:56:23 +02:00
|
|
|
#include <cinttypes>
|
2014-04-12 00:44:21 +02:00
|
|
|
#include <map>
|
2018-04-30 00:37:15 +02:00
|
|
|
#include <fmt/format.h>
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/logging/log.h"
|
2015-08-17 23:25:21 +02:00
|
|
|
#include "common/microprofile.h"
|
2016-05-21 16:45:18 +02:00
|
|
|
#include "common/scope_exit.h"
|
2015-03-16 17:16:59 +01:00
|
|
|
#include "core/arm/arm_interface.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/core_timing.h"
|
2016-09-20 17:21:23 +02:00
|
|
|
#include "core/hle/function_wrappers.h"
|
2014-07-07 05:15:40 +02:00
|
|
|
#include "core/hle/kernel/address_arbiter.h"
|
2016-05-22 19:30:13 +02:00
|
|
|
#include "core/hle/kernel/client_port.h"
|
2016-06-15 01:03:30 +02:00
|
|
|
#include "core/hle/kernel/client_session.h"
|
2017-05-21 09:11:36 +02:00
|
|
|
#include "core/hle/kernel/errors.h"
|
2014-05-28 04:41:09 +02:00
|
|
|
#include "core/hle/kernel/event.h"
|
2017-05-30 01:45:42 +02:00
|
|
|
#include "core/hle/kernel/handle_table.h"
|
2017-10-02 05:12:42 +02:00
|
|
|
#include "core/hle/kernel/ipc.h"
|
2015-07-29 17:08:00 +02:00
|
|
|
#include "core/hle/kernel/memory.h"
|
2014-05-21 04:57:54 +02:00
|
|
|
#include "core/hle/kernel/mutex.h"
|
2015-05-11 16:15:10 +02:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 22:25:15 +02:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2014-12-04 00:49:51 +01:00
|
|
|
#include "core/hle/kernel/semaphore.h"
|
2016-05-22 19:30:13 +02:00
|
|
|
#include "core/hle/kernel/server_port.h"
|
2016-12-01 04:50:13 +01:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
2017-06-21 02:27:20 +02:00
|
|
|
#include "core/hle/kernel/session.h"
|
2014-07-05 06:59:58 +02:00
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
2014-05-17 19:47:44 +02:00
|
|
|
#include "core/hle/kernel/thread.h"
|
2014-12-04 20:45:47 +01:00
|
|
|
#include "core/hle/kernel/timer.h"
|
2015-07-17 08:23:22 +02:00
|
|
|
#include "core/hle/kernel/vm_manager.h"
|
2017-05-30 00:45:30 +02:00
|
|
|
#include "core/hle/kernel/wait_object.h"
|
2017-07-21 06:52:50 +02:00
|
|
|
#include "core/hle/lock.h"
|
2014-10-23 05:20:01 +02:00
|
|
|
#include "core/hle/result.h"
|
2014-04-13 03:55:36 +02:00
|
|
|
#include "core/hle/service/service.h"
|
2014-04-11 01:58:28 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
namespace Kernel {
|
2014-04-11 05:26:12 +02:00
|
|
|
|
2014-04-25 05:56:06 +02:00
|
|
|
enum ControlMemoryOperation {
|
2016-09-18 02:38:01 +02:00
|
|
|
MEMOP_FREE = 1,
|
2015-07-18 04:19:16 +02:00
|
|
|
MEMOP_RESERVE = 2, // This operation seems to be unsupported in the kernel
|
2016-09-18 02:38:01 +02:00
|
|
|
MEMOP_COMMIT = 3,
|
|
|
|
MEMOP_MAP = 4,
|
|
|
|
MEMOP_UNMAP = 5,
|
2015-07-18 04:19:16 +02:00
|
|
|
MEMOP_PROTECT = 6,
|
|
|
|
MEMOP_OPERATION_MASK = 0xFF,
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
MEMOP_REGION_APP = 0x100,
|
2015-07-18 04:19:16 +02:00
|
|
|
MEMOP_REGION_SYSTEM = 0x200,
|
2016-09-18 02:38:01 +02:00
|
|
|
MEMOP_REGION_BASE = 0x300,
|
|
|
|
MEMOP_REGION_MASK = 0xF00,
|
2015-07-18 04:19:16 +02:00
|
|
|
|
|
|
|
MEMOP_LINEAR = 0x10000,
|
2014-04-25 05:56:06 +02:00
|
|
|
};
|
|
|
|
|
2014-04-18 05:05:31 +02:00
|
|
|
/// Map application or GSP heap memory
|
2016-09-18 02:38:01 +02:00
|
|
|
static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size,
|
|
|
|
u32 permissions) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Kernel_SVC,
|
2018-06-29 15:56:12 +02:00
|
|
|
"called operation=0x{:08X}, addr0=0x{:08X}, addr1=0x{:08X}, "
|
|
|
|
"size=0x{:X}, permissions=0x{:08X}",
|
|
|
|
operation, addr0, addr1, size, permissions);
|
2014-05-07 05:34:20 +02:00
|
|
|
|
2015-07-18 04:19:16 +02: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) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_WARNING(Kernel_SVC, "ControlMemory with specified region not supported, region={:X}",
|
2018-06-29 15:56:12 +02:00
|
|
|
region);
|
2015-07-18 04:19:16 +02:00
|
|
|
}
|
2014-04-18 05:05:31 +02:00
|
|
|
|
2015-07-18 04:19:16 +02:00
|
|
|
if ((permissions & (u32)MemoryPermission::ReadWrite) != permissions) {
|
|
|
|
return ERR_INVALID_COMBINATION;
|
|
|
|
}
|
|
|
|
VMAPermission vma_permissions = (VMAPermission)permissions;
|
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
auto& process = *Core::System::GetInstance().Kernel().GetCurrentProcess();
|
2015-07-18 04:19:16 +02:00
|
|
|
|
|
|
|
switch (operation & MEMOP_OPERATION_MASK) {
|
2016-09-18 02:38:01 +02: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 04:19:16 +02:00
|
|
|
if (addr0 >= Memory::HEAP_VADDR && addr0 < Memory::HEAP_VADDR_END) {
|
|
|
|
ResultCode result = process.HeapFree(addr0, size);
|
2016-09-18 02:38:01 +02:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-08-06 02:26:52 +02:00
|
|
|
} else if (addr0 >= process.GetLinearHeapBase() && addr0 < process.GetLinearHeapLimit()) {
|
2015-07-18 04:19:16 +02:00
|
|
|
ResultCode result = process.LinearFree(addr0, size);
|
2016-09-18 02:38:01 +02:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-07-18 04:19:16 +02:00
|
|
|
} else {
|
|
|
|
return ERR_INVALID_ADDRESS;
|
|
|
|
}
|
|
|
|
*out_addr = addr0;
|
2014-04-25 05:56:06 +02:00
|
|
|
break;
|
2015-07-18 04:19:16 +02:00
|
|
|
}
|
2014-04-25 05:56:06 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
case MEMOP_COMMIT: {
|
2015-07-18 04:19:16 +02: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 05:05:31 +02:00
|
|
|
break;
|
2015-07-18 04:19:16 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01: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 04:19:16 +02:00
|
|
|
|
2017-12-10 03:05:43 +01: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 04:19:16 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
case MEMOP_PROTECT: {
|
2015-07-18 04:19:16 +02:00
|
|
|
ResultCode result = process.vm_manager.ReprotectRange(addr0, size, vma_permissions);
|
2016-09-18 02:38:01 +02:00
|
|
|
if (result.IsError())
|
|
|
|
return result;
|
2015-07-18 04:19:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-04-18 05:05:31 +02:00
|
|
|
|
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown operation=0x{:08X}", operation);
|
2015-07-18 04:19:16 +02:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2014-04-18 05:05:31 +02:00
|
|
|
}
|
2015-07-18 04:19:16 +02:00
|
|
|
|
|
|
|
process.vm_manager.LogLayout(Log::Level::Trace);
|
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-04-25 05:56:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 16:58:09 +01:00
|
|
|
static void ExitProcess() {
|
2018-10-17 21:23:56 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->process_id);
|
2017-12-15 16:58:09 +01:00
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
ASSERT_MSG(current_process->status == ProcessStatus::Running, "Process has already exited");
|
2017-12-15 16:58:09 +01:00
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
current_process->status = ProcessStatus::Exited;
|
2017-12-15 16:58:09 +01:00
|
|
|
|
|
|
|
// Stop all the process threads that are currently waiting for objects.
|
|
|
|
auto& thread_list = GetThreadList();
|
|
|
|
for (auto& thread : thread_list) {
|
2018-10-17 21:23:56 +02:00
|
|
|
if (thread->owner_process != current_process)
|
2017-12-15 16:58:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (thread == GetCurrentThread())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(Subv): When are the other running/ready threads terminated?
|
2018-07-20 03:39:05 +02:00
|
|
|
ASSERT_MSG(thread->status == ThreadStatus::WaitSynchAny ||
|
|
|
|
thread->status == ThreadStatus::WaitSynchAll,
|
2017-12-15 16:58:09 +01:00
|
|
|
"Exiting processes with non-waiting threads is currently unimplemented");
|
|
|
|
|
|
|
|
thread->Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill the current thread
|
|
|
|
GetCurrentThread()->Stop();
|
|
|
|
|
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
}
|
|
|
|
|
2014-04-25 05:56:06 +02:00
|
|
|
/// Maps a memory block to specified address
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC,
|
2018-06-29 15:56:12 +02:00
|
|
|
"called memblock=0x{:08X}, addr=0x{:08X}, mypermissions=0x{:08X}, "
|
|
|
|
"otherpermission={}",
|
|
|
|
handle, addr, permissions, other_permissions);
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<SharedMemory> shared_memory =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<SharedMemory>(
|
|
|
|
handle);
|
2015-01-11 06:43:29 +01:00
|
|
|
if (shared_memory == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 06:43:29 +01:00
|
|
|
|
|
|
|
MemoryPermission permissions_type = static_cast<MemoryPermission>(permissions);
|
2014-07-05 16:22:03 +02:00
|
|
|
switch (permissions_type) {
|
2015-01-11 06:43:29 +01: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:
|
2018-10-17 21:23:56 +02:00
|
|
|
return shared_memory->Map(Core::System::GetInstance().Kernel().GetCurrentProcess().get(),
|
|
|
|
addr, permissions_type,
|
2016-09-18 02:38:01 +02:00
|
|
|
static_cast<MemoryPermission>(other_permissions));
|
2014-04-25 05:56:06 +02:00
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown permissions=0x{:08X}", permissions);
|
2014-04-25 05:56:06 +02:00
|
|
|
}
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2015-12-31 15:46:32 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode UnmapMemoryBlock(Handle handle, u32 addr) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called memblock=0x{:08X}, addr=0x{:08X}", handle, addr);
|
2015-12-31 15:46:32 +01:00
|
|
|
|
|
|
|
// TODO(Subv): Return E0A01BF5 if the address is not in the application's heap
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
SharedPtr<SharedMemory> shared_memory = current_process->handle_table.Get<SharedMemory>(handle);
|
2015-12-31 15:46:32 +01:00
|
|
|
if (shared_memory == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
return shared_memory->Unmap(current_process.get(), addr);
|
2014-04-18 05:05:31 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 03:55:36 +02:00
|
|
|
/// Connect to an OS service given the port name, returns the handle to the port to out
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ConnectToPort(Handle* out_handle, VAddr port_name_address) {
|
2017-10-04 19:05:13 +02:00
|
|
|
if (!Memory::IsValidVirtualAddress(port_name_address))
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_NOT_FOUND;
|
2017-10-04 19:05:13 +02:00
|
|
|
|
|
|
|
static constexpr std::size_t PortNameMaxLength = 11;
|
|
|
|
// 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)
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_PORT_NAME_TOO_LONG;
|
2014-06-02 02:48:29 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
|
2014-06-02 02:48:29 +02:00
|
|
|
|
2015-01-30 19:07:04 +01:00
|
|
|
auto it = Service::g_kernel_named_ports.find(port_name);
|
|
|
|
if (it == Service::g_kernel_named_ports.end()) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_NOT_FOUND;
|
2015-01-30 19:07:04 +01:00
|
|
|
}
|
2014-06-02 02:48:29 +02:00
|
|
|
|
2016-12-05 17:02:08 +01:00
|
|
|
auto client_port = it->second;
|
2016-06-15 01:03:30 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
SharedPtr<ClientSession> client_session;
|
2016-12-05 19:59:57 +01:00
|
|
|
CASCADE_RESULT(client_session, client_port->Connect());
|
2016-06-15 01:03:30 +02:00
|
|
|
|
|
|
|
// Return the client session
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle,
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Create(
|
|
|
|
client_session));
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-04-13 03:55:36 +02:00
|
|
|
}
|
|
|
|
|
2016-12-08 17:06:19 +01:00
|
|
|
/// Makes a blocking IPC call to an OS service.
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode SendSyncRequest(Handle handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<ClientSession> session =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<ClientSession>(
|
|
|
|
handle);
|
2014-12-14 06:30:11 +01:00
|
|
|
if (session == nullptr) {
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
2014-05-27 04:12:46 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
|
2014-05-27 04:12:46 +02:00
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
return session->SendSyncRequest(GetCurrentThread());
|
2014-04-11 01:58:28 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 02:41:33 +02:00
|
|
|
/// Close a handle
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CloseHandle(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
|
2018-10-20 03:04:18 +02:00
|
|
|
return Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Close(handle);
|
2014-04-17 02:41:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a handle to synchronize, timeout after the specified nanoseconds
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
|
2018-10-20 03:04:18 +02:00
|
|
|
auto object =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<WaitObject>(
|
|
|
|
handle);
|
2017-12-10 03:05:43 +01:00
|
|
|
Thread* thread = GetCurrentThread();
|
2015-06-08 05:39:37 +02:00
|
|
|
|
2014-12-21 13:04:08 +01:00
|
|
|
if (object == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-05-28 04:41:09 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({}:{}), nanoseconds={}", handle,
|
2018-06-29 15:56:12 +02:00
|
|
|
object->GetTypeName(), object->GetName(), nano_seconds);
|
2014-06-06 04:35:36 +02:00
|
|
|
|
2017-01-01 22:53:22 +01:00
|
|
|
if (object->ShouldWait(thread)) {
|
2015-01-18 19:25:51 +01:00
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
if (nano_seconds == 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2016-12-04 04:38:14 +01:00
|
|
|
|
2017-01-04 16:53:01 +01:00
|
|
|
thread->wait_objects = {object};
|
2015-06-08 05:39:37 +02:00
|
|
|
object->AddWaitingThread(thread);
|
2018-07-20 03:39:05 +02:00
|
|
|
thread->status = ThreadStatus::WaitSynchAny;
|
2015-01-18 19:25:51 +01:00
|
|
|
|
2015-01-10 17:34:50 +01:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2015-06-08 05:39:37 +02:00
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2015-01-17 08:03:44 +01:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->wakeup_callback = [](ThreadWakeupReason reason, SharedPtr<Thread> thread,
|
|
|
|
SharedPtr<WaitObject> object) {
|
2018-07-20 03:39:05 +02:00
|
|
|
ASSERT(thread->status == ThreadStatus::WaitSynchAny);
|
2017-09-28 18:53:32 +02:00
|
|
|
|
|
|
|
if (reason == ThreadWakeupReason::Timeout) {
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
|
2017-09-28 18:53:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(reason == ThreadWakeupReason::Signal);
|
|
|
|
thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
|
|
|
|
|
|
|
|
// WaitSynchronization1 doesn't have an output index like WaitSynchronizationN, so we
|
|
|
|
// don't have to do anything else here.
|
|
|
|
};
|
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2016-12-14 18:13:02 +01: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 04:38:14 +01:00
|
|
|
// Otherwise we retain the default value of timeout.
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2014-05-28 04:41:09 +02:00
|
|
|
}
|
|
|
|
|
2017-01-01 22:53:22 +01:00
|
|
|
object->Acquire(thread);
|
2015-01-22 00:27:01 +01:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-18 05:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for the given handles to synchronize, timeout after the specified nanoseconds
|
2017-10-04 18:40:40 +02:00
|
|
|
static ResultCode WaitSynchronizationN(s32* out, VAddr handles_address, s32 handle_count,
|
2016-12-17 07:20:47 +01:00
|
|
|
bool wait_all, s64 nano_seconds) {
|
2017-12-10 03:05:43 +01:00
|
|
|
Thread* thread = GetCurrentThread();
|
2014-05-23 01:32:45 +02:00
|
|
|
|
2017-10-04 18:40:40 +02:00
|
|
|
if (!Memory::IsValidVirtualAddress(handles_address))
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_POINTER;
|
2015-01-17 20:04:54 +01:00
|
|
|
|
2015-01-18 08:16:54 +01: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 02:16:47 +01:00
|
|
|
ASSERT_MSG(out != nullptr, "invalid output pointer specified!");
|
2015-01-18 08:16:54 +01:00
|
|
|
|
|
|
|
// Check if 'handle_count' is invalid
|
2015-01-17 18:35:55 +01:00
|
|
|
if (handle_count < 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_OUT_OF_RANGE;
|
2015-01-17 18:35:55 +01:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
using ObjectPtr = SharedPtr<WaitObject>;
|
2016-12-04 04:38:14 +01:00
|
|
|
std::vector<ObjectPtr> objects(handle_count);
|
|
|
|
|
|
|
|
for (int i = 0; i < handle_count; ++i) {
|
2017-12-10 03:05:43 +01:00
|
|
|
Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
|
2018-10-20 03:04:18 +02:00
|
|
|
auto object =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<WaitObject>(
|
|
|
|
handle);
|
2016-12-04 04:38:14 +01:00
|
|
|
if (object == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
objects[i] = object;
|
2014-05-28 04:41:09 +02:00
|
|
|
}
|
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
if (wait_all) {
|
2016-12-14 18:13:02 +01:00
|
|
|
bool all_available =
|
|
|
|
std::all_of(objects.begin(), objects.end(),
|
2017-01-01 22:53:22 +01:00
|
|
|
[thread](const ObjectPtr& object) { return !object->ShouldWait(thread); });
|
2016-12-08 16:34:53 +01:00
|
|
|
if (all_available) {
|
|
|
|
// We can acquire all objects right now, do so.
|
2016-12-10 19:29:31 +01:00
|
|
|
for (auto& object : objects)
|
2017-01-01 22:53:22 +01:00
|
|
|
object->Acquire(thread);
|
2016-12-14 18:13:02 +01:00
|
|
|
// Note: In this case, the `out` parameter is not set,
|
|
|
|
// and retains whatever value it had before.
|
2016-12-04 04:38:14 +01:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
// Not all objects were available right now, prepare to suspend the thread.
|
2015-02-10 04:05:39 +01:00
|
|
|
|
2016-12-14 18:13:02 +01:00
|
|
|
// If a timeout value of 0 was provided, just return the Timeout error code instead of
|
|
|
|
// suspending the thread.
|
2016-12-08 16:34:53 +01:00
|
|
|
if (nano_seconds == 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2015-01-18 19:25:51 +01:00
|
|
|
|
2016-12-04 04:38:14 +01:00
|
|
|
// Put the thread to sleep
|
2018-07-20 03:39:05 +02:00
|
|
|
thread->status = ThreadStatus::WaitSynchAll;
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2016-12-04 04:38:14 +01:00
|
|
|
// Add the thread to each of the objects' waiting threads.
|
2016-12-08 16:34:53 +01:00
|
|
|
for (auto& object : objects) {
|
2016-12-04 04:38:14 +01:00
|
|
|
object->AddWaitingThread(thread);
|
2015-01-18 19:25:51 +01:00
|
|
|
}
|
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
thread->wait_objects = std::move(objects);
|
2015-01-26 07:56:17 +01:00
|
|
|
|
2015-01-17 08:03:44 +01:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2016-12-04 04:38:14 +01:00
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2014-05-28 04:41:09 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->wakeup_callback = [](ThreadWakeupReason reason, SharedPtr<Thread> thread,
|
|
|
|
SharedPtr<WaitObject> object) {
|
2018-07-20 03:39:05 +02:00
|
|
|
ASSERT(thread->status == ThreadStatus::WaitSynchAll);
|
2017-09-28 18:53:32 +02:00
|
|
|
|
|
|
|
if (reason == ThreadWakeupReason::Timeout) {
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
|
2017-09-28 18:53:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(reason == ThreadWakeupReason::Signal);
|
|
|
|
|
|
|
|
thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
|
|
|
|
// The wait_all case does not update the output index.
|
|
|
|
};
|
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
// This value gets set to -1 by default in this case, it is not modified after this.
|
2016-12-04 04:38:14 +01:00
|
|
|
*out = -1;
|
2016-12-14 18:13:02 +01: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.
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2016-12-04 04:38:14 +01:00
|
|
|
} else {
|
2016-12-08 16:34:53 +01:00
|
|
|
// Find the first object that is acquirable in the provided list of objects
|
2017-01-02 19:53:10 +01:00
|
|
|
auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
|
|
|
|
return !object->ShouldWait(thread);
|
|
|
|
});
|
2015-01-17 08:03:44 +01:00
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
if (itr != objects.end()) {
|
|
|
|
// We found a ready object, acquire it and set the result value
|
2017-12-10 03:05:43 +01:00
|
|
|
WaitObject* object = itr->get();
|
2017-01-01 22:53:22 +01:00
|
|
|
object->Acquire(thread);
|
2017-09-27 01:26:09 +02:00
|
|
|
*out = static_cast<s32>(std::distance(objects.begin(), itr));
|
2016-12-04 04:38:14 +01:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2014-05-28 04:41:09 +02:00
|
|
|
|
2016-12-08 16:34:53 +01:00
|
|
|
// No objects were ready to be acquired, prepare to suspend the thread.
|
2015-01-17 08:03:44 +01:00
|
|
|
|
2016-12-14 18:13:02 +01:00
|
|
|
// If a timeout value of 0 was provided, just return the Timeout error code instead of
|
|
|
|
// suspending the thread.
|
2016-12-08 16:34:53 +01:00
|
|
|
if (nano_seconds == 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2016-12-04 04:38:14 +01:00
|
|
|
|
|
|
|
// Put the thread to sleep
|
2018-07-20 03:39:05 +02:00
|
|
|
thread->status = ThreadStatus::WaitSynchAny;
|
2015-01-17 08:03:44 +01:00
|
|
|
|
2016-12-04 04:38:14 +01:00
|
|
|
// Add the thread to each of the objects' waiting threads.
|
2018-09-06 22:03:28 +02:00
|
|
|
for (std::size_t i = 0; i < objects.size(); ++i) {
|
2017-12-10 03:05:43 +01:00
|
|
|
WaitObject* object = objects[i].get();
|
2016-12-04 04:38:14 +01:00
|
|
|
object->AddWaitingThread(thread);
|
2015-01-17 08:03:44 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:53:01 +01:00
|
|
|
thread->wait_objects = std::move(objects);
|
|
|
|
|
2016-12-14 18:13:02 +01:00
|
|
|
// Note: If no handles and no timeout were given, then the thread will deadlock, this is
|
|
|
|
// consistent with hardware behavior.
|
2014-05-28 04:41:09 +02:00
|
|
|
|
2016-12-04 04:38:14 +01:00
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
|
|
|
thread->WakeAfterDelay(nano_seconds);
|
2014-05-28 04:41:09 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->wakeup_callback = [](ThreadWakeupReason reason, SharedPtr<Thread> thread,
|
|
|
|
SharedPtr<WaitObject> object) {
|
2018-07-20 03:39:05 +02:00
|
|
|
ASSERT(thread->status == ThreadStatus::WaitSynchAny);
|
2017-09-28 18:53:32 +02:00
|
|
|
|
|
|
|
if (reason == ThreadWakeupReason::Timeout) {
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
|
2017-09-28 18:53:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(reason == ThreadWakeupReason::Signal);
|
|
|
|
|
|
|
|
thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
|
|
|
|
thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(object.get()));
|
|
|
|
};
|
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2016-12-14 18:13:02 +01: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 16:34:53 +01:00
|
|
|
// Otherwise we retain the default value of timeout, and -1 in the out parameter
|
|
|
|
*out = -1;
|
2017-12-10 03:05:43 +01:00
|
|
|
return RESULT_TIMEOUT;
|
2016-12-04 04:38:14 +01:00
|
|
|
}
|
2014-04-17 02:41:33 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ReceiveIPCRequest(SharedPtr<ServerSession> server_session,
|
|
|
|
SharedPtr<Thread> thread) {
|
2017-10-02 05:12:42 +02:00
|
|
|
if (server_session->parent->client == nullptr) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_SESSION_CLOSED_BY_REMOTE;
|
2017-10-02 05:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VAddr target_address = thread->GetCommandBufferAddress();
|
|
|
|
VAddr source_address = server_session->currently_handling->GetCommandBufferAddress();
|
|
|
|
|
2017-11-07 20:35:17 +01:00
|
|
|
ResultCode translation_result = TranslateCommandBuffer(
|
|
|
|
server_session->currently_handling, thread, source_address, target_address, false);
|
2017-10-02 05:12:42 +02:00
|
|
|
|
|
|
|
// If a translation error occurred, immediately resume the client thread.
|
|
|
|
if (translation_result.IsError()) {
|
|
|
|
// Set the output of SendSyncRequest in the client thread to the translation output.
|
|
|
|
server_session->currently_handling->SetWaitSynchronizationResult(translation_result);
|
|
|
|
|
|
|
|
server_session->currently_handling->ResumeFromWait();
|
|
|
|
server_session->currently_handling = nullptr;
|
|
|
|
|
|
|
|
// TODO(Subv): This path should try to wait again on the same objects.
|
|
|
|
ASSERT_MSG(false, "ReplyAndReceive translation error behavior unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
return translation_result;
|
|
|
|
}
|
|
|
|
|
2017-06-21 02:27:20 +02:00
|
|
|
/// In a single operation, sends a IPC reply and waits for a new request.
|
2017-10-04 18:49:29 +02:00
|
|
|
static ResultCode ReplyAndReceive(s32* index, VAddr handles_address, s32 handle_count,
|
2017-12-10 03:05:43 +01:00
|
|
|
Handle reply_target) {
|
2017-10-04 18:49:29 +02:00
|
|
|
if (!Memory::IsValidVirtualAddress(handles_address))
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_POINTER;
|
2017-06-21 02:27:20 +02:00
|
|
|
|
|
|
|
// Check if 'handle_count' is invalid
|
|
|
|
if (handle_count < 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_OUT_OF_RANGE;
|
2017-06-21 02:27:20 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
using ObjectPtr = SharedPtr<WaitObject>;
|
2017-06-21 02:27:20 +02:00
|
|
|
std::vector<ObjectPtr> objects(handle_count);
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
|
2017-06-21 02:27:20 +02:00
|
|
|
for (int i = 0; i < handle_count; ++i) {
|
2017-12-10 03:05:43 +01:00
|
|
|
Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
|
2018-10-20 03:04:18 +02:00
|
|
|
auto object = current_process->handle_table.Get<WaitObject>(handle);
|
2017-06-21 02:27:20 +02:00
|
|
|
if (object == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
objects[i] = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are also sending a command reply.
|
|
|
|
// Do not send a reply if the command id in the command buffer is 0xFFFF.
|
2017-12-10 03:05:43 +01:00
|
|
|
u32* cmd_buff = GetCommandBuffer();
|
2017-06-21 02:27:20 +02:00
|
|
|
IPC::Header header{cmd_buff[0]};
|
|
|
|
if (reply_target != 0 && header.command_id != 0xFFFF) {
|
2018-10-20 03:04:18 +02:00
|
|
|
auto session = current_process->handle_table.Get<ServerSession>(reply_target);
|
2017-06-21 02:27:20 +02:00
|
|
|
if (session == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
auto request_thread = std::move(session->currently_handling);
|
|
|
|
|
|
|
|
// Mark the request as "handled".
|
|
|
|
session->currently_handling = nullptr;
|
|
|
|
|
|
|
|
// Error out if there's no request thread or the session was closed.
|
|
|
|
// TODO(Subv): Is the same error code (ClosedByRemote) returned for both of these cases?
|
|
|
|
if (request_thread == nullptr || session->parent->client == nullptr) {
|
|
|
|
*index = -1;
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_SESSION_CLOSED_BY_REMOTE;
|
2017-06-21 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
VAddr source_address = GetCurrentThread()->GetCommandBufferAddress();
|
2017-10-02 05:12:42 +02:00
|
|
|
VAddr target_address = request_thread->GetCommandBufferAddress();
|
|
|
|
|
2017-11-07 20:35:17 +01:00
|
|
|
ResultCode translation_result = TranslateCommandBuffer(
|
|
|
|
Kernel::GetCurrentThread(), request_thread, source_address, target_address, true);
|
2017-10-02 05:12:42 +02:00
|
|
|
|
|
|
|
// Note: The real kernel seems to always panic if the Server->Client buffer translation
|
|
|
|
// fails for whatever reason.
|
|
|
|
ASSERT(translation_result.IsSuccess());
|
2017-06-21 02:27:20 +02:00
|
|
|
|
|
|
|
// Note: The scheduler is not invoked here.
|
|
|
|
request_thread->ResumeFromWait();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle_count == 0) {
|
|
|
|
*index = 0;
|
|
|
|
// The kernel uses this value as a placeholder for the real error, and returns it when we
|
|
|
|
// pass no handles and do not perform any reply.
|
|
|
|
if (reply_target == 0 || header.command_id == 0xFFFF)
|
|
|
|
return ResultCode(0xE7E3FFFF);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
auto thread = GetCurrentThread();
|
2017-06-21 02:27:20 +02:00
|
|
|
|
|
|
|
// Find the first object that is acquirable in the provided list of objects
|
|
|
|
auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
|
|
|
|
return !object->ShouldWait(thread);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (itr != objects.end()) {
|
|
|
|
// We found a ready object, acquire it and set the result value
|
2017-12-10 03:05:43 +01:00
|
|
|
WaitObject* object = itr->get();
|
2017-06-21 02:27:20 +02:00
|
|
|
object->Acquire(thread);
|
2017-09-27 01:26:09 +02:00
|
|
|
*index = static_cast<s32>(std::distance(objects.begin(), itr));
|
2017-06-21 02:27:20 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
if (object->GetHandleType() != HandleType::ServerSession)
|
2017-10-02 05:12:42 +02:00
|
|
|
return RESULT_SUCCESS;
|
2017-06-21 02:27:20 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
auto server_session = static_cast<ServerSession*>(object);
|
|
|
|
return ReceiveIPCRequest(server_session, GetCurrentThread());
|
2017-06-21 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No objects were ready to be acquired, prepare to suspend the thread.
|
|
|
|
|
|
|
|
// Put the thread to sleep
|
2018-07-20 03:39:05 +02:00
|
|
|
thread->status = ThreadStatus::WaitSynchAny;
|
2017-06-21 02:27:20 +02:00
|
|
|
|
|
|
|
// Add the thread to each of the objects' waiting threads.
|
2018-09-06 22:03:28 +02:00
|
|
|
for (std::size_t i = 0; i < objects.size(); ++i) {
|
2017-12-10 03:05:43 +01:00
|
|
|
WaitObject* object = objects[i].get();
|
2017-06-21 02:27:20 +02:00
|
|
|
object->AddWaitingThread(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->wait_objects = std::move(objects);
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
thread->wakeup_callback = [](ThreadWakeupReason reason, SharedPtr<Thread> thread,
|
|
|
|
SharedPtr<WaitObject> object) {
|
2018-07-20 03:39:05 +02:00
|
|
|
ASSERT(thread->status == ThreadStatus::WaitSynchAny);
|
2017-09-28 18:53:32 +02:00
|
|
|
ASSERT(reason == ThreadWakeupReason::Signal);
|
|
|
|
|
2017-10-02 05:12:42 +02:00
|
|
|
ResultCode result = RESULT_SUCCESS;
|
2017-09-28 18:53:32 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
if (object->GetHandleType() == HandleType::ServerSession) {
|
|
|
|
auto server_session = DynamicObjectCast<ServerSession>(object);
|
2017-10-02 05:12:42 +02:00
|
|
|
result = ReceiveIPCRequest(server_session, thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->SetWaitSynchronizationResult(result);
|
|
|
|
thread->SetWaitSynchronizationOutput(thread->GetWaitObjectIndex(object.get()));
|
2017-09-28 18:53:32 +02:00
|
|
|
};
|
|
|
|
|
2017-06-21 02:27:20 +02:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
|
|
|
// 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, or to 0xC8A01836 if there was a translation error.
|
|
|
|
// By default the index is set to -1.
|
|
|
|
*index = -1;
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-05-02 00:50:36 +02:00
|
|
|
/// Create an address arbiter (to allocate access to shared resources)
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateAddressArbiter(Handle* out_handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
SharedPtr<AddressArbiter> arbiter = kernel.CreateAddressArbiter();
|
|
|
|
CASCADE_RESULT(*out_handle,
|
|
|
|
kernel.GetCurrentProcess()->handle_table.Create(std::move(arbiter)));
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "returned handle=0x{:08X}", *out_handle);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-02 00:50:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 02:24:51 +02:00
|
|
|
/// Arbitrate address
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ArbitrateAddress(Handle handle, u32 address, u32 type, u32 value,
|
2016-09-18 02:38:01 +02:00
|
|
|
s64 nanoseconds) {
|
2018-06-29 15:56:12 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}, address=0x{:08X}, type=0x{:08X}, value=0x{:08X}",
|
|
|
|
handle, address, type, value);
|
2015-01-11 17:46:49 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<AddressArbiter> arbiter =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<AddressArbiter>(
|
|
|
|
handle);
|
2015-01-11 17:46:49 +01:00
|
|
|
if (arbiter == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 17:46:49 +01:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
auto res = arbiter->ArbitrateAddress(GetCurrentThread(), static_cast<ArbitrationType>(type),
|
|
|
|
address, value, nanoseconds);
|
2015-04-07 03:58:05 +02:00
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
// TODO(Subv): Identify in which specific cases this call should cause a reschedule.
|
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2015-04-07 03:58:05 +02:00
|
|
|
return res;
|
2014-05-30 02:24:51 +02:00
|
|
|
}
|
|
|
|
|
2015-05-17 07:06:59 +02:00
|
|
|
static void Break(u8 break_reason) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
|
2015-05-17 07:06:59 +02:00
|
|
|
std::string reason_str;
|
|
|
|
switch (break_reason) {
|
2016-09-18 02:38:01 +02: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 07:06:59 +02:00
|
|
|
}
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_CRITICAL(Debug_Emulated, "Break reason: {}", reason_str);
|
2015-05-17 07:06:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 00:50:36 +02:00
|
|
|
/// Used to output a message on a debug hardware unit - does nothing on a retail unit
|
2017-10-04 18:52:39 +02:00
|
|
|
static void OutputDebugString(VAddr address, int len) {
|
2018-09-18 01:49:54 +02:00
|
|
|
if (len <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:12:54 +02:00
|
|
|
std::string string(len, ' ');
|
2017-10-04 18:52:39 +02:00
|
|
|
Memory::ReadBlock(address, string.data(), len);
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Debug_Emulated, "{}", string);
|
2014-05-07 05:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetResourceLimit(Handle* resource_limit, Handle process_handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
SharedPtr<Process> process = current_process->handle_table.Get<Process>(process_handle);
|
2015-05-12 22:25:15 +02:00
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*resource_limit, current_process->handle_table.Create(process->resource_limit));
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-07 05:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit current values
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetResourceLimitCurrentValues(VAddr values, Handle resource_limit_handle,
|
2017-10-04 19:23:08 +02:00
|
|
|
VAddr names, u32 name_count) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called resource_limit={:08X}, names={:08X}, name_count={}",
|
2018-06-29 15:56:12 +02:00
|
|
|
resource_limit_handle, names, name_count);
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
SharedPtr<ResourceLimit> resource_limit =
|
2018-10-20 03:04:18 +02:00
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<ResourceLimit>(
|
|
|
|
resource_limit_handle);
|
2015-05-12 22:25:15 +02:00
|
|
|
if (resource_limit == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2017-10-04 19:23:08 +02:00
|
|
|
for (unsigned int i = 0; i < name_count; ++i) {
|
|
|
|
u32 name = Memory::Read32(names + i * sizeof(u32));
|
|
|
|
s64 value = resource_limit->GetCurrentResourceValue(name);
|
|
|
|
Memory::Write64(values + i * sizeof(u64), value);
|
|
|
|
}
|
2015-05-12 22:25:15 +02:00
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get resource limit max values
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetResourceLimitLimitValues(VAddr values, Handle resource_limit_handle,
|
2017-10-04 19:23:08 +02:00
|
|
|
VAddr names, u32 name_count) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called resource_limit={:08X}, names={:08X}, name_count={}",
|
2018-06-29 15:56:12 +02:00
|
|
|
resource_limit_handle, names, name_count);
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
SharedPtr<ResourceLimit> resource_limit =
|
2018-10-20 03:04:18 +02:00
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<ResourceLimit>(
|
|
|
|
resource_limit_handle);
|
2015-05-12 22:25:15 +02:00
|
|
|
if (resource_limit == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2017-10-04 19:23:08 +02:00
|
|
|
for (unsigned int i = 0; i < name_count; ++i) {
|
|
|
|
u32 name = Memory::Read32(names + i * sizeof(u32));
|
2017-12-30 22:42:08 +01:00
|
|
|
s64 value = resource_limit->GetMaxResourceValue(name);
|
2017-10-04 19:23:08 +02:00
|
|
|
Memory::Write64(values + i * sizeof(u64), value);
|
|
|
|
}
|
2015-05-12 22:25:15 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-02 00:50:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 04:57:54 +02:00
|
|
|
/// Creates a new thread
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateThread(Handle* out_handle, u32 priority, u32 entry_point, u32 arg,
|
2016-09-18 02:38:01 +02:00
|
|
|
u32 stack_top, s32 processor_id) {
|
2018-09-08 14:40:24 +02:00
|
|
|
std::string name = fmt::format("thread-{:08X}", entry_point);
|
2014-05-17 19:47:44 +02:00
|
|
|
|
2018-09-21 16:39:10 +02:00
|
|
|
if (priority > ThreadPrioLowest) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_OUT_OF_RANGE;
|
2015-03-24 04:55:21 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
|
|
|
|
SharedPtr<ResourceLimit>& resource_limit = current_process->resource_limit;
|
2017-12-10 03:05:43 +01:00
|
|
|
if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
|
|
|
|
return ERR_NOT_AUTHORIZED;
|
2017-01-11 19:20:14 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:39:10 +02:00
|
|
|
if (processor_id == ThreadProcessorIdDefault) {
|
2017-11-06 19:47:48 +01:00
|
|
|
// Set the target CPU to the one specified in the process' exheader.
|
2018-10-17 21:23:56 +02:00
|
|
|
processor_id = current_process->ideal_processor;
|
2018-09-21 16:39:10 +02:00
|
|
|
ASSERT(processor_id != ThreadProcessorIdDefault);
|
2017-11-06 19:47:48 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 04:55:18 +02:00
|
|
|
switch (processor_id) {
|
2018-09-21 16:39:10 +02:00
|
|
|
case ThreadProcessorId0:
|
2017-11-06 19:47:48 +01:00
|
|
|
break;
|
2018-09-21 16:39:10 +02:00
|
|
|
case ThreadProcessorIdAll:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_INFO(Kernel_SVC,
|
2018-06-29 15:56:12 +02:00
|
|
|
"Newly created thread is allowed to be run in any Core, unimplemented.");
|
2017-11-06 19:47:48 +01:00
|
|
|
break;
|
2018-09-21 16:39:10 +02:00
|
|
|
case ThreadProcessorId1:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC,
|
2018-06-29 15:56:12 +02:00
|
|
|
"Newly created thread must run in the SysCore (Core1), unimplemented.");
|
2015-04-10 04:55:18 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO(bunnei): Implement support for other processor IDs
|
2018-03-27 17:28:42 +02:00
|
|
|
ASSERT_MSG(false, "Unsupported thread processor ID: {}", processor_id);
|
2015-04-10 04:55:18 +02:00
|
|
|
break;
|
2015-03-24 04:55:21 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 01:54:06 +02:00
|
|
|
CASCADE_RESULT(SharedPtr<Thread> thread, Core::System::GetInstance().Kernel().CreateThread(
|
|
|
|
name, entry_point, priority, arg, processor_id,
|
2018-10-26 15:37:46 +02:00
|
|
|
stack_top, *current_process));
|
2016-05-14 15:45:04 +02:00
|
|
|
|
2017-12-12 20:12:03 +01:00
|
|
|
thread->context->SetFpscr(FPSCR_DEFAULT_NAN | FPSCR_FLUSH_TO_ZERO |
|
|
|
|
FPSCR_ROUND_TOZERO); // 0x03C00000
|
2016-05-14 15:45:04 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle, current_process->handle_table.Create(std::move(thread)));
|
2014-05-22 03:41:40 +02:00
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC,
|
2018-06-29 15:56:12 +02:00
|
|
|
"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);
|
2014-11-19 09:49:13 +01:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-14 03:58:26 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 04:35:36 +02:00
|
|
|
/// Called when a thread exits
|
2014-12-22 14:07:22 +01:00
|
|
|
static void ExitThread() {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", Core::CPU().GetPC());
|
2014-06-06 04:35:36 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
ExitCurrentThread();
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
2014-06-06 04:35:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 04:12:54 +02:00
|
|
|
/// Gets the priority for the specified thread
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetThreadPriority(u32* priority, Handle handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
const SharedPtr<Thread> thread =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Thread>(handle);
|
2014-12-22 14:07:22 +01:00
|
|
|
if (thread == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 14:07:22 +01:00
|
|
|
|
|
|
|
*priority = thread->GetPriority();
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-02 04:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the priority for the specified thread
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode SetThreadPriority(Handle handle, u32 priority) {
|
2018-09-21 16:39:10 +02:00
|
|
|
if (priority > ThreadPrioLowest) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_OUT_OF_RANGE;
|
2017-01-11 18:08:10 +01:00
|
|
|
}
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Thread> thread =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Thread>(handle);
|
2014-12-22 14:07:22 +01:00
|
|
|
if (thread == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 14:07:22 +01:00
|
|
|
|
2017-01-11 18:08:10 +01:00
|
|
|
// Note: The kernel uses the current process's resource limit instead of
|
|
|
|
// the one from the thread owner's resource limit.
|
2018-10-17 21:23:56 +02:00
|
|
|
SharedPtr<ResourceLimit>& resource_limit =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->resource_limit;
|
2017-12-10 03:05:43 +01:00
|
|
|
if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
|
|
|
|
return ERR_NOT_AUTHORIZED;
|
2017-01-11 18:08:10 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 14:07:22 +01:00
|
|
|
thread->SetPriority(priority);
|
2017-01-03 01:38:08 +01:00
|
|
|
thread->UpdatePriority();
|
|
|
|
|
|
|
|
// Update the mutexes that this thread is waiting for
|
|
|
|
for (auto& mutex : thread->pending_mutexes)
|
|
|
|
mutex->UpdatePriority();
|
|
|
|
|
2017-01-01 17:57:02 +01:00
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-02 04:12:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 04:57:54 +02:00
|
|
|
/// Create a mutex
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateMutex(Handle* out_handle, u32 initial_locked) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
SharedPtr<Mutex> mutex = kernel.CreateMutex(initial_locked != 0);
|
2018-08-29 16:30:33 +02:00
|
|
|
mutex->name = fmt::format("mutex-{:08x}", Core::CPU().GetReg(14));
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle, kernel.GetCurrentProcess()->handle_table.Create(std::move(mutex)));
|
2015-01-23 02:12:19 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called initial_locked={} : created handle=0x{:08X}",
|
2018-06-29 15:56:12 +02:00
|
|
|
initial_locked ? "true" : "false", *out_handle);
|
2015-05-20 02:24:30 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-14 03:58:26 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 04:57:54 +02:00
|
|
|
/// Release a mutex
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ReleaseMutex(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}", handle);
|
2015-01-23 02:12:19 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Mutex> mutex =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Mutex>(handle);
|
2015-01-23 02:12:19 +01:00
|
|
|
if (mutex == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 02:12:19 +01:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
return mutex->Release(GetCurrentThread());
|
2014-05-14 03:58:26 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:15:10 +02:00
|
|
|
/// Get the ID of the specified process
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
|
2015-05-11 16:15:10 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
const SharedPtr<Process> process =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Process>(
|
|
|
|
process_handle);
|
2015-05-11 16:15:10 +02:00
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2015-05-12 01:23:45 +02:00
|
|
|
*process_id = process->process_id;
|
2015-05-11 16:15:10 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the ID of the process that owns the specified thread
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetProcessIdOfThread(u32* process_id, Handle thread_handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
|
2015-05-11 16:15:10 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
const SharedPtr<Thread> thread =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Thread>(
|
|
|
|
thread_handle);
|
2015-05-11 16:15:10 +02:00
|
|
|
if (thread == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
const SharedPtr<Process> process = thread->owner_process;
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2018-03-27 17:37:36 +02:00
|
|
|
ASSERT_MSG(process != nullptr, "Invalid parent process for thread={:#010X}", thread_handle);
|
2015-05-11 16:15:10 +02:00
|
|
|
|
|
|
|
*process_id = process->process_id;
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-12-04 05:22:06 +01:00
|
|
|
/// Get the ID for the specified thread.
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetThreadId(u32* thread_id, Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", handle);
|
2014-12-22 14:07:22 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
const SharedPtr<Thread> thread =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Thread>(handle);
|
2014-12-22 14:07:22 +01:00
|
|
|
if (thread == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2014-12-22 14:07:22 +01:00
|
|
|
|
|
|
|
*thread_id = thread->GetThreadId();
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-16 02:17:30 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 00:49:51 +01:00
|
|
|
/// Creates a semaphore
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateSemaphore(Handle* out_handle, s32 initial_count, s32 max_count) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
2018-10-12 22:12:08 +02:00
|
|
|
CASCADE_RESULT(SharedPtr<Semaphore> semaphore,
|
2018-10-20 03:04:18 +02:00
|
|
|
kernel.CreateSemaphore(initial_count, max_count));
|
2018-08-29 16:30:33 +02:00
|
|
|
semaphore->name = fmt::format("semaphore-{:08x}", Core::CPU().GetReg(14));
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle,
|
|
|
|
kernel.GetCurrentProcess()->handle_table.Create(std::move(semaphore)));
|
2015-01-11 16:53:11 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called initial_count={}, max_count={}, created handle=0x{:08X}",
|
2018-06-29 15:56:12 +02:00
|
|
|
initial_count, max_count, *out_handle);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 17:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Releases a certain number of slots in a semaphore
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called release_count={}, handle=0x{:08X}", release_count, handle);
|
2015-01-11 16:53:11 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Semaphore> semaphore =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Semaphore>(
|
|
|
|
handle);
|
2015-01-11 16:53:11 +01:00
|
|
|
if (semaphore == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-11 16:53:11 +01:00
|
|
|
|
2015-01-23 07:00:39 +01:00
|
|
|
CASCADE_RESULT(*count, semaphore->Release(release_count));
|
2015-04-07 03:58:05 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 00:49:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-17 21:45:12 +02:00
|
|
|
/// Query process memory
|
2016-09-18 02:38:01 +02:00
|
|
|
static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* page_info,
|
2017-12-10 03:05:43 +01:00
|
|
|
Handle process_handle, u32 addr) {
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> process =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Process>(
|
|
|
|
process_handle);
|
2015-07-17 21:45:12 +02:00
|
|
|
if (process == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
2015-07-18 04:19:16 +02:00
|
|
|
auto vma = process->vm_manager.FindVMA(addr);
|
2015-07-17 08:23:22 +02:00
|
|
|
|
2018-10-22 04:00:06 +02:00
|
|
|
if (vma == process->vm_manager.vma_map.end())
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_ADDRESS;
|
2015-07-17 08:23:22 +02: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;
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr=0x{:08X}", process_handle, addr);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-16 02:17:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 21:45:12 +02:00
|
|
|
/// Query memory
|
|
|
|
static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, u32 addr) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
|
2015-07-17 21:45:12 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 04:57:54 +02:00
|
|
|
/// Create an event
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateEvent(Handle* out_handle, u32 reset_type) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
SharedPtr<Event> evt = kernel.CreateEvent(static_cast<ResetType>(reset_type),
|
|
|
|
fmt::format("event-{:08x}", Core::CPU().GetReg(14)));
|
|
|
|
CASCADE_RESULT(*out_handle, kernel.GetCurrentProcess()->handle_table.Create(std::move(evt)));
|
2015-01-23 07:00:39 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called reset_type=0x{:08X} : created handle=0x{:08X}", reset_type,
|
2018-06-29 15:56:12 +02:00
|
|
|
*out_handle);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-18 23:52:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-27 02:55:48 +02:00
|
|
|
/// Duplicates a kernel handle
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode DuplicateHandle(Handle* out, Handle handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(
|
|
|
|
*out,
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Duplicate(handle));
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "duplicated 0x{:08X} to 0x{:08X}", handle, *out);
|
2015-01-31 19:12:54 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-27 02:55:48 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 04:35:36 +02:00
|
|
|
/// Signals an event
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode SignalEvent(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called event=0x{:08X}", handle);
|
2015-01-23 06:11:25 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Event> evt =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Event>(handle);
|
2015-01-23 06:11:25 +01:00
|
|
|
if (evt == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 06:11:25 +01:00
|
|
|
|
|
|
|
evt->Signal();
|
2015-05-20 02:24:30 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-06-06 04:35:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 02:24:51 +02:00
|
|
|
/// Clears an event
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ClearEvent(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called event=0x{:08X}", handle);
|
2015-01-23 06:11:25 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Event> evt =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Event>(handle);
|
2015-01-23 06:11:25 +01:00
|
|
|
if (evt == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 06:11:25 +01:00
|
|
|
|
|
|
|
evt->Clear();
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-05-30 02:24:51 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:47 +01:00
|
|
|
/// Creates a timer
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateTimer(Handle* out_handle, u32 reset_type) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
SharedPtr<Timer> timer = kernel.CreateTimer(
|
2018-10-12 22:26:23 +02:00
|
|
|
static_cast<ResetType>(reset_type), fmt ::format("timer-{:08x}", Core::CPU().GetReg(14)));
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle, kernel.GetCurrentProcess()->handle_table.Create(std::move(timer)));
|
2015-01-23 05:19:33 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called reset_type=0x{:08X} : created handle=0x{:08X}", reset_type,
|
2018-06-29 15:56:12 +02:00
|
|
|
*out_handle);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears a timer
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode ClearTimer(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x{:08X}", handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Timer> timer =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Timer>(handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
if (timer == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 05:19:33 +01:00
|
|
|
|
|
|
|
timer->Clear();
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a timer
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode SetTimer(Handle handle, s64 initial, s64 interval) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x{:08X}", handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
|
2017-01-09 19:02:44 +01:00
|
|
|
if (initial < 0 || interval < 0) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_OUT_OF_RANGE_KERNEL;
|
2017-01-09 19:02:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Timer> timer =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Timer>(handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
if (timer == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 05:19:33 +01:00
|
|
|
|
|
|
|
timer->Set(initial, interval);
|
2015-04-07 03:58:05 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancels a timer
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CancelTimer(Handle handle) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called timer=0x{:08X}", handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Timer> timer =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Timer>(handle);
|
2015-01-23 05:19:33 +01:00
|
|
|
if (timer == nullptr)
|
2015-01-23 06:44:52 +01:00
|
|
|
return ERR_INVALID_HANDLE;
|
2015-01-23 05:19:33 +01:00
|
|
|
|
|
|
|
timer->Cancel();
|
2015-04-07 03:58:05 +02:00
|
|
|
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-04 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
2014-06-01 16:37:19 +02:00
|
|
|
/// Sleep the current thread
|
2014-11-17 04:58:39 +01:00
|
|
|
static void SleepThread(s64 nanoseconds) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds);
|
2014-11-26 06:34:14 +01:00
|
|
|
|
2017-01-05 20:14:22 +01:00
|
|
|
// Don't attempt to yield execution if there are no available threads to run,
|
|
|
|
// this way we avoid a useless reschedule to the idle thread.
|
2017-12-10 03:05:43 +01:00
|
|
|
if (nanoseconds == 0 && !HaveReadyThreads())
|
2017-01-05 20:14:22 +01:00
|
|
|
return;
|
|
|
|
|
2014-12-20 08:32:19 +01:00
|
|
|
// Sleep current thread and check for next thread to schedule
|
2017-12-10 03:05:43 +01:00
|
|
|
WaitCurrentThread_Sleep();
|
2015-01-07 22:40:08 +01:00
|
|
|
|
|
|
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
2017-12-10 03:05:43 +01:00
|
|
|
GetCurrentThread()->WakeAfterDelay(nanoseconds);
|
2017-01-01 17:57:02 +01:00
|
|
|
|
|
|
|
Core::System::GetInstance().PrepareReschedule();
|
2014-06-01 16:37:19 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:01:02 +02:00
|
|
|
/// This returns the total CPU ticks elapsed since the CPU was powered-on
|
2014-11-17 04:58:39 +01:00
|
|
|
static s64 GetSystemTick() {
|
2015-08-26 04:47:29 +02:00
|
|
|
s64 result = CoreTiming::GetTicks();
|
|
|
|
// Advance time to defeat dumb games (like Cubic Ninja) that busy-wait for the frame to end.
|
2017-09-30 18:25:49 +02:00
|
|
|
CoreTiming::AddTicks(150); // Measured time between two calls on a 9.2 o3DS with Ninjhax 1.1b
|
2015-08-26 04:47:29 +02:00
|
|
|
return result;
|
2014-08-15 02:01:02 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 05:52:27 +01:00
|
|
|
/// Creates a memory block at the specified address with the specified permissions and size
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreateMemoryBlock(Handle* out_handle, u32 addr, u32 size, u32 my_permission,
|
|
|
|
u32 other_permission) {
|
2015-12-31 15:46:32 +01:00
|
|
|
if (size % Memory::PAGE_SIZE != 0)
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_MISALIGNED_SIZE;
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
SharedPtr<SharedMemory> shared_memory = nullptr;
|
2014-12-09 05:52:27 +01:00
|
|
|
|
2016-04-18 04:07:52 +02: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 04:58:51 +02:00
|
|
|
case MemoryPermission::DontCare:
|
2016-04-18 04:07:52 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!VerifyPermissions(static_cast<MemoryPermission>(my_permission)) ||
|
|
|
|
!VerifyPermissions(static_cast<MemoryPermission>(other_permission)))
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2016-04-18 04:07:52 +02:00
|
|
|
|
2017-01-06 14:21:22 +01:00
|
|
|
// TODO(Subv): Processes with memory type APPLICATION are not allowed
|
|
|
|
// to create memory blocks with addr = 0, any attempts to do so
|
|
|
|
// should return error 0xD92007EA.
|
|
|
|
if ((addr < Memory::PROCESS_IMAGE_VADDR || addr + size > Memory::SHARED_MEMORY_VADDR_END) &&
|
|
|
|
addr != 0) {
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_ADDRESS;
|
2016-04-18 04:07:52 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
|
2016-04-18 04:07:52 +02: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 02:38:01 +02:00
|
|
|
// then we have to allocate from the same region as the caller process instead of the BASE
|
|
|
|
// region.
|
2017-12-10 03:05:43 +01:00
|
|
|
MemoryRegion region = MemoryRegion::BASE;
|
2018-10-17 21:23:56 +02:00
|
|
|
if (addr == 0 && current_process->flags.shared_device_mem)
|
|
|
|
region = current_process->flags.memory_region;
|
2016-04-18 04:07:52 +02:00
|
|
|
|
2018-10-13 23:08:37 +02:00
|
|
|
shared_memory = Core::System::GetInstance().Kernel().CreateSharedMemory(
|
2018-10-26 01:54:06 +02:00
|
|
|
current_process.get(), size, static_cast<MemoryPermission>(my_permission),
|
2018-10-13 23:08:37 +02:00
|
|
|
static_cast<MemoryPermission>(other_permission), addr, region);
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_handle, current_process->handle_table.Create(std::move(shared_memory)));
|
2015-01-11 06:43:29 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_WARNING(Kernel_SVC, "called addr=0x{:08X}", addr);
|
2015-01-23 06:36:58 +01:00
|
|
|
return RESULT_SUCCESS;
|
2014-12-09 05:52:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode CreatePort(Handle* server_port, Handle* client_port, VAddr name_address,
|
|
|
|
u32 max_sessions) {
|
2016-05-22 19:30:13 +02:00
|
|
|
// TODO(Subv): Implement named ports.
|
2017-10-04 19:11:55 +02:00
|
|
|
ASSERT_MSG(name_address == 0, "Named ports are currently unimplemented");
|
2016-05-22 19:30:13 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
SharedPtr<Process> current_process = kernel.GetCurrentProcess();
|
|
|
|
|
|
|
|
auto ports = kernel.CreatePortPair(max_sessions);
|
|
|
|
CASCADE_RESULT(*client_port, current_process->handle_table.Create(
|
|
|
|
std::move(std::get<SharedPtr<ClientPort>>(ports))));
|
2016-09-18 02:38:01 +02:00
|
|
|
// Note: The 3DS kernel also leaks the client port handle if the server port handle fails to be
|
|
|
|
// created.
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*server_port, current_process->handle_table.Create(
|
|
|
|
std::move(std::get<SharedPtr<ServerPort>>(ports))));
|
2016-05-22 19:30:13 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called max_sessions={}", max_sessions);
|
2016-05-22 19:30:13 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:54:36 +02:00
|
|
|
static ResultCode CreateSessionToPort(Handle* out_client_session, Handle client_port_handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
SharedPtr<ClientPort> client_port =
|
|
|
|
current_process->handle_table.Get<ClientPort>(client_port_handle);
|
2017-06-23 08:54:36 +02:00
|
|
|
if (client_port == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
CASCADE_RESULT(auto session, client_port->Connect());
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_client_session, current_process->handle_table.Create(std::move(session)));
|
2017-06-23 08:54:36 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-06-22 02:19:24 +02:00
|
|
|
static ResultCode CreateSession(Handle* server_session, Handle* client_session) {
|
2018-10-20 03:04:18 +02:00
|
|
|
KernelSystem& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
auto sessions = kernel.CreateSessionPair();
|
|
|
|
|
|
|
|
SharedPtr<Process> current_process = kernel.GetCurrentProcess();
|
2017-06-22 02:19:24 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
auto& server = std::get<SharedPtr<ServerSession>>(sessions);
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*server_session, current_process->handle_table.Create(std::move(server)));
|
2017-06-22 02:19:24 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
auto& client = std::get<SharedPtr<ClientSession>>(sessions);
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*client_session, current_process->handle_table.Create(std::move(client)));
|
2017-06-22 02:19:24 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called");
|
2017-06-22 02:19:24 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:57:05 +02:00
|
|
|
static ResultCode AcceptSession(Handle* out_server_session, Handle server_port_handle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> current_process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
|
|
|
SharedPtr<ServerPort> server_port =
|
|
|
|
current_process->handle_table.Get<ServerPort>(server_port_handle);
|
2017-06-23 08:57:05 +02:00
|
|
|
if (server_port == nullptr)
|
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
CASCADE_RESULT(auto session, server_port->Accept());
|
2018-10-20 03:04:18 +02:00
|
|
|
CASCADE_RESULT(*out_server_session, current_process->handle_table.Create(std::move(session)));
|
2017-06-23 08:57:05 +02:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-11-27 04:00:16 +01:00
|
|
|
static ResultCode GetSystemInfo(s64* out, u32 type, s32 param) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called type={} param={}", type, param);
|
2015-11-27 04:00:16 +01:00
|
|
|
|
|
|
|
switch ((SystemInfoType)type) {
|
|
|
|
case SystemInfoType::REGION_MEMORY_USAGE:
|
|
|
|
switch ((SystemInfoMemUsageRegion)param) {
|
|
|
|
case SystemInfoMemUsageRegion::ALL:
|
2017-12-10 03:05:43 +01:00
|
|
|
*out = GetMemoryRegion(MemoryRegion::APPLICATION)->used +
|
|
|
|
GetMemoryRegion(MemoryRegion::SYSTEM)->used +
|
|
|
|
GetMemoryRegion(MemoryRegion::BASE)->used;
|
2015-11-27 04:00:16 +01:00
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::APPLICATION:
|
2017-12-10 03:05:43 +01:00
|
|
|
*out = GetMemoryRegion(MemoryRegion::APPLICATION)->used;
|
2015-11-27 04:00:16 +01:00
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::SYSTEM:
|
2017-12-10 03:05:43 +01:00
|
|
|
*out = GetMemoryRegion(MemoryRegion::SYSTEM)->used;
|
2015-11-27 04:00:16 +01:00
|
|
|
break;
|
|
|
|
case SystemInfoMemUsageRegion::BASE:
|
2017-12-10 03:05:43 +01:00
|
|
|
*out = GetMemoryRegion(MemoryRegion::BASE)->used;
|
2015-11-27 04:00:16 +01:00
|
|
|
break;
|
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetSystemInfo type=0 region: param={}", param);
|
2015-11-27 04:00:16 +01:00
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SystemInfoType::KERNEL_ALLOCATED_PAGES:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented GetSystemInfo type=2 param={}", param);
|
2015-11-27 04:00:16 +01:00
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
case SystemInfoType::KERNEL_SPAWNED_PIDS:
|
|
|
|
*out = 5;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetSystemInfo type={} param={}", type, param);
|
2015-11-27 04:00:16 +01:00
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function never returns an error, even if invalid parameters were passed.
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
static ResultCode GetProcessInfo(s64* out, Handle process_handle, u32 type) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Kernel_SVC, "called process=0x{:08X} type={}", process_handle, type);
|
2015-08-06 02:39:53 +02:00
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
SharedPtr<Process> process =
|
|
|
|
Core::System::GetInstance().Kernel().GetCurrentProcess()->handle_table.Get<Process>(
|
|
|
|
process_handle);
|
2015-08-06 02:39:53 +02:00
|
|
|
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 02:38:01 +02:00
|
|
|
if (*out % Memory::PAGE_SIZE != 0) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "called, memory size not page-aligned");
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_MISALIGNED_SIZE;
|
2016-04-07 10:26:12 +02:00
|
|
|
}
|
2015-08-06 02:39:53 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
// These are valid, but not implemented yet
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented GetProcessInfo type={}", type);
|
2015-08-06 02:39:53 +02:00
|
|
|
break;
|
|
|
|
case 20:
|
2017-12-08 20:28:48 +01:00
|
|
|
*out = Memory::FCRAM_PADDR - process->GetLinearHeapAreaAddress();
|
2015-08-06 02:39:53 +02:00
|
|
|
break;
|
2017-05-21 09:11:36 +02:00
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
case 23:
|
|
|
|
// These return a different error value than higher invalid values
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetProcessInfo type={}", type);
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_NOT_IMPLEMENTED;
|
2015-08-06 02:39:53 +02:00
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown GetProcessInfo type={}", type);
|
2017-12-10 03:05:43 +01:00
|
|
|
return ERR_INVALID_ENUM_VALUE;
|
2015-08-06 02:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:04:25 +02:00
|
|
|
namespace {
|
2016-09-18 02:38:01 +02:00
|
|
|
struct FunctionDef {
|
|
|
|
using Func = void();
|
2015-05-06 05:04:25 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
u32 id;
|
|
|
|
Func* func;
|
|
|
|
const char* name;
|
|
|
|
};
|
2017-07-21 06:52:50 +02:00
|
|
|
} // namespace
|
2015-05-06 05:04:25 +02:00
|
|
|
|
|
|
|
static const FunctionDef SVC_Table[] = {
|
2016-09-18 02:38:01 +02:00
|
|
|
{0x00, nullptr, "Unknown"},
|
|
|
|
{0x01, HLE::Wrap<ControlMemory>, "ControlMemory"},
|
|
|
|
{0x02, HLE::Wrap<QueryMemory>, "QueryMemory"},
|
2017-12-15 16:58:09 +01:00
|
|
|
{0x03, ExitProcess, "ExitProcess"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{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 04:30:39 +02:00
|
|
|
{0x21, HLE::Wrap<CreateAddressArbiter>, "CreateAddressArbiter"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{0x22, HLE::Wrap<ArbitrateAddress>, "ArbitrateAddress"},
|
|
|
|
{0x23, HLE::Wrap<CloseHandle>, "CloseHandle"},
|
2014-06-10 04:30:39 +02:00
|
|
|
{0x24, HLE::Wrap<WaitSynchronization1>, "WaitSynchronization1"},
|
|
|
|
{0x25, HLE::Wrap<WaitSynchronizationN>, "WaitSynchronizationN"},
|
2016-09-18 02:38:01 +02: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 16:15:10 +02:00
|
|
|
{0x36, HLE::Wrap<GetProcessIdOfThread>, "GetProcessIdOfThread"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{0x37, HLE::Wrap<GetThreadId>, "GetThreadId"},
|
|
|
|
{0x38, HLE::Wrap<GetResourceLimit>, "GetResourceLimit"},
|
2015-05-12 22:25:15 +02:00
|
|
|
{0x39, HLE::Wrap<GetResourceLimitLimitValues>, "GetResourceLimitLimitValues"},
|
2014-06-10 04:30:39 +02:00
|
|
|
{0x3A, HLE::Wrap<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
|
2016-09-18 02:38:01 +02: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"},
|
2017-06-23 08:54:36 +02:00
|
|
|
{0x48, HLE::Wrap<CreateSessionToPort>, "CreateSessionToPort"},
|
2017-06-22 02:19:24 +02:00
|
|
|
{0x49, HLE::Wrap<CreateSession>, "CreateSession"},
|
2017-06-23 08:57:05 +02:00
|
|
|
{0x4A, HLE::Wrap<AcceptSession>, "AcceptSession"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{0x4B, nullptr, "ReplyAndReceive1"},
|
|
|
|
{0x4C, nullptr, "ReplyAndReceive2"},
|
|
|
|
{0x4D, nullptr, "ReplyAndReceive3"},
|
|
|
|
{0x4E, nullptr, "ReplyAndReceive4"},
|
2017-06-21 02:27:20 +02:00
|
|
|
{0x4F, HLE::Wrap<ReplyAndReceive>, "ReplyAndReceive"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{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"},
|
2017-12-08 04:14:51 +01:00
|
|
|
{0x59, nullptr, "SetGpuProt"},
|
|
|
|
{0x5A, nullptr, "SetWifiEnabled"},
|
2016-09-18 02:38:01 +02:00
|
|
|
{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 01:58:28 +02:00
|
|
|
};
|
|
|
|
|
2015-07-21 09:51:36 +02:00
|
|
|
static const FunctionDef* GetSVCInfo(u32 func_num) {
|
2015-05-06 05:04:25 +02:00
|
|
|
if (func_num >= ARRAY_SIZE(SVC_Table)) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unknown svc=0x{:02X}", func_num);
|
2015-05-06 05:04:25 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &SVC_Table[func_num];
|
|
|
|
}
|
|
|
|
|
2015-08-17 23:25:21 +02:00
|
|
|
MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
|
|
|
|
|
2015-07-21 09:51:36 +02:00
|
|
|
void CallSVC(u32 immediate) {
|
2015-08-17 23:25:21 +02:00
|
|
|
MICROPROFILE_SCOPE(Kernel_SVC);
|
2015-05-06 05:04:25 +02:00
|
|
|
|
2017-07-21 06:52:50 +02:00
|
|
|
// Lock the global kernel mutex when we enter the kernel HLE.
|
2017-08-29 10:39:55 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
|
2017-07-21 06:52:50 +02:00
|
|
|
|
2018-10-17 21:23:56 +02:00
|
|
|
DEBUG_ASSERT_MSG(Core::System::GetInstance().Kernel().GetCurrentProcess()->status ==
|
|
|
|
ProcessStatus::Running,
|
|
|
|
"Running threads from exiting processes is unimplemented");
|
2017-12-15 16:58:09 +01:00
|
|
|
|
2015-07-21 09:51:36 +02:00
|
|
|
const FunctionDef* info = GetSVCInfo(immediate);
|
2015-05-06 05:04:25 +02:00
|
|
|
if (info) {
|
|
|
|
if (info->func) {
|
|
|
|
info->func();
|
|
|
|
} else {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Kernel_SVC, "unimplemented SVC function {}(..)", info->name);
|
2015-05-06 05:04:25 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-11 01:58:28 +02:00
|
|
|
}
|
2014-04-12 00:44:21 +02:00
|
|
|
|
2017-12-10 03:05:43 +01:00
|
|
|
} // namespace Kernel
|