2014-07-05 06:55:39 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 09:49:13 +01:00
|
|
|
// Refer to the license.txt file included.
|
2014-07-05 06:55:39 +02:00
|
|
|
|
2015-05-11 00:49:46 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/logging/log.h"
|
2014-07-05 06:55:39 +02:00
|
|
|
|
2015-05-13 03:38:29 +02:00
|
|
|
#include "core/memory.h"
|
2016-04-18 04:07:52 +02:00
|
|
|
#include "core/hle/kernel/memory.h"
|
2014-07-05 06:55:39 +02:00
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2015-02-01 01:56:59 +01:00
|
|
|
SharedMemory::SharedMemory() {}
|
|
|
|
SharedMemory::~SharedMemory() {}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u32 size, MemoryPermission permissions,
|
|
|
|
MemoryPermission other_permissions, VAddr address, MemoryRegion region, std::string name) {
|
2015-01-11 06:43:29 +01:00
|
|
|
SharedPtr<SharedMemory> shared_memory(new SharedMemory);
|
2014-07-05 06:55:39 +02:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
shared_memory->owner_process = owner_process;
|
2015-01-11 06:43:29 +01:00
|
|
|
shared_memory->name = std::move(name);
|
2015-05-11 00:47:07 +02:00
|
|
|
shared_memory->size = size;
|
|
|
|
shared_memory->permissions = permissions;
|
|
|
|
shared_memory->other_permissions = other_permissions;
|
2015-02-01 03:14:40 +01:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
if (address == 0) {
|
|
|
|
// We need to allocate a block from the Linear Heap ourselves.
|
|
|
|
// We'll manually allocate some memory from the linear heap in the specified region.
|
|
|
|
MemoryRegionInfo* memory_region = GetMemoryRegion(region);
|
|
|
|
auto& linheap_memory = memory_region->linear_heap_memory;
|
|
|
|
|
|
|
|
ASSERT_MSG(linheap_memory->size() + size <= memory_region->size, "Not enough space in region to allocate shared memory!");
|
|
|
|
|
|
|
|
shared_memory->backing_block = linheap_memory;
|
|
|
|
shared_memory->backing_block_offset = linheap_memory->size();
|
|
|
|
// Allocate some memory from the end of the linear heap for this region.
|
|
|
|
linheap_memory->insert(linheap_memory->end(), size, 0);
|
|
|
|
memory_region->used += size;
|
|
|
|
|
|
|
|
shared_memory->linear_heap_phys_address = Memory::FCRAM_PADDR + memory_region->base + shared_memory->backing_block_offset;
|
|
|
|
|
|
|
|
// Refresh the address mappings for the current process.
|
|
|
|
if (Kernel::g_current_process != nullptr) {
|
|
|
|
Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-18 04:58:51 +02:00
|
|
|
// TODO(Subv): What happens if an application tries to create multiple memory blocks pointing to the same address?
|
2016-04-18 04:07:52 +02:00
|
|
|
auto& vm_manager = shared_memory->owner_process->vm_manager;
|
|
|
|
// The memory is already available and mapped in the owner process.
|
|
|
|
auto vma = vm_manager.FindVMA(address)->second;
|
|
|
|
// Copy it over to our own storage
|
|
|
|
shared_memory->backing_block = std::make_shared<std::vector<u8>>(vma.backing_block->data() + vma.offset,
|
|
|
|
vma.backing_block->data() + vma.offset + size);
|
|
|
|
// Unmap the existing pages
|
|
|
|
vm_manager.UnmapRange(address, size);
|
|
|
|
// Map our own block into the address space
|
|
|
|
vm_manager.MapMemoryBlock(address, shared_memory->backing_block, 0, size, MemoryState::Shared);
|
2016-04-18 04:58:51 +02:00
|
|
|
// Reprotect the block with the new permissions
|
|
|
|
vm_manager.ReprotectRange(address, size, ConvertPermissions(permissions));
|
2016-04-18 04:07:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_memory->base_address = address;
|
2015-02-01 03:14:40 +01:00
|
|
|
return shared_memory;
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions,
|
2015-01-11 06:43:29 +01:00
|
|
|
MemoryPermission other_permissions) {
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2016-04-18 04:58:51 +02:00
|
|
|
MemoryPermission own_other_permissions = target_process == owner_process ? this->permissions : this->other_permissions;
|
|
|
|
|
|
|
|
// Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
|
|
|
|
if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
|
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if the requested permissions don't match what the creator process allows.
|
|
|
|
if (static_cast<u32>(permissions) & ~static_cast<u32>(own_other_permissions)) {
|
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heap-backed memory blocks can not be mapped with other_permissions = DontCare
|
|
|
|
if (base_address != 0 && other_permissions == MemoryPermission::DontCare) {
|
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if the provided permissions are not compatible with what the creator process needs.
|
|
|
|
if (other_permissions != MemoryPermission::DontCare &&
|
|
|
|
static_cast<u32>(this->permissions) & ~static_cast<u32>(other_permissions)) {
|
|
|
|
return ResultCode(ErrorDescription::WrongPermission, ErrorModule::OS, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
// TODO(Subv): Check for the Shared Device Mem flag in the creator process.
|
|
|
|
/*if (was_created_with_shared_device_mem && address != 0) {
|
|
|
|
return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}*/
|
2015-12-31 15:46:32 +01:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
// TODO(Subv): The same process that created a SharedMemory object
|
|
|
|
// can not map it in its own address space unless it was created with addr=0, result 0xD900182C.
|
2015-08-26 08:34:31 +02:00
|
|
|
|
2016-04-18 04:58:51 +02:00
|
|
|
if (address != 0) {
|
|
|
|
if (address < Memory::HEAP_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) {
|
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s, invalid address",
|
|
|
|
GetObjectId(), address, name.c_str());
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
2014-07-05 16:22:03 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
VAddr target_address = address;
|
2015-05-11 00:47:07 +02:00
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
if (base_address == 0 && target_address == 0) {
|
|
|
|
// Calculate the address at which to map the memory block.
|
|
|
|
target_address = Memory::PhysicalToVirtualAddress(linear_heap_phys_address);
|
2015-05-11 00:49:46 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
// Map the memory block into the target process
|
2016-04-18 04:58:51 +02:00
|
|
|
auto result = target_process->vm_manager.MapMemoryBlock(target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
|
|
|
|
if (result.Failed())
|
|
|
|
return result.Code();
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2016-04-18 04:58:51 +02:00
|
|
|
return target_process->vm_manager.ReprotectRange(target_address, size, ConvertPermissions(permissions));
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) {
|
|
|
|
// TODO(Subv): Verify what happens if the application tries to unmap an address that is not mapped to a SharedMemory.
|
|
|
|
return target_process->vm_manager.UnmapRange(address, size);
|
2015-12-31 15:46:32 +01:00
|
|
|
}
|
|
|
|
|
2016-04-18 04:58:51 +02:00
|
|
|
VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
|
|
|
|
u32 masked_permissions = static_cast<u32>(permission) & static_cast<u32>(MemoryPermission::ReadWriteExecute);
|
|
|
|
return static_cast<VMAPermission>(masked_permissions);
|
|
|
|
};
|
|
|
|
|
2015-05-11 00:47:07 +02:00
|
|
|
u8* SharedMemory::GetPointer(u32 offset) {
|
2016-04-18 04:07:52 +02:00
|
|
|
return backing_block->data() + backing_block_offset + offset;
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|