shared_memory.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "common/logging/log.h"
  6. #include "core/memory.h"
  7. #include "core/hle/kernel/shared_memory.h"
  8. namespace Kernel {
  9. SharedMemory::SharedMemory() {}
  10. SharedMemory::~SharedMemory() {}
  11. SharedPtr<SharedMemory> SharedMemory::Create(u32 size, MemoryPermission permissions,
  12. MemoryPermission other_permissions, std::string name) {
  13. SharedPtr<SharedMemory> shared_memory(new SharedMemory);
  14. shared_memory->name = std::move(name);
  15. shared_memory->base_address = 0x0;
  16. shared_memory->fixed_address = 0x0;
  17. shared_memory->size = size;
  18. shared_memory->permissions = permissions;
  19. shared_memory->other_permissions = other_permissions;
  20. return shared_memory;
  21. }
  22. ResultCode SharedMemory::Map(VAddr address, MemoryPermission permissions,
  23. MemoryPermission other_permissions) {
  24. if (base_address != 0) {
  25. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s: already mapped at 0x%08X!",
  26. GetObjectId(), address, name.c_str(), base_address);
  27. // TODO: Verify error code with hardware
  28. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  29. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  30. }
  31. if (fixed_address != 0) {
  32. if (address != 0 && address != fixed_address) {
  33. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s: fixed_addres is 0x%08X!",
  34. GetObjectId(), address, name.c_str(), fixed_address);
  35. // TODO: Verify error code with hardware
  36. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  37. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  38. }
  39. // HACK(yuriks): This is only here to support the APT shared font mapping right now.
  40. // Later, this should actually map the memory block onto the address space.
  41. return RESULT_SUCCESS;
  42. }
  43. if (address < Memory::SHARED_MEMORY_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) {
  44. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s outside of shared mem bounds!",
  45. GetObjectId(), address, name.c_str());
  46. // TODO: Verify error code with hardware
  47. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  48. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  49. }
  50. // TODO: Test permissions
  51. // HACK: Since there's no way to write to the memory block without mapping it onto the game
  52. // process yet, at least initialize memory the first time it's mapped.
  53. if (address != this->base_address) {
  54. std::memset(Memory::GetPointer(address), 0, size);
  55. }
  56. this->base_address = address;
  57. return RESULT_SUCCESS;
  58. }
  59. u8* SharedMemory::GetPointer(u32 offset) {
  60. if (base_address != 0)
  61. return Memory::GetPointer(base_address + offset);
  62. LOG_ERROR(Kernel_SVC, "memory block id=%u not mapped!", GetObjectId());
  63. return nullptr;
  64. }
  65. } // namespace