2018-01-13 16:22:39 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-06-05 00:52:19 -04:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-31 08:06:09 -04:00
|
|
|
#include <algorithm>
|
2018-08-01 22:40:00 -04:00
|
|
|
#include <array>
|
|
|
|
#include <sstream>
|
2018-07-18 19:02:47 -04:00
|
|
|
#include <utility>
|
|
|
|
|
2017-06-05 00:52:19 -04:00
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
2018-08-01 22:40:00 -04:00
|
|
|
|
2017-06-05 00:52:19 -04:00
|
|
|
#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"
|
2018-08-01 22:40:00 -04:00
|
|
|
#include "common/logging/log.h"
|
2017-10-14 22:18:42 -04:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2020-02-25 18:43:28 -05:00
|
|
|
#include "core/hle/kernel/errors.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"
|
2018-08-31 12:21:34 -04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2018-08-01 22:40:00 -04:00
|
|
|
#include "core/hle/kernel/object.h"
|
2017-06-09 08:23:13 -04:00
|
|
|
#include "core/hle/kernel/process.h"
|
2018-11-26 18:32:52 -05:00
|
|
|
#include "core/hle/kernel/readable_event.h"
|
2020-02-25 18:43:28 -05:00
|
|
|
#include "core/hle/kernel/scheduler.h"
|
2020-03-03 12:02:50 -05:00
|
|
|
#include "core/hle/kernel/server_session.h"
|
2018-12-31 18:09:41 -05:00
|
|
|
#include "core/hle/kernel/thread.h"
|
2020-02-25 18:43:28 -05:00
|
|
|
#include "core/hle/kernel/time_manager.h"
|
2018-11-26 18:32:52 -05:00
|
|
|
#include "core/hle/kernel/writable_event.h"
|
2018-02-13 21:41:20 -05:00
|
|
|
#include "core/memory.h"
|
2017-06-05 00:52:19 -04:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2018-12-31 18:09:41 -05:00
|
|
|
SessionRequestHandler::SessionRequestHandler() = default;
|
|
|
|
|
|
|
|
SessionRequestHandler::~SessionRequestHandler() = default;
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
void SessionRequestHandler::ClientConnected(std::shared_ptr<ServerSession> server_session) {
|
2017-06-06 01:39:26 -04:00
|
|
|
server_session->SetHleHandler(shared_from_this());
|
2018-07-18 19:02:47 -04:00
|
|
|
connected_sessions.push_back(std::move(server_session));
|
2017-06-05 00:52:19 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
void SessionRequestHandler::ClientDisconnected(
|
|
|
|
const std::shared_ptr<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);
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<WritableEvent> HLERequestContext::SleepClientThread(
|
2019-04-02 12:30:03 -04:00
|
|
|
const std::string& reason, u64 timeout, WakeupCallback&& callback,
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<WritableEvent> writable_event) {
|
2018-03-18 20:22:46 -04:00
|
|
|
// Put the client thread to sleep until the wait event is signaled or the timeout expires.
|
|
|
|
|
2018-11-29 09:14:12 -05:00
|
|
|
if (!writable_event) {
|
2018-07-17 00:05:13 -04:00
|
|
|
// Create event if not provided
|
2019-11-03 04:10:12 -05:00
|
|
|
const auto pair = WritableEvent::CreateEventPair(kernel, "HLE Pause Event: " + reason);
|
2018-11-27 09:18:29 -05:00
|
|
|
writable_event = pair.writable;
|
2018-07-17 00:05:13 -04:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:43:28 -05:00
|
|
|
{
|
|
|
|
Handle event_handle = InvalidHandle;
|
|
|
|
SchedulerLockAndSleep lock(kernel, event_handle, thread.get(), timeout);
|
2020-03-03 12:02:50 -05:00
|
|
|
thread->SetHLECallback(
|
|
|
|
[context = *this, callback](std::shared_ptr<Thread> thread) mutable -> bool {
|
|
|
|
ThreadWakeupReason reason = thread->GetSignalingResult() == RESULT_TIMEOUT
|
|
|
|
? ThreadWakeupReason::Timeout
|
|
|
|
: ThreadWakeupReason::Signal;
|
|
|
|
callback(thread, context, reason);
|
|
|
|
context.WriteToOutgoingCommandBuffer(*thread);
|
|
|
|
return true;
|
|
|
|
});
|
2020-02-25 18:43:28 -05:00
|
|
|
const auto readable_event{writable_event->GetReadableEvent()};
|
|
|
|
writable_event->Clear();
|
2020-03-30 21:50:05 -04:00
|
|
|
thread->SetHLESyncObject(readable_event.get());
|
2020-02-25 18:43:28 -05:00
|
|
|
thread->SetStatus(ThreadStatus::WaitHLEEvent);
|
|
|
|
thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT);
|
|
|
|
readable_event->AddWaitingThread(thread);
|
|
|
|
lock.Release();
|
|
|
|
thread->SetHLETimeEvent(event_handle);
|
2018-03-18 20:22:46 -04:00
|
|
|
}
|
|
|
|
|
2019-11-25 18:28:48 -05:00
|
|
|
is_thread_waiting = true;
|
|
|
|
|
2018-11-26 18:32:52 -05:00
|
|
|
return writable_event;
|
2018-03-18 20:22:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:41:30 -04:00
|
|
|
HLERequestContext::HLERequestContext(KernelCore& kernel, Core::Memory::Memory& memory,
|
|
|
|
std::shared_ptr<ServerSession> server_session,
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Thread> thread)
|
2020-05-03 12:41:30 -04:00
|
|
|
: server_session(std::move(server_session)),
|
|
|
|
thread(std::move(thread)), kernel{kernel}, memory{memory} {
|
2017-06-18 19:05:12 -04:00
|
|
|
cmd_buf[0] = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:20:52 -04:00
|
|
|
HLERequestContext::~HLERequestContext() = default;
|
|
|
|
|
2018-10-20 14:34:41 -04:00
|
|
|
void HLERequestContext::ParseCommandBuffer(const HandleTable& handle_table, u32_le* src_cmdbuf,
|
|
|
|
bool incoming) {
|
2017-10-14 22:18:42 -04:00
|
|
|
IPC::RequestParser rp(src_cmdbuf);
|
2019-03-07 16:44:28 -05:00
|
|
|
command_header = 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) {
|
2019-03-07 16:44:28 -05:00
|
|
|
handle_descriptor_header = rp.PopRaw<IPC::HandleDescriptorHeader>();
|
2017-10-14 22:18:42 -04:00
|
|
|
if (handle_descriptor_header->send_current_pid) {
|
|
|
|
rp.Skip(2, false);
|
|
|
|
}
|
2018-01-07 09:22:20 -05:00
|
|
|
if (incoming) {
|
|
|
|
// Populate the object lists with the data in the IPC request.
|
|
|
|
for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_copy; ++handle) {
|
2018-08-28 12:30:33 -04:00
|
|
|
copy_objects.push_back(handle_table.GetGeneric(rp.Pop<Handle>()));
|
2018-01-07 09:22:20 -05:00
|
|
|
}
|
|
|
|
for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_move; ++handle) {
|
2018-08-28 12:30:33 -04:00
|
|
|
move_objects.push_back(handle_table.GetGeneric(rp.Pop<Handle>()));
|
2018-01-07 09:22:20 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For responses we just ignore the handles, they're empty and will be populated when
|
|
|
|
// translating the response.
|
|
|
|
rp.Skip(handle_descriptor_header->num_handles_to_copy, false);
|
|
|
|
rp.Skip(handle_descriptor_header->num_handles_to_move, false);
|
|
|
|
}
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
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
|
|
|
}
|
2018-01-18 14:54:34 -05:00
|
|
|
|
|
|
|
buffer_c_offset = rp.GetCurrentOffset() + command_header->data_size;
|
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
|
|
|
|
2018-05-17 17:03:52 -04:00
|
|
|
if (Session()->IsDomain() && ((command_header->type == IPC::CommandType::Request ||
|
|
|
|
command_header->type == IPC::CommandType::RequestWithContext) ||
|
|
|
|
!incoming)) {
|
2017-12-29 00:36:22 -05:00
|
|
|
// If this is an incoming message, only CommandType "Request" has a domain header
|
2018-02-20 11:27:49 -05:00
|
|
|
// All outgoing domain messages have the domain header, if only incoming has it
|
|
|
|
if (incoming || domain_message_header) {
|
2019-03-07 16:44:28 -05:00
|
|
|
domain_message_header = rp.PopRaw<IPC::DomainMessageHeader>();
|
2018-02-20 14:51:54 -05:00
|
|
|
} else {
|
2019-03-07 16:44:28 -05:00
|
|
|
if (Session()->IsDomain()) {
|
2018-07-02 12:13:26 -04:00
|
|
|
LOG_WARNING(IPC, "Domain request has no DomainMessageHeader!");
|
2019-03-07 16:44:28 -05:00
|
|
|
}
|
2018-02-20 11:27:49 -05:00
|
|
|
}
|
2017-10-15 01:24:22 -04:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:44:28 -05:00
|
|
|
data_payload_header = rp.PopRaw<IPC::DataPayloadHeader>();
|
2017-10-15 01:24:22 -04:00
|
|
|
|
2018-01-17 01:16:55 -05:00
|
|
|
data_payload_offset = rp.GetCurrentOffset();
|
|
|
|
|
2018-01-20 02:48:02 -05:00
|
|
|
if (domain_message_header && domain_message_header->command ==
|
|
|
|
IPC::DomainMessageHeader::CommandType::CloseVirtualHandle) {
|
2018-01-17 01:16:55 -05:00
|
|
|
// CloseVirtualHandle command does not have SFC* or any data
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-18 14:54:34 -05:00
|
|
|
rp.SetCurrentOffset(buffer_c_offset);
|
|
|
|
|
|
|
|
// For Inline buffers, the response data is written directly to buffer_c_offset
|
|
|
|
// and in this case we don't have any BufferDescriptorC on the request.
|
|
|
|
if (command_header->buf_c_descriptor_flags >
|
|
|
|
IPC::CommandHeader::BufferDescriptorCFlag::InlineDescriptor) {
|
|
|
|
if (command_header->buf_c_descriptor_flags ==
|
|
|
|
IPC::CommandHeader::BufferDescriptorCFlag::OneDescriptor) {
|
|
|
|
buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
|
|
|
|
} else {
|
|
|
|
unsigned num_buf_c_descriptors =
|
|
|
|
static_cast<unsigned>(command_header->buf_c_descriptor_flags.Value()) - 2;
|
|
|
|
|
|
|
|
// This is used to detect possible underflows, in case something is broken
|
|
|
|
// with the two ifs above and the flags value is == 0 || == 1.
|
|
|
|
ASSERT(num_buf_c_descriptors < 14);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_buf_c_descriptors; ++i) {
|
|
|
|
buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.SetCurrentOffset(data_payload_offset);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-20 14:34:41 -04:00
|
|
|
ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const HandleTable& handle_table,
|
|
|
|
u32_le* src_cmdbuf) {
|
|
|
|
ParseCommandBuffer(handle_table, src_cmdbuf, true);
|
2018-01-07 01:59:31 -05:00
|
|
|
if (command_header->type == IPC::CommandType::Close) {
|
|
|
|
// Close does not populate the rest of the IPC header
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-01-07 01:50:55 -05:00
|
|
|
// The data_size already includes the payload header, the padding and the domain header.
|
2018-09-15 09:21:06 -04:00
|
|
|
std::size_t size = data_payload_offset + command_header->data_size -
|
|
|
|
sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
|
2018-01-07 01:50:55 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:34:41 -04:00
|
|
|
ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(Thread& thread) {
|
|
|
|
auto& owner_process = *thread.GetOwnerProcess();
|
|
|
|
auto& handle_table = owner_process.GetHandleTable();
|
|
|
|
|
2018-03-18 20:17:06 -04:00
|
|
|
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> dst_cmdbuf;
|
2019-11-26 16:29:34 -05:00
|
|
|
memory.ReadBlock(owner_process, thread.GetTLSAddress(), dst_cmdbuf.data(),
|
|
|
|
dst_cmdbuf.size() * sizeof(u32));
|
2018-03-18 20:17:06 -04:00
|
|
|
|
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.
|
2018-10-20 14:34:41 -04:00
|
|
|
ParseCommandBuffer(handle_table, cmd_buf.data(), false);
|
2018-01-07 01:50:55 -05:00
|
|
|
|
|
|
|
// The data_size already includes the payload header, the padding and the domain header.
|
2018-09-15 09:21:06 -04:00
|
|
|
std::size_t size = data_payload_offset + command_header->data_size -
|
|
|
|
sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
|
2018-01-07 01:50:55 -05:00
|
|
|
if (domain_message_header)
|
|
|
|
size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
|
|
|
|
|
2018-03-18 20:17:06 -04:00
|
|
|
std::copy_n(cmd_buf.begin(), size, dst_cmdbuf.data());
|
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.
|
2018-09-15 09:21:06 -04:00
|
|
|
std::size_t current_offset =
|
2018-01-07 01:50:55 -05:00
|
|
|
(sizeof(IPC::CommandHeader) + sizeof(IPC::HandleDescriptorHeader)) / sizeof(u32);
|
|
|
|
ASSERT_MSG(!handle_descriptor_header->send_current_pid, "Sending PID is not implemented");
|
|
|
|
|
2018-07-14 09:13:16 -04:00
|
|
|
ASSERT(copy_objects.size() == handle_descriptor_header->num_handles_to_copy);
|
|
|
|
ASSERT(move_objects.size() == handle_descriptor_header->num_handles_to_move);
|
2018-01-07 01:50:55 -05:00
|
|
|
|
|
|
|
// 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);
|
2018-08-28 12:30:33 -04:00
|
|
|
dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap();
|
2018-01-07 01:50:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& object : move_objects) {
|
|
|
|
ASSERT(object != nullptr);
|
2018-08-28 12:30:33 -04:00
|
|
|
dst_cmdbuf[current_offset++] = 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.
|
|
|
|
|
2018-02-20 13:59:58 -05:00
|
|
|
if (Session()->IsDomain() && domain_message_header) {
|
2018-01-07 01:50:55 -05:00
|
|
|
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.
|
2018-09-15 09:21:06 -04:00
|
|
|
std::size_t domain_offset = size - domain_message_header->num_objects;
|
2018-01-07 01:50:55 -05:00
|
|
|
|
2019-03-05 18:51:16 -05:00
|
|
|
for (const auto& object : domain_objects) {
|
|
|
|
server_session->AppendDomainRequestHandler(object);
|
|
|
|
dst_cmdbuf[domain_offset++] =
|
|
|
|
static_cast<u32_le>(server_session->NumDomainRequestHandlers());
|
2018-01-07 01:50:55 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-18 20:17:06 -04:00
|
|
|
|
|
|
|
// Copy the translated command buffer back into the thread's command buffer area.
|
2019-11-26 17:39:57 -05:00
|
|
|
memory.WriteBlock(owner_process, thread.GetTLSAddress(), dst_cmdbuf.data(),
|
|
|
|
dst_cmdbuf.size() * sizeof(u32));
|
2018-03-18 20:17:06 -04:00
|
|
|
|
2017-06-09 08:23:13 -04:00
|
|
|
return RESULT_SUCCESS;
|
2017-06-09 02:55:18 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 22:02:08 -04:00
|
|
|
std::vector<u8> HLERequestContext::ReadBuffer(std::size_t buffer_index) const {
|
2020-06-24 08:50:27 -04:00
|
|
|
std::vector<u8> buffer{};
|
2020-04-16 22:02:08 -04:00
|
|
|
const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
|
2020-02-05 22:09:16 -05:00
|
|
|
BufferDescriptorA()[buffer_index].Size()};
|
2018-02-13 21:41:20 -05:00
|
|
|
|
|
|
|
if (is_buffer_a) {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorA().size() > buffer_index, { return buffer; },
|
|
|
|
"BufferDescriptorA invalid buffer_index {}", buffer_index);
|
2018-05-01 16:28:36 -04:00
|
|
|
buffer.resize(BufferDescriptorA()[buffer_index].Size());
|
2019-11-26 16:29:34 -05:00
|
|
|
memory.ReadBlock(BufferDescriptorA()[buffer_index].Address(), buffer.data(), buffer.size());
|
2018-02-13 21:41:20 -05:00
|
|
|
} else {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorX().size() > buffer_index, { return buffer; },
|
|
|
|
"BufferDescriptorX invalid buffer_index {}", buffer_index);
|
2018-05-01 16:28:36 -04:00
|
|
|
buffer.resize(BufferDescriptorX()[buffer_index].Size());
|
2019-11-26 16:29:34 -05:00
|
|
|
memory.ReadBlock(BufferDescriptorX()[buffer_index].Address(), buffer.data(), buffer.size());
|
2018-02-13 21:41:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:06 -04:00
|
|
|
std::size_t HLERequestContext::WriteBuffer(const void* buffer, std::size_t size,
|
2020-04-16 22:02:08 -04:00
|
|
|
std::size_t buffer_index) const {
|
2018-06-22 04:18:23 -04:00
|
|
|
if (size == 0) {
|
2018-07-02 12:13:26 -04:00
|
|
|
LOG_WARNING(Core, "skip empty buffer write");
|
2018-06-22 04:18:23 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:02:08 -04:00
|
|
|
const bool is_buffer_b{BufferDescriptorB().size() > buffer_index &&
|
2020-02-05 22:09:16 -05:00
|
|
|
BufferDescriptorB()[buffer_index].Size()};
|
2018-09-15 09:21:06 -04:00
|
|
|
const std::size_t buffer_size{GetWriteBufferSize(buffer_index)};
|
2018-03-31 16:05:19 -04:00
|
|
|
if (size > buffer_size) {
|
2018-07-02 12:13:26 -04:00
|
|
|
LOG_CRITICAL(Core, "size ({:016X}) is greater than buffer_size ({:016X})", size,
|
2018-07-02 12:20:50 -04:00
|
|
|
buffer_size);
|
2018-03-31 16:05:19 -04:00
|
|
|
size = buffer_size; // TODO(bunnei): This needs to be HW tested
|
|
|
|
}
|
2018-02-14 00:14:17 -05:00
|
|
|
|
2018-02-13 21:41:20 -05:00
|
|
|
if (is_buffer_b) {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorB().size() > buffer_index &&
|
|
|
|
BufferDescriptorB()[buffer_index].Size() >= size,
|
|
|
|
{ return 0; }, "BufferDescriptorB is invalid, index={}, size={}",
|
|
|
|
buffer_index, size);
|
2019-11-26 17:39:57 -05:00
|
|
|
memory.WriteBlock(BufferDescriptorB()[buffer_index].Address(), buffer, size);
|
2018-02-13 21:41:20 -05:00
|
|
|
} else {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorC().size() > buffer_index &&
|
|
|
|
BufferDescriptorC()[buffer_index].Size() >= size,
|
|
|
|
{ return 0; }, "BufferDescriptorC is invalid, index={}, size={}",
|
|
|
|
buffer_index, size);
|
2019-11-26 17:39:57 -05:00
|
|
|
memory.WriteBlock(BufferDescriptorC()[buffer_index].Address(), buffer, size);
|
2018-02-13 21:41:20 -05:00
|
|
|
}
|
2018-02-13 23:53:18 -05:00
|
|
|
|
|
|
|
return size;
|
2018-02-13 21:41:20 -05:00
|
|
|
}
|
|
|
|
|
2020-04-16 22:02:08 -04:00
|
|
|
std::size_t HLERequestContext::GetReadBufferSize(std::size_t buffer_index) const {
|
|
|
|
const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
|
2020-02-05 22:09:16 -05:00
|
|
|
BufferDescriptorA()[buffer_index].Size()};
|
|
|
|
if (is_buffer_a) {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorA().size() > buffer_index, { return 0; },
|
|
|
|
"BufferDescriptorA invalid buffer_index {}", buffer_index);
|
2020-02-05 22:09:16 -05:00
|
|
|
return BufferDescriptorA()[buffer_index].Size();
|
|
|
|
} else {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorX().size() > buffer_index, { return 0; },
|
|
|
|
"BufferDescriptorX invalid buffer_index {}", buffer_index);
|
2020-02-05 22:09:16 -05:00
|
|
|
return BufferDescriptorX()[buffer_index].Size();
|
|
|
|
}
|
2018-02-14 00:14:17 -05:00
|
|
|
}
|
|
|
|
|
2020-04-16 22:02:08 -04:00
|
|
|
std::size_t HLERequestContext::GetWriteBufferSize(std::size_t buffer_index) const {
|
|
|
|
const bool is_buffer_b{BufferDescriptorB().size() > buffer_index &&
|
2020-02-05 22:09:16 -05:00
|
|
|
BufferDescriptorB()[buffer_index].Size()};
|
|
|
|
if (is_buffer_b) {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorB().size() > buffer_index, { return 0; },
|
|
|
|
"BufferDescriptorB invalid buffer_index {}", buffer_index);
|
2020-02-05 22:09:16 -05:00
|
|
|
return BufferDescriptorB()[buffer_index].Size();
|
|
|
|
} else {
|
2020-06-24 08:50:27 -04:00
|
|
|
ASSERT_OR_EXECUTE_MSG(BufferDescriptorC().size() > buffer_index, { return 0; },
|
|
|
|
"BufferDescriptorC invalid buffer_index {}", buffer_index);
|
2020-02-05 22:09:16 -05:00
|
|
|
return BufferDescriptorC()[buffer_index].Size();
|
|
|
|
}
|
2020-06-24 08:50:27 -04:00
|
|
|
return 0;
|
2018-02-13 21:41:20 -05:00
|
|
|
}
|
|
|
|
|
2018-02-15 09:22:11 -05:00
|
|
|
std::string HLERequestContext::Description() const {
|
|
|
|
if (!command_header) {
|
|
|
|
return "No command header available";
|
|
|
|
}
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "IPC::CommandHeader: Type:" << static_cast<u32>(command_header->type.Value());
|
|
|
|
s << ", X(Pointer):" << command_header->num_buf_x_descriptors;
|
|
|
|
if (command_header->num_buf_x_descriptors) {
|
|
|
|
s << '[';
|
|
|
|
for (u64 i = 0; i < command_header->num_buf_x_descriptors; ++i) {
|
|
|
|
s << "0x" << std::hex << BufferDescriptorX()[i].Size();
|
|
|
|
if (i < command_header->num_buf_x_descriptors - 1)
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
s << ']';
|
|
|
|
}
|
|
|
|
s << ", A(Send):" << command_header->num_buf_a_descriptors;
|
|
|
|
if (command_header->num_buf_a_descriptors) {
|
|
|
|
s << '[';
|
|
|
|
for (u64 i = 0; i < command_header->num_buf_a_descriptors; ++i) {
|
|
|
|
s << "0x" << std::hex << BufferDescriptorA()[i].Size();
|
|
|
|
if (i < command_header->num_buf_a_descriptors - 1)
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
s << ']';
|
|
|
|
}
|
|
|
|
s << ", B(Receive):" << command_header->num_buf_b_descriptors;
|
|
|
|
if (command_header->num_buf_b_descriptors) {
|
|
|
|
s << '[';
|
|
|
|
for (u64 i = 0; i < command_header->num_buf_b_descriptors; ++i) {
|
|
|
|
s << "0x" << std::hex << BufferDescriptorB()[i].Size();
|
|
|
|
if (i < command_header->num_buf_b_descriptors - 1)
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
s << ']';
|
|
|
|
}
|
|
|
|
s << ", C(ReceiveList):" << BufferDescriptorC().size();
|
|
|
|
if (!BufferDescriptorC().empty()) {
|
|
|
|
s << '[';
|
|
|
|
for (u64 i = 0; i < BufferDescriptorC().size(); ++i) {
|
|
|
|
s << "0x" << std::hex << BufferDescriptorC()[i].Size();
|
|
|
|
if (i < BufferDescriptorC().size() - 1)
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
s << ']';
|
|
|
|
}
|
|
|
|
s << ", data_size:" << command_header->data_size.Value();
|
|
|
|
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2017-06-05 00:52:19 -04:00
|
|
|
} // namespace Kernel
|