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"
|
2018-03-13 22:49:59 +01:00
|
|
|
#include "core/core.h"
|
2017-05-21 09:11:36 +02:00
|
|
|
#include "core/hle/kernel/errors.h"
|
2016-04-18 04:07:52 +02:00
|
|
|
#include "core/hle/kernel/memory.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "core/hle/kernel/shared_memory.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/memory.h"
|
2014-07-05 06:55:39 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2016-09-19 03:01:46 +02:00
|
|
|
SharedMemory::SharedMemory() {}
|
|
|
|
SharedMemory::~SharedMemory() {}
|
2015-02-01 01:56:59 +01:00
|
|
|
|
2018-02-03 19:36:54 +01:00
|
|
|
SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u64 size,
|
2016-09-18 02:38:01 +02:00
|
|
|
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;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
ASSERT_MSG(linheap_memory->size() + size <= memory_region->size,
|
|
|
|
"Not enough space in region to allocate shared memory!");
|
2016-04-18 04:07:52 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
shared_memory->linear_heap_phys_address =
|
2017-09-27 01:26:09 +02:00
|
|
|
Memory::FCRAM_PADDR + memory_region->base +
|
|
|
|
static_cast<PAddr>(shared_memory->backing_block_offset);
|
2016-04-18 04:07:52 +02:00
|
|
|
|
2016-04-19 22:56:11 +02:00
|
|
|
// Increase the amount of used linear heap memory for the owner process.
|
|
|
|
if (shared_memory->owner_process != nullptr) {
|
|
|
|
shared_memory->owner_process->linear_heap_used += size;
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
// Refresh the address mappings for the current process.
|
2018-03-13 22:49:59 +01:00
|
|
|
if (Core::CurrentProcess() != nullptr) {
|
|
|
|
Core::CurrentProcess()->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
|
2016-04-18 04:07:52 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto& vm_manager = shared_memory->owner_process->vm_manager;
|
|
|
|
// The memory is already available and mapped in the owner process.
|
2017-10-01 20:57:50 +02:00
|
|
|
auto vma = vm_manager.FindVMA(address);
|
|
|
|
ASSERT_MSG(vma != vm_manager.vma_map.end(), "Invalid memory address");
|
|
|
|
ASSERT_MSG(vma->second.backing_block, "Backing block doesn't exist for address");
|
|
|
|
|
|
|
|
// The returned VMA might be a bigger one encompassing the desired address.
|
|
|
|
auto vma_offset = address - vma->first;
|
|
|
|
ASSERT_MSG(vma_offset + size <= vma->second.size,
|
|
|
|
"Shared memory exceeds bounds of mapped block");
|
|
|
|
|
|
|
|
shared_memory->backing_block = vma->second.backing_block;
|
|
|
|
shared_memory->backing_block_offset = vma->second.offset + vma_offset;
|
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-09-18 02:38:01 +02:00
|
|
|
SharedPtr<SharedMemory> SharedMemory::CreateForApplet(std::shared_ptr<std::vector<u8>> heap_block,
|
|
|
|
u32 offset, u32 size,
|
|
|
|
MemoryPermission permissions,
|
|
|
|
MemoryPermission other_permissions,
|
|
|
|
std::string name) {
|
2016-05-09 00:10:53 +02:00
|
|
|
SharedPtr<SharedMemory> shared_memory(new SharedMemory);
|
|
|
|
|
|
|
|
shared_memory->owner_process = nullptr;
|
|
|
|
shared_memory->name = std::move(name);
|
|
|
|
shared_memory->size = size;
|
|
|
|
shared_memory->permissions = permissions;
|
|
|
|
shared_memory->other_permissions = other_permissions;
|
|
|
|
shared_memory->backing_block = heap_block;
|
|
|
|
shared_memory->backing_block_offset = offset;
|
|
|
|
shared_memory->base_address = Memory::HEAP_VADDR + offset;
|
|
|
|
|
|
|
|
return shared_memory;
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:07:52 +02:00
|
|
|
ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions,
|
2016-09-18 02:38:01 +02:00
|
|
|
MemoryPermission other_permissions) {
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
MemoryPermission own_other_permissions =
|
|
|
|
target_process == owner_process ? this->permissions : this->other_permissions;
|
2016-04-18 04:58:51 +02:00
|
|
|
|
|
|
|
// Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
|
|
|
|
if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
|
2017-05-21 09:11:36 +02:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2016-04-18 04:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2018-01-14 23:17:53 +01:00
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, permissions don't match",
|
2016-04-19 18:59:44 +02:00
|
|
|
GetObjectId(), address, name.c_str());
|
2017-05-21 09:11:36 +02:00
|
|
|
return ERR_INVALID_COMBINATION;
|
2016-04-18 04:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2018-01-14 23:17:53 +01:00
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, permissions don't match",
|
2016-04-19 18:59:44 +02:00
|
|
|
GetObjectId(), address, name.c_str());
|
2017-05-21 09:11:36 +02:00
|
|
|
return ERR_WRONG_PERMISSION;
|
2016-04-18 04:58:51 +02:00
|
|
|
}
|
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) {
|
2018-01-30 07:38:56 +01:00
|
|
|
// TODO(shinyquagsire23): Check for virtual/mappable memory here too?
|
|
|
|
if (address >= Memory::HEAP_VADDR && address < Memory::HEAP_VADDR_END) {
|
2018-01-14 23:17:53 +01:00
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, invalid address",
|
2016-04-18 04:58:51 +02:00
|
|
|
GetObjectId(), address, name.c_str());
|
2017-05-21 09:11:36 +02:00
|
|
|
return ERR_INVALID_ADDRESS;
|
2016-04-18 04:58:51 +02:00
|
|
|
}
|
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.
|
2017-06-22 05:21:49 +02:00
|
|
|
target_address = Memory::PhysicalToVirtualAddress(linear_heap_phys_address).value();
|
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-09-18 02:38:01 +02:00
|
|
|
auto result = target_process->vm_manager.MapMemoryBlock(
|
|
|
|
target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
|
2016-04-19 18:59:44 +02:00
|
|
|
if (result.Failed()) {
|
2016-09-18 02:38:01 +02:00
|
|
|
LOG_ERROR(
|
|
|
|
Kernel,
|
2018-01-14 23:17:53 +01:00
|
|
|
"cannot map id=%u, target_address=0x%llx name=%s, error mapping to virtual memory",
|
2016-09-18 02:38:01 +02:00
|
|
|
GetObjectId(), target_address, name.c_str());
|
2016-04-18 04:58:51 +02:00
|
|
|
return result.Code();
|
2016-04-19 18:59:44 +02:00
|
|
|
}
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2016-09-18 02:38:01 +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) {
|
2016-09-18 02:38:01 +02:00
|
|
|
// TODO(Subv): Verify what happens if the application tries to unmap an address that is not
|
|
|
|
// mapped to a SharedMemory.
|
2016-04-18 04:07:52 +02:00
|
|
|
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) {
|
2016-09-18 02:38:01 +02:00
|
|
|
u32 masked_permissions =
|
|
|
|
static_cast<u32>(permission) & static_cast<u32>(MemoryPermission::ReadWriteExecute);
|
2016-04-18 04:58:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-01 20:57:50 +02:00
|
|
|
} // namespace Kernel
|