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-12-29 00:36:22 -05:00
|
|
|
#include "core/hle/kernel/domain.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-12-29 00:36:22 -05:00
|
|
|
HLERequestContext::HLERequestContext(SharedPtr<Kernel::Domain> domain) : domain(std::move(domain)) {
|
|
|
|
cmd_buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HLERequestContext::HLERequestContext(SharedPtr<Kernel::ServerSession> server_session)
|
|
|
|
: server_session(std::move(server_session)) {
|
2017-06-18 19:05:12 -04:00
|
|
|
cmd_buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:20:52 -04:00
|
|
|
HLERequestContext::~HLERequestContext() = default;
|
|
|
|
|
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-12-29 00:36:22 -05:00
|
|
|
for (unsigned i = 0; i < command_header->num_buf_x_descriptors; ++i) {
|
2017-10-18 21:38:01 -04:00
|
|
|
buffer_x_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorX>());
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
2017-12-29 00:36:22 -05:00
|
|
|
for (unsigned i = 0; i < command_header->num_buf_a_descriptors; ++i) {
|
2017-10-18 21:38:01 -04:00
|
|
|
buffer_a_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
2017-12-29 00:36:22 -05:00
|
|
|
for (unsigned i = 0; i < command_header->num_buf_b_descriptors; ++i) {
|
2017-10-18 21:38:01 -04:00
|
|
|
buffer_b_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
2017-12-29 00:36:22 -05:00
|
|
|
for (unsigned i = 0; i < command_header->num_buf_w_descriptors; ++i) {
|
2017-10-18 21:38:01 -04:00
|
|
|
buffer_w_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
|
|
|
if (command_header->buf_c_descriptor_flags !=
|
|
|
|
IPC::CommandHeader::BufferDescriptorCFlag::Disabled) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-18 21:38:01 -04:00
|
|
|
// Padding to align to 16 bytes
|
|
|
|
rp.AlignWithPadding();
|
2017-12-29 00:36:22 -05:00
|
|
|
|
|
|
|
if (IsDomain() && (command_header->type == IPC::CommandType::Request || !incoming)) {
|
|
|
|
// If this is an incoming message, only CommandType "Request" has a domain header
|
|
|
|
// All outgoing domain messages have the domain header
|
|
|
|
domain_message_header =
|
2018-01-07 01:50:55 -05:00
|
|
|
std::make_unique<IPC::DomainMessageHeader>(rp.PopRaw<IPC::DomainMessageHeader>());
|
2017-10-15 01:24:22 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-01-07 01:50:55 -05:00
|
|
|
data_payload_offset = rp.GetCurrentOffset();
|
2017-10-14 22:18:42 -04:00
|
|
|
command = rp.Pop<u32_le>();
|
2018-01-06 23:19:42 -05:00
|
|
|
rp.Skip(1, false); // The command is actually an u64, but we don't use the high part.
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-01-07 01:50:55 -05:00
|
|
|
// The data_size already includes the payload header, the padding and the domain header.
|
|
|
|
size_t size = data_payload_offset + command_header->data_size -
|
|
|
|
sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
|
|
|
|
if (domain_message_header)
|
|
|
|
size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
|
|
|
|
std::copy_n(src_cmdbuf, size, cmd_buf.begin());
|
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) {
|
2018-01-07 01:50:55 -05:00
|
|
|
// The header was already built in the internal command buffer. Attempt to parse it to verify
|
|
|
|
// the integrity and then copy it over to the target command buffer.
|
|
|
|
ParseCommandBuffer(cmd_buf.data(), false);
|
|
|
|
|
|
|
|
// The data_size already includes the payload header, the padding and the domain header.
|
|
|
|
size_t size = data_payload_offset + command_header->data_size -
|
|
|
|
sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
|
|
|
|
if (domain_message_header)
|
|
|
|
size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
|
|
|
|
|
|
|
|
std::copy_n(cmd_buf.begin(), size, dst_cmdbuf);
|
2017-06-09 08:23:13 -04:00
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
if (command_header->enable_handle_descriptor) {
|
2018-01-07 01:50:55 -05:00
|
|
|
ASSERT_MSG(!move_objects.empty() || !copy_objects.empty(),
|
|
|
|
"Handle descriptor bit set but no handles to translate");
|
|
|
|
// We write the translated handles at a specific offset in the command buffer, this space
|
|
|
|
// was already reserved when writing the header.
|
|
|
|
size_t current_offset =
|
|
|
|
(sizeof(IPC::CommandHeader) + sizeof(IPC::HandleDescriptorHeader)) / sizeof(u32);
|
|
|
|
ASSERT_MSG(!handle_descriptor_header->send_current_pid, "Sending PID is not implemented");
|
|
|
|
|
|
|
|
ASSERT_MSG(copy_objects.size() == handle_descriptor_header->num_handles_to_copy);
|
|
|
|
ASSERT_MSG(move_objects.size() == handle_descriptor_header->num_handles_to_move);
|
|
|
|
|
|
|
|
// We don't make a distinction between copy and move handles when translating since HLE
|
|
|
|
// services don't deal with handles directly. However, the guest applications might check
|
|
|
|
// for specific values in each of these descriptors.
|
|
|
|
for (auto& object : copy_objects) {
|
|
|
|
ASSERT(object != nullptr);
|
|
|
|
dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& object : move_objects) {
|
|
|
|
ASSERT(object != nullptr);
|
|
|
|
dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
|
2017-06-09 08:23:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 01:50:55 -05:00
|
|
|
// TODO(Subv): Translate the X/A/B/W buffers.
|
|
|
|
|
|
|
|
if (IsDomain()) {
|
|
|
|
ASSERT(domain_message_header->num_objects == domain_objects.size());
|
|
|
|
// Write the domain objects to the command buffer, these go after the raw untranslated data.
|
|
|
|
// TODO(Subv): This completely ignores C buffers.
|
|
|
|
size_t domain_offset = size - domain_message_header->num_objects;
|
|
|
|
auto& request_handlers = domain->request_handlers;
|
|
|
|
|
|
|
|
for (auto& object : domain_objects) {
|
|
|
|
request_handlers.emplace_back(object);
|
|
|
|
dst_cmdbuf[domain_offset++] = request_handlers.size();
|
|
|
|
}
|
|
|
|
}
|
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
|