shared_memory.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // TODO(Subv): Return E0E01BEE when permissions and other_permissions don't
  32. // match what was specified when the memory block was created.
  33. // TODO(Subv): Return E0E01BEE when address should be 0.
  34. // Note: Find out when that's the case.
  35. if (fixed_address != 0) {
  36. if (address != 0 && address != fixed_address) {
  37. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s: fixed_addres is 0x%08X!",
  38. GetObjectId(), address, name.c_str(), fixed_address);
  39. // TODO: Verify error code with hardware
  40. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  41. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  42. }
  43. // HACK(yuriks): This is only here to support the APT shared font mapping right now.
  44. // Later, this should actually map the memory block onto the address space.
  45. return RESULT_SUCCESS;
  46. }
  47. if (address < Memory::SHARED_MEMORY_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) {
  48. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s outside of shared mem bounds!",
  49. GetObjectId(), address, name.c_str());
  50. // TODO: Verify error code with hardware
  51. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  52. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  53. }
  54. // TODO: Test permissions
  55. // HACK: Since there's no way to write to the memory block without mapping it onto the game
  56. // process yet, at least initialize memory the first time it's mapped.
  57. if (address != this->base_address) {
  58. std::memset(Memory::GetPointer(address), 0, size);
  59. }
  60. this->base_address = address;
  61. return RESULT_SUCCESS;
  62. }
  63. ResultCode SharedMemory::Unmap(VAddr address) {
  64. if (base_address == 0) {
  65. // TODO(Subv): Verify what actually happens when you want to unmap a memory block that
  66. // was originally mapped with address = 0
  67. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  68. }
  69. if (base_address != address)
  70. return ResultCode(ErrorDescription::WrongAddress, ErrorModule::OS, ErrorSummary::InvalidState, ErrorLevel::Usage);
  71. base_address = 0;
  72. return RESULT_SUCCESS;
  73. }
  74. u8* SharedMemory::GetPointer(u32 offset) {
  75. if (base_address != 0)
  76. return Memory::GetPointer(base_address + offset);
  77. LOG_ERROR(Kernel_SVC, "memory block id=%u not mapped!", GetObjectId());
  78. return nullptr;
  79. }
  80. } // namespace