2017-10-02 04:56:43 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-11-08 19:53:20 +01:00
|
|
|
#include <algorithm>
|
2017-11-07 20:35:17 +01:00
|
|
|
#include "common/alignment.h"
|
2018-11-21 21:21:30 +01:00
|
|
|
#include "core/core.h"
|
2017-10-02 04:56:43 +02:00
|
|
|
#include "core/hle/ipc.h"
|
|
|
|
#include "core/hle/kernel/handle_table.h"
|
|
|
|
#include "core/hle/kernel/ipc.h"
|
2019-07-22 14:19:03 +02:00
|
|
|
#include "core/hle/kernel/ipc_debugger/recorder.h"
|
2017-10-02 04:56:43 +02:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/memory.h"
|
|
|
|
#include "core/hle/kernel/process.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2019-07-22 14:19:03 +02:00
|
|
|
ResultCode TranslateCommandBuffer(Kernel::KernelSystem& kernel, Memory::MemorySystem& memory,
|
|
|
|
std::shared_ptr<Thread> src_thread,
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<Thread> dst_thread, VAddr src_address,
|
2019-02-01 18:32:49 +01:00
|
|
|
VAddr dst_address,
|
2018-11-08 19:53:20 +01:00
|
|
|
std::vector<MappedBufferContext>& mapped_buffer_context,
|
|
|
|
bool reply) {
|
2017-10-02 04:56:43 +02:00
|
|
|
auto& src_process = src_thread->owner_process;
|
2017-11-06 19:30:31 +01:00
|
|
|
auto& dst_process = dst_thread->owner_process;
|
2017-10-02 04:56:43 +02:00
|
|
|
|
|
|
|
IPC::Header header;
|
|
|
|
// TODO(Subv): Replace by Memory::Read32 when possible.
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.ReadBlock(*src_process, src_address, &header.raw, sizeof(header.raw));
|
2017-10-02 04:56:43 +02:00
|
|
|
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t untranslated_size = 1u + header.normal_params_size;
|
|
|
|
std::size_t command_size = untranslated_size + header.translate_params_size;
|
2017-10-02 04:56:43 +02:00
|
|
|
|
|
|
|
// Note: The real kernel does not check that the command length fits into the IPC buffer area.
|
|
|
|
ASSERT(command_size <= IPC::COMMAND_BUFFER_LENGTH);
|
|
|
|
|
|
|
|
std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.ReadBlock(*src_process, src_address, cmd_buf.data(), command_size * sizeof(u32));
|
2017-10-02 04:56:43 +02:00
|
|
|
|
2019-07-22 14:19:03 +02:00
|
|
|
const bool should_record = kernel.GetIPCRecorder().IsEnabled();
|
|
|
|
|
|
|
|
std::vector<u32> untranslated_cmdbuf;
|
|
|
|
if (should_record) {
|
|
|
|
untranslated_cmdbuf = std::vector<u32>{cmd_buf.begin(), cmd_buf.begin() + command_size};
|
|
|
|
}
|
|
|
|
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t i = untranslated_size;
|
2017-10-02 04:56:43 +02:00
|
|
|
while (i < command_size) {
|
|
|
|
u32 descriptor = cmd_buf[i];
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
switch (IPC::GetDescriptorType(descriptor)) {
|
|
|
|
case IPC::DescriptorType::CopyHandle:
|
|
|
|
case IPC::DescriptorType::MoveHandle: {
|
|
|
|
u32 num_handles = IPC::HandleNumberFromDesc(descriptor);
|
|
|
|
// Note: The real kernel does not check that the number of handles fits into the command
|
|
|
|
// buffer before writing them, only after finishing.
|
|
|
|
if (i + num_handles > command_size) {
|
|
|
|
return ResultCode(ErrCodes::CommandTooLarge, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidState, ErrorLevel::Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 j = 0; j < num_handles; ++j) {
|
|
|
|
Handle handle = cmd_buf[i];
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<Object> object = nullptr;
|
2017-10-02 04:56:43 +02:00
|
|
|
// Perform pseudo-handle detection here because by the time this function is called,
|
|
|
|
// the current thread and process are no longer the ones which created this IPC
|
|
|
|
// request, but the ones that are handling it.
|
|
|
|
if (handle == CurrentThread) {
|
|
|
|
object = src_thread;
|
|
|
|
} else if (handle == CurrentProcess) {
|
2019-03-23 21:04:19 +01:00
|
|
|
object = SharedFrom(src_process);
|
2017-10-02 04:56:43 +02:00
|
|
|
} else if (handle != 0) {
|
2018-10-20 03:04:18 +02:00
|
|
|
object = src_process->handle_table.GetGeneric(handle);
|
2017-10-02 04:56:43 +02:00
|
|
|
if (descriptor == IPC::DescriptorType::MoveHandle) {
|
2018-10-20 03:04:18 +02:00
|
|
|
src_process->handle_table.Close(handle);
|
2017-10-02 04:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object == nullptr) {
|
|
|
|
// Note: The real kernel sets invalid translated handles to 0 in the target
|
|
|
|
// command buffer.
|
|
|
|
cmd_buf[i++] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-20 03:04:18 +02:00
|
|
|
auto result = dst_process->handle_table.Create(std::move(object));
|
2017-10-02 04:56:43 +02:00
|
|
|
cmd_buf[i++] = result.ValueOr(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPC::DescriptorType::CallingPid: {
|
|
|
|
cmd_buf[i++] = src_process->process_id;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-06 19:30:31 +01:00
|
|
|
case IPC::DescriptorType::StaticBuffer: {
|
|
|
|
IPC::StaticBufferDescInfo bufferInfo{descriptor};
|
|
|
|
VAddr static_buffer_src_address = cmd_buf[i];
|
|
|
|
|
|
|
|
std::vector<u8> data(bufferInfo.size);
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.ReadBlock(*src_process, static_buffer_src_address, data.data(), data.size());
|
2017-11-06 19:30:31 +01:00
|
|
|
|
|
|
|
// Grab the address that the target thread set up to receive the response static buffer
|
|
|
|
// and write our data there. The static buffers area is located right after the command
|
|
|
|
// buffer area.
|
|
|
|
struct StaticBuffer {
|
|
|
|
IPC::StaticBufferDescInfo descriptor;
|
|
|
|
VAddr address;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(StaticBuffer) == 8, "StaticBuffer struct has incorrect size.");
|
|
|
|
|
|
|
|
StaticBuffer target_buffer;
|
|
|
|
|
|
|
|
u32 static_buffer_offset = IPC::COMMAND_BUFFER_LENGTH * sizeof(u32) +
|
|
|
|
sizeof(StaticBuffer) * bufferInfo.buffer_id;
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.ReadBlock(*dst_process, dst_address + static_buffer_offset, &target_buffer,
|
|
|
|
sizeof(target_buffer));
|
2017-11-06 19:30:31 +01:00
|
|
|
|
|
|
|
// Note: The real kernel doesn't seem to have any error recovery mechanisms for this
|
|
|
|
// case.
|
|
|
|
ASSERT_MSG(target_buffer.descriptor.size >= data.size(),
|
|
|
|
"Static buffer data is too big");
|
|
|
|
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.WriteBlock(*dst_process, target_buffer.address, data.data(), data.size());
|
2017-11-06 19:30:31 +01:00
|
|
|
|
|
|
|
cmd_buf[i++] = target_buffer.address;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-07 20:35:17 +01:00
|
|
|
case IPC::DescriptorType::MappedBuffer: {
|
|
|
|
IPC::MappedBufferDescInfo descInfo{descriptor};
|
|
|
|
VAddr source_address = cmd_buf[i];
|
|
|
|
|
2018-07-23 23:08:14 +02:00
|
|
|
u32 size = static_cast<u32>(descInfo.size);
|
2017-11-07 20:35:17 +01:00
|
|
|
IPC::MappedBufferPermissions permissions = descInfo.perms;
|
|
|
|
|
|
|
|
VAddr page_start = Common::AlignDown(source_address, Memory::PAGE_SIZE);
|
|
|
|
u32 page_offset = source_address - page_start;
|
|
|
|
u32 num_pages =
|
|
|
|
Common::AlignUp(page_offset + size, Memory::PAGE_SIZE) >> Memory::PAGE_BITS;
|
|
|
|
|
2018-10-01 03:21:51 +02:00
|
|
|
// Skip when the size is zero and num_pages == 0
|
2018-09-11 20:34:18 +02:00
|
|
|
if (size == 0) {
|
2018-10-01 03:21:51 +02:00
|
|
|
cmd_buf[i++] = 0;
|
2018-09-11 20:34:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-07 20:35:17 +01:00
|
|
|
ASSERT(num_pages >= 1);
|
|
|
|
|
|
|
|
if (reply) {
|
2018-10-29 23:35:34 +01:00
|
|
|
// Scan the target's command buffer for the matching mapped buffer.
|
|
|
|
// The real kernel panics if you try to reply with an unsolicited MappedBuffer.
|
2018-11-08 19:53:20 +01:00
|
|
|
auto found = std::find_if(
|
|
|
|
mapped_buffer_context.begin(), mapped_buffer_context.end(),
|
|
|
|
[permissions, size, source_address](const MappedBufferContext& context) {
|
|
|
|
// Note: reply's source_address is request's target_address
|
|
|
|
return context.permissions == permissions && context.size == size &&
|
|
|
|
context.target_address == source_address;
|
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT(found != mapped_buffer_context.end());
|
|
|
|
|
|
|
|
if (permissions != IPC::MappedBufferPermissions::R) {
|
|
|
|
// Copy the modified buffer back into the target process
|
2019-04-19 20:15:05 +02:00
|
|
|
// NOTE: As this is a reply the "source" is the destination and the
|
|
|
|
// "target" is the source.
|
|
|
|
memory.CopyBlock(*dst_process, *src_process, found->source_address,
|
|
|
|
found->target_address, size);
|
2018-11-08 19:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VAddr prev_reserve = page_start - Memory::PAGE_SIZE;
|
|
|
|
VAddr next_reserve = page_start + num_pages * Memory::PAGE_SIZE;
|
|
|
|
|
|
|
|
auto& prev_vma = src_process->vm_manager.FindVMA(prev_reserve)->second;
|
|
|
|
auto& next_vma = src_process->vm_manager.FindVMA(next_reserve)->second;
|
|
|
|
ASSERT(prev_vma.meminfo_state == MemoryState::Reserved &&
|
|
|
|
next_vma.meminfo_state == MemoryState::Reserved);
|
|
|
|
|
|
|
|
// Unmap the buffer and guard pages from the source process
|
|
|
|
ResultCode result = src_process->vm_manager.UnmapRange(
|
|
|
|
page_start - Memory::PAGE_SIZE, (num_pages + 2) * Memory::PAGE_SIZE);
|
|
|
|
ASSERT(result == RESULT_SUCCESS);
|
|
|
|
|
|
|
|
mapped_buffer_context.erase(found);
|
2017-11-07 20:35:17 +01:00
|
|
|
|
|
|
|
i += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr target_address = 0;
|
|
|
|
|
|
|
|
// TODO(Subv): Perform permission checks.
|
|
|
|
|
2018-10-01 03:21:51 +02:00
|
|
|
// Reserve a page of memory before the mapped buffer
|
2020-01-03 18:19:59 +01:00
|
|
|
auto reserve_buffer = std::vector<u8>(Memory::PAGE_SIZE);
|
2018-11-08 20:05:54 +01:00
|
|
|
dst_process->vm_manager.MapBackingMemoryToBase(
|
2020-01-03 18:19:59 +01:00
|
|
|
Memory::IPC_MAPPING_VADDR, Memory::IPC_MAPPING_SIZE, reserve_buffer.data(),
|
2018-11-08 20:05:54 +01:00
|
|
|
Memory::PAGE_SIZE, Kernel::MemoryState::Reserved);
|
2017-11-07 20:35:17 +01:00
|
|
|
|
2020-01-03 18:19:59 +01:00
|
|
|
auto buffer = std::vector<u8>(num_pages * Memory::PAGE_SIZE);
|
|
|
|
memory.ReadBlock(*src_process, source_address, buffer.data() + page_offset, size);
|
2017-11-07 20:35:17 +01:00
|
|
|
|
2018-10-01 03:21:51 +02:00
|
|
|
// Map the page(s) into the target process' address space.
|
2018-11-08 20:05:54 +01:00
|
|
|
target_address =
|
|
|
|
dst_process->vm_manager
|
|
|
|
.MapBackingMemoryToBase(Memory::IPC_MAPPING_VADDR, Memory::IPC_MAPPING_SIZE,
|
2020-01-03 18:19:59 +01:00
|
|
|
buffer.data(), num_pages * Memory::PAGE_SIZE,
|
2018-11-08 20:05:54 +01:00
|
|
|
Kernel::MemoryState::Shared)
|
|
|
|
.Unwrap();
|
2017-11-07 20:35:17 +01:00
|
|
|
|
|
|
|
cmd_buf[i++] = target_address + page_offset;
|
2018-10-01 03:21:51 +02:00
|
|
|
|
2018-10-02 03:07:25 +02:00
|
|
|
// Reserve a page of memory after the mapped buffer
|
2018-11-08 20:05:54 +01:00
|
|
|
dst_process->vm_manager.MapBackingMemoryToBase(
|
2020-01-03 18:19:59 +01:00
|
|
|
Memory::IPC_MAPPING_VADDR, Memory::IPC_MAPPING_SIZE, reserve_buffer.data(),
|
2018-11-08 20:05:54 +01:00
|
|
|
Memory::PAGE_SIZE, Kernel::MemoryState::Reserved);
|
2018-11-08 19:53:20 +01:00
|
|
|
|
2018-11-17 01:47:16 +01:00
|
|
|
mapped_buffer_context.push_back({permissions, size, source_address,
|
|
|
|
target_address + page_offset, std::move(buffer),
|
|
|
|
std::move(reserve_buffer)});
|
2018-11-08 19:53:20 +01:00
|
|
|
|
2017-11-07 20:35:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-10-02 04:56:43 +02:00
|
|
|
default:
|
2018-03-27 17:37:36 +02:00
|
|
|
UNIMPLEMENTED_MSG("Unsupported handle translation: {:#010X}", descriptor);
|
2017-10-02 04:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 14:19:03 +02:00
|
|
|
if (should_record) {
|
|
|
|
std::vector<u32> translated_cmdbuf{cmd_buf.begin(), cmd_buf.begin() + command_size};
|
|
|
|
if (reply) {
|
|
|
|
kernel.GetIPCRecorder().SetReplyInfo(dst_thread, std::move(untranslated_cmdbuf),
|
|
|
|
std::move(translated_cmdbuf));
|
|
|
|
} else {
|
|
|
|
kernel.GetIPCRecorder().SetRequestInfo(src_thread, std::move(untranslated_cmdbuf),
|
|
|
|
std::move(translated_cmdbuf), dst_thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 21:21:30 +01:00
|
|
|
memory.WriteBlock(*dst_process, dst_address, cmd_buf.data(), command_size * sizeof(u32));
|
2017-10-02 04:56:43 +02:00
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
} // namespace Kernel
|