From 20e5abb30807d4e0e34c79c049252f0872e47ca7 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Thu, 8 Jun 2017 23:52:30 -0700 Subject: ServiceFramework: Use separate copy of command buffer Copy the IPC command buffer to/from the request context before/after the handler is invoked. This is part of a move away from using global data for handling IPC requests. --- src/core/hle/kernel/hle_ipc.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index c30184eab..aa0046001 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -4,8 +4,11 @@ #pragma once +#include #include #include +#include "common/common_types.h" +#include "core/hle/ipc.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/server_session.h" @@ -65,8 +68,8 @@ public: ~HLERequestContext(); /// Returns a pointer to the IPC command buffer for this request. - u32* CommandBuffer() const { - return cmd_buf; + u32* CommandBuffer() { + return cmd_buf.data(); } /** @@ -80,7 +83,7 @@ public: private: friend class Service::ServiceFrameworkBase; - u32* cmd_buf = nullptr; + std::array cmd_buf; SharedPtr session; }; -- cgit v1.2.3 From 05fee702117dd5000109257ac35efe28c820d828 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Thu, 8 Jun 2017 23:55:18 -0700 Subject: Kernel: Add methods in HLERequestContext abstracting handle creation --- src/core/hle/kernel/hle_ipc.cpp | 9 +++++++++ src/core/hle/kernel/hle_ipc.h | 3 +++ 2 files changed, 12 insertions(+) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index a60b8ef00..5a74645c7 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -5,6 +5,7 @@ #include #include "common/assert.h" #include "common/common_types.h" +#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/server_session.h" @@ -23,4 +24,12 @@ void SessionRequestHandler::ClientDisconnected(SharedPtr server_s HLERequestContext::~HLERequestContext() = default; +SharedPtr HLERequestContext::GetIncomingHandle(Handle id_from_cmdbuf) const { + return Kernel::g_handle_table.GetGeneric(id_from_cmdbuf); +} + +Handle HLERequestContext::AddOutgoingHandle(SharedPtr object) { + return Kernel::g_handle_table.Create(object).Unwrap(); +} + } // namespace Kernel diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index aa0046001..f23daa7ea 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -80,6 +80,9 @@ public: return session; } + SharedPtr GetIncomingHandle(Handle id_from_cmdbuf) const; + Handle AddOutgoingHandle(SharedPtr object); + private: friend class Service::ServiceFrameworkBase; -- cgit v1.2.3 From 8cb65fe65a48988d40dd3ca2c4f673629a275b4b Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Fri, 9 Jun 2017 05:23:13 -0700 Subject: Kernel: Basic support for IPC translation for HLE services --- src/core/hle/kernel/hle_ipc.cpp | 94 +++++++++++++++++++++++++++++++++++++++-- src/core/hle/kernel/hle_ipc.h | 37 +++++++++++++++- 2 files changed, 125 insertions(+), 6 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 5a74645c7..656405dd6 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -8,6 +8,7 @@ #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/process.h" #include "core/hle/kernel/server_session.h" namespace Kernel { @@ -24,12 +25,97 @@ void SessionRequestHandler::ClientDisconnected(SharedPtr server_s HLERequestContext::~HLERequestContext() = default; -SharedPtr HLERequestContext::GetIncomingHandle(Handle id_from_cmdbuf) const { - return Kernel::g_handle_table.GetGeneric(id_from_cmdbuf); +SharedPtr HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const { + ASSERT(id_from_cmdbuf < request_handles.size()); + return request_handles[id_from_cmdbuf]; } -Handle HLERequestContext::AddOutgoingHandle(SharedPtr object) { - return Kernel::g_handle_table.Create(object).Unwrap(); +u32 HLERequestContext::AddOutgoingHandle(SharedPtr object) { + request_handles.push_back(std::move(object)); + return request_handles.size() - 1; +} + +ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, + Process& src_process, + HandleTable& src_table) { + IPC::Header header{src_cmdbuf[0]}; + + size_t untranslated_size = 1u + header.normal_params_size; + size_t command_size = untranslated_size + header.translate_params_size; + ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH); // TODO(yuriks): Return error + + std::copy_n(src_cmdbuf, untranslated_size, cmd_buf.begin()); + + size_t i = untranslated_size; + while (i < command_size) { + u32 descriptor = cmd_buf[i] = src_cmdbuf[i]; + i += 1; + + switch (IPC::GetDescriptorType(descriptor)) { + case IPC::DescriptorType::CopyHandle: + case IPC::DescriptorType::MoveHandle: { + u32 num_handles = IPC::HandleNumberFromDesc(descriptor); + ASSERT(i + num_handles <= command_size); // TODO(yuriks): Return error + for (u32 j = 0; j < num_handles; ++j) { + Handle handle = src_cmdbuf[i]; + SharedPtr object = src_table.GetGeneric(handle); + ASSERT(object != nullptr); // TODO(yuriks): Return error + if (descriptor == IPC::DescriptorType::MoveHandle) { + src_table.Close(handle); + } + + cmd_buf[i++] = AddOutgoingHandle(std::move(object)); + } + break; + } + case IPC::DescriptorType::CallingPid: { + cmd_buf[i++] = src_process.process_id; + break; + } + default: + UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor); + } + } + + return RESULT_SUCCESS; +} + +ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process, + HandleTable& dst_table) const { + IPC::Header header{cmd_buf[0]}; + + size_t untranslated_size = 1u + header.normal_params_size; + size_t command_size = untranslated_size + header.translate_params_size; + ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH); + + std::copy_n(cmd_buf.begin(), untranslated_size, dst_cmdbuf); + + size_t i = untranslated_size; + while (i < command_size) { + u32 descriptor = dst_cmdbuf[i] = cmd_buf[i]; + i += 1; + + switch (IPC::GetDescriptorType(descriptor)) { + case IPC::DescriptorType::CopyHandle: + case IPC::DescriptorType::MoveHandle: { + // HLE services don't use handles, so we treat both CopyHandle and MoveHandle equally + u32 num_handles = IPC::HandleNumberFromDesc(descriptor); + ASSERT(i + num_handles <= command_size); + for (u32 j = 0; j < num_handles; ++j) { + SharedPtr object = GetIncomingHandle(cmd_buf[i]); + + // TODO(yuriks): Figure out the proper error handling for if this fails + Handle handle = dst_table.Create(object).Unwrap(); + dst_cmdbuf[i++] = handle; + } + break; + } + default: + UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor); + } + } + + return RESULT_SUCCESS; } } // namespace Kernel diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index f23daa7ea..d6ebf113c 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -8,6 +8,7 @@ #include #include #include "common/common_types.h" +#include "common/swap.h" #include "core/hle/ipc.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/server_session.h" @@ -18,6 +19,9 @@ class ServiceFrameworkBase; namespace Kernel { +class HandleTable; +class Process; + /** * Interface implemented by HLE Session handlers. * This can be provided to a ServerSession in order to hook into several relevant events @@ -62,6 +66,20 @@ protected: * Class containing information about an in-flight IPC request being handled by an HLE service * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and * when possible use the APIs in this class to service the request. + * + * HLE handle protocol + * =================== + * + * To avoid needing HLE services to keep a separate handle table, or having to directly modify the + * requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel + * will decode the incoming handles into object pointers and insert a id in the buffer where the + * handle would normally be. The service then calls GetIncomingHandle() with that id to get the + * pointer to the object. Similarly, instead of inserting a handle into the command buffer, the + * service calls AddOutgoingHandle() and stores the returned id where the handle would normally go. + * + * The end result is similar to just giving services their own real handle tables, but since these + * ids are local to a specific context, it avoids requiring services to manage handles for objects + * across multiple calls and ensuring that unneeded handles are cleaned up. */ class HLERequestContext { public: @@ -80,14 +98,29 @@ public: return session; } - SharedPtr GetIncomingHandle(Handle id_from_cmdbuf) const; - Handle AddOutgoingHandle(SharedPtr object); + /** + * Resolves a object id from the request command buffer into a pointer to an object. See the + * "HLE handle protocol" section in the class documentation for more details. + */ + SharedPtr GetIncomingHandle(u32 id_from_cmdbuf) const; + + /** + * Adds an outgoing object to the response, returning the id which should be used to reference + * it. See the "HLE handle protocol" section in the class documentation for more details. + */ + u32 AddOutgoingHandle(SharedPtr object); private: friend class Service::ServiceFrameworkBase; + ResultCode PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, Process& src_process, + HandleTable& src_table); + ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process, + HandleTable& dst_table) const; + std::array cmd_buf; SharedPtr session; + std::vector> request_handles; }; } // namespace Kernel -- cgit v1.2.3 From 92ca422088eb49c31e8b6146872548c456e56f3e Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 10 Jun 2017 17:57:08 -0700 Subject: Kernel: Allow clearing request_objects to re-use buffer space Reduces the necessary allocation to max(in_handles, out_handles) rather than (in_handles + out_handles). --- src/core/hle/kernel/hle_ipc.cpp | 4 ++++ src/core/hle/kernel/hle_ipc.h | 7 +++++++ 2 files changed, 11 insertions(+) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 656405dd6..6cf1886cf 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -35,6 +35,10 @@ u32 HLERequestContext::AddOutgoingHandle(SharedPtr object) { return request_handles.size() - 1; } +void HLERequestContext::ClearIncomingObjects() { + request_handles.clear(); +} + ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, Process& src_process, HandleTable& src_table) { diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index d6ebf113c..1022dece8 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -110,6 +110,13 @@ public: */ u32 AddOutgoingHandle(SharedPtr object); + /** + * Discards all Objects from the context, invalidating all ids. This may be called after reading + * out all incoming objects, so that the buffer memory can be re-used for outgoing handles, but + * this is not required. + */ + void ClearIncomingObjects(); + private: friend class Service::ServiceFrameworkBase; -- cgit v1.2.3 From d8f6000f5434b94412ac5738f8f72ba7ad9f8497 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sun, 11 Jun 2017 12:51:05 -0700 Subject: Kernel/IPC: Use boost::small_vector for HLE context objects --- src/core/hle/kernel/hle_ipc.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 1022dece8..cbb109d8f 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "common/common_types.h" #include "common/swap.h" #include "core/hle/ipc.h" @@ -127,7 +128,8 @@ private: std::array cmd_buf; SharedPtr session; - std::vector> request_handles; + // TODO(yuriks): Check common usage of this and optimize size accordingly + boost::container::small_vector, 8> request_handles; }; } // namespace Kernel -- cgit v1.2.3