summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r--src/core/hle/kernel/hle_ipc.cpp99
-rw-r--r--src/core/hle/kernel/hle_ipc.h54
2 files changed, 150 insertions, 3 deletions
diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp
index a60b8ef00..6cf1886cf 100644
--- a/src/core/hle/kernel/hle_ipc.cpp
+++ b/src/core/hle/kernel/hle_ipc.cpp
@@ -5,8 +5,10 @@
#include <boost/range/algorithm_ext/erase.hpp>
#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/process.h"
#include "core/hle/kernel/server_session.h"
namespace Kernel {
@@ -23,4 +25,101 @@ void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_s
HLERequestContext::~HLERequestContext() = default;
+SharedPtr<Object> HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const {
+ ASSERT(id_from_cmdbuf < request_handles.size());
+ return request_handles[id_from_cmdbuf];
+}
+
+u32 HLERequestContext::AddOutgoingHandle(SharedPtr<Object> object) {
+ request_handles.push_back(std::move(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) {
+ 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> 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> 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 c30184eab..cbb109d8f 100644
--- a/src/core/hle/kernel/hle_ipc.h
+++ b/src/core/hle/kernel/hle_ipc.h
@@ -4,8 +4,13 @@
#pragma once
+#include <array>
#include <memory>
#include <vector>
+#include <boost/container/small_vector.hpp>
+#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"
@@ -15,6 +20,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
@@ -59,14 +67,28 @@ 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:
~HLERequestContext();
/// Returns a pointer to the IPC command buffer for this request.
- u32* CommandBuffer() const {
- return cmd_buf;
+ u32* CommandBuffer() {
+ return cmd_buf.data();
}
/**
@@ -77,11 +99,37 @@ public:
return session;
}
+ /**
+ * 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<Object> 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> 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;
- u32* cmd_buf = nullptr;
+ 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<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
SharedPtr<ServerSession> session;
+ // TODO(yuriks): Check common usage of this and optimize size accordingly
+ boost::container::small_vector<SharedPtr<Object>, 8> request_handles;
};
} // namespace Kernel