2017-06-05 00:52:19 -04:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
|
|
|
#include "common/assert.h"
|
2017-10-15 01:24:22 -04:00
|
|
|
#include "common/common_funcs.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
#include "common/common_types.h"
|
2017-10-14 22:18:42 -04:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2017-06-09 02:55:18 -04:00
|
|
|
#include "core/hle/kernel/handle_table.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
#include "core/hle/kernel/hle_ipc.h"
|
|
|
|
#include "core/hle/kernel/kernel.h"
|
2017-06-09 08:23:13 -04:00
|
|
|
#include "core/hle/kernel/process.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
void SessionRequestHandler::ClientConnected(SharedPtr<ServerSession> server_session) {
|
2017-06-06 01:39:26 -04:00
|
|
|
server_session->SetHleHandler(shared_from_this());
|
2017-06-05 00:52:19 -04:00
|
|
|
connected_sessions.push_back(server_session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_session) {
|
2017-06-06 01:39:26 -04:00
|
|
|
server_session->SetHleHandler(nullptr);
|
2017-06-05 00:52:19 -04:00
|
|
|
boost::range::remove_erase(connected_sessions, server_session);
|
|
|
|
}
|
|
|
|
|
2017-06-18 19:05:12 -04:00
|
|
|
HLERequestContext::HLERequestContext(SharedPtr<ServerSession> session)
|
|
|
|
: session(std::move(session)) {
|
|
|
|
cmd_buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:20:52 -04:00
|
|
|
HLERequestContext::~HLERequestContext() = default;
|
|
|
|
|
2017-06-09 08:23:13 -04:00
|
|
|
SharedPtr<Object> HLERequestContext::GetIncomingHandle(u32 id_from_cmdbuf) const {
|
|
|
|
ASSERT(id_from_cmdbuf < request_handles.size());
|
|
|
|
return request_handles[id_from_cmdbuf];
|
2017-06-09 02:55:18 -04:00
|
|
|
}
|
|
|
|
|
2017-06-09 08:23:13 -04:00
|
|
|
u32 HLERequestContext::AddOutgoingHandle(SharedPtr<Object> object) {
|
|
|
|
request_handles.push_back(std::move(object));
|
2017-09-26 19:26:09 -04:00
|
|
|
return static_cast<u32>(request_handles.size() - 1);
|
2017-06-09 08:23:13 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 20:57:08 -04:00
|
|
|
void HLERequestContext::ClearIncomingObjects() {
|
|
|
|
request_handles.clear();
|
|
|
|
}
|
|
|
|
|
2017-10-15 01:24:22 -04:00
|
|
|
void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
|
2017-10-14 22:18:42 -04:00
|
|
|
IPC::RequestParser rp(src_cmdbuf);
|
|
|
|
command_header = std::make_unique<IPC::CommandHeader>(rp.PopRaw<IPC::CommandHeader>());
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-15 01:24:22 -04:00
|
|
|
if (command_header->type == IPC::CommandType::Close) {
|
|
|
|
// Close does not populate the rest of the IPC header
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
// If handle descriptor is present, add size of it
|
|
|
|
if (command_header->enable_handle_descriptor) {
|
|
|
|
handle_descriptor_header =
|
|
|
|
std::make_unique<IPC::HandleDescriptorHeader>(rp.PopRaw<IPC::HandleDescriptorHeader>());
|
|
|
|
if (handle_descriptor_header->send_current_pid) {
|
|
|
|
rp.Skip(2, false);
|
|
|
|
}
|
|
|
|
rp.Skip(handle_descriptor_header->num_handles_to_copy, false);
|
|
|
|
rp.Skip(handle_descriptor_header->num_handles_to_move, false);
|
|
|
|
}
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
// Padding to align to 16 bytes
|
|
|
|
rp.AlignWithPadding();
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
if (command_header->num_buf_x_descriptors) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
if (command_header->num_buf_a_descriptors) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
if (command_header->num_buf_b_descriptors) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
if (command_header->num_buf_w_descriptors) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
if (command_header->buf_c_descriptor_flags !=
|
|
|
|
IPC::CommandHeader::BufferDescriptorCFlag::Disabled) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-15 01:24:22 -04:00
|
|
|
if (incoming && Session()->IsDomain()) {
|
|
|
|
domain_message_header = std::make_unique<IPC::DomainMessageHeader>(rp.PopRaw<IPC::DomainMessageHeader>());
|
|
|
|
}
|
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
data_payload_header =
|
|
|
|
std::make_unique<IPC::DataPayloadHeader>(rp.PopRaw<IPC::DataPayloadHeader>());
|
2017-10-15 01:24:22 -04:00
|
|
|
|
|
|
|
if (incoming) {
|
|
|
|
ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'I'));
|
|
|
|
} else {
|
|
|
|
ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'O'));
|
|
|
|
}
|
2017-10-14 22:18:42 -04:00
|
|
|
|
|
|
|
data_payload_offset = rp.GetCurrentOffset();
|
|
|
|
command = rp.Pop<u32_le>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf,
|
|
|
|
Process& src_process,
|
|
|
|
HandleTable& src_table) {
|
2017-10-15 01:24:22 -04:00
|
|
|
ParseCommandBuffer(src_cmdbuf, true);
|
2017-10-14 22:18:42 -04:00
|
|
|
size_t untranslated_size = data_payload_offset + command_header->data_size;
|
|
|
|
std::copy_n(src_cmdbuf, untranslated_size, cmd_buf.begin());
|
|
|
|
|
|
|
|
if (command_header->enable_handle_descriptor) {
|
|
|
|
if (handle_descriptor_header->num_handles_to_copy ||
|
|
|
|
handle_descriptor_header->num_handles_to_move) {
|
|
|
|
UNIMPLEMENTED();
|
2017-06-09 08:23:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
|
2017-10-14 22:18:42 -04:00
|
|
|
HandleTable& dst_table) {
|
2017-10-15 01:24:22 -04:00
|
|
|
ParseCommandBuffer(&cmd_buf[0], false);
|
2017-10-14 22:18:42 -04:00
|
|
|
size_t untranslated_size = data_payload_offset + command_header->data_size;
|
2017-06-09 08:23:13 -04:00
|
|
|
std::copy_n(cmd_buf.begin(), untranslated_size, dst_cmdbuf);
|
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
if (command_header->enable_handle_descriptor) {
|
|
|
|
size_t command_size = untranslated_size + handle_descriptor_header->num_handles_to_copy +
|
|
|
|
handle_descriptor_header->num_handles_to_move;
|
|
|
|
ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH);
|
|
|
|
|
|
|
|
size_t untranslated_index = untranslated_size;
|
|
|
|
size_t handle_write_offset = 3;
|
|
|
|
while (untranslated_index < command_size) {
|
|
|
|
u32 descriptor = cmd_buf[untranslated_index];
|
|
|
|
untranslated_index += 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);
|
|
|
|
for (u32 j = 0; j < num_handles; ++j) {
|
|
|
|
SharedPtr<Object> object = GetIncomingHandle(cmd_buf[untranslated_index]);
|
|
|
|
Handle handle = 0;
|
|
|
|
if (object != nullptr) {
|
|
|
|
// TODO(yuriks): Figure out the proper error handling for if this fails
|
|
|
|
handle = dst_table.Create(object).Unwrap();
|
|
|
|
}
|
|
|
|
dst_cmdbuf[handle_write_offset++] = handle;
|
|
|
|
untranslated_index++;
|
2017-06-21 17:27:03 -04:00
|
|
|
}
|
2017-10-14 22:18:42 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unsupported handle translation: 0x%08X", descriptor);
|
2017-06-09 08:23:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
2017-06-09 02:55:18 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 00:52:19 -04:00
|
|
|
} // namespace Kernel
|