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"
|
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() {}
|
|
|
|
|
2015-05-11 00:47:07 +02:00
|
|
|
SharedPtr<SharedMemory> SharedMemory::Create(u32 size, MemoryPermission permissions,
|
|
|
|
MemoryPermission other_permissions, 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
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
shared_memory->name = std::move(name);
|
2015-04-09 02:34:41 +02:00
|
|
|
shared_memory->base_address = 0x0;
|
2015-08-26 08:34:31 +02:00
|
|
|
shared_memory->fixed_address = 0x0;
|
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
|
|
|
|
|
|
|
return shared_memory;
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-11 06:43:29 +01:00
|
|
|
ResultCode SharedMemory::Map(VAddr address, MemoryPermission permissions,
|
|
|
|
MemoryPermission other_permissions) {
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2015-08-26 08:34:31 +02:00
|
|
|
if (base_address != 0) {
|
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s: already mapped at 0x%08X!",
|
|
|
|
GetObjectId(), address, name.c_str(), base_address);
|
|
|
|
// TODO: Verify error code with hardware
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
|
|
|
|
2015-12-31 15:46:32 +01:00
|
|
|
// TODO(Subv): Return E0E01BEE when permissions and other_permissions don't
|
|
|
|
// match what was specified when the memory block was created.
|
|
|
|
|
|
|
|
// TODO(Subv): Return E0E01BEE when address should be 0.
|
|
|
|
// Note: Find out when that's the case.
|
|
|
|
|
2015-08-26 08:34:31 +02:00
|
|
|
if (fixed_address != 0) {
|
|
|
|
if (address != 0 && address != fixed_address) {
|
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s: fixed_addres is 0x%08X!",
|
|
|
|
GetObjectId(), address, name.c_str(), fixed_address);
|
|
|
|
// TODO: Verify error code with hardware
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK(yuriks): This is only here to support the APT shared font mapping right now.
|
|
|
|
// Later, this should actually map the memory block onto the address space.
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-11 00:47:07 +02:00
|
|
|
if (address < Memory::SHARED_MEMORY_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) {
|
2015-08-26 08:34:31 +02:00
|
|
|
LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s outside of shared mem bounds!",
|
|
|
|
GetObjectId(), address, name.c_str());
|
2015-01-11 06:43:29 +01:00
|
|
|
// TODO: Verify error code with hardware
|
2014-10-23 05:20:01 +02:00
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
|
|
|
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
2014-07-05 16:22:03 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 00:47:07 +02:00
|
|
|
// TODO: Test permissions
|
|
|
|
|
2015-05-11 00:49:46 +02:00
|
|
|
// HACK: Since there's no way to write to the memory block without mapping it onto the game
|
|
|
|
// process yet, at least initialize memory the first time it's mapped.
|
|
|
|
if (address != this->base_address) {
|
|
|
|
std::memset(Memory::GetPointer(address), 0, size);
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:37:53 +01:00
|
|
|
this->base_address = address;
|
2014-07-05 16:22:03 +02:00
|
|
|
|
2014-10-23 05:20:01 +02:00
|
|
|
return RESULT_SUCCESS;
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-31 15:46:32 +01:00
|
|
|
ResultCode SharedMemory::Unmap(VAddr address) {
|
|
|
|
if (base_address == 0) {
|
|
|
|
// TODO(Subv): Verify what actually happens when you want to unmap a memory block that
|
|
|
|
// was originally mapped with address = 0
|
|
|
|
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base_address != address)
|
|
|
|
return ResultCode(ErrorDescription::WrongAddress, ErrorModule::OS, ErrorSummary::InvalidState, ErrorLevel::Usage);
|
|
|
|
|
|
|
|
base_address = 0;
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-05-11 00:47:07 +02:00
|
|
|
u8* SharedMemory::GetPointer(u32 offset) {
|
2015-01-11 06:43:29 +01:00
|
|
|
if (base_address != 0)
|
2015-05-11 00:47:07 +02:00
|
|
|
return Memory::GetPointer(base_address + offset);
|
2014-07-05 06:55:39 +02:00
|
|
|
|
2015-01-31 17:55:40 +01:00
|
|
|
LOG_ERROR(Kernel_SVC, "memory block id=%u not mapped!", GetObjectId());
|
2015-05-11 00:47:07 +02:00
|
|
|
return nullptr;
|
2014-07-05 06:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|