shared_memory.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/memory.h"
  8. #include "core/hle/kernel/shared_memory.h"
  9. namespace Kernel {
  10. SharedMemory::SharedMemory() {}
  11. SharedMemory::~SharedMemory() {}
  12. SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u32 size, MemoryPermission permissions,
  13. MemoryPermission other_permissions, VAddr address, MemoryRegion region, std::string name) {
  14. SharedPtr<SharedMemory> shared_memory(new SharedMemory);
  15. shared_memory->owner_process = owner_process;
  16. shared_memory->name = std::move(name);
  17. shared_memory->size = size;
  18. shared_memory->permissions = permissions;
  19. shared_memory->other_permissions = other_permissions;
  20. if (address == 0) {
  21. // We need to allocate a block from the Linear Heap ourselves.
  22. // We'll manually allocate some memory from the linear heap in the specified region.
  23. MemoryRegionInfo* memory_region = GetMemoryRegion(region);
  24. auto& linheap_memory = memory_region->linear_heap_memory;
  25. ASSERT_MSG(linheap_memory->size() + size <= memory_region->size, "Not enough space in region to allocate shared memory!");
  26. shared_memory->backing_block = linheap_memory;
  27. shared_memory->backing_block_offset = linheap_memory->size();
  28. // Allocate some memory from the end of the linear heap for this region.
  29. linheap_memory->insert(linheap_memory->end(), size, 0);
  30. memory_region->used += size;
  31. shared_memory->linear_heap_phys_address = Memory::FCRAM_PADDR + memory_region->base + shared_memory->backing_block_offset;
  32. // Refresh the address mappings for the current process.
  33. if (Kernel::g_current_process != nullptr) {
  34. Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
  35. }
  36. } else {
  37. // TODO(Subv): What happens if an application tries to create multiple memory blocks pointing to the same address?
  38. auto& vm_manager = shared_memory->owner_process->vm_manager;
  39. // The memory is already available and mapped in the owner process.
  40. auto vma = vm_manager.FindVMA(address)->second;
  41. // Copy it over to our own storage
  42. shared_memory->backing_block = std::make_shared<std::vector<u8>>(vma.backing_block->data() + vma.offset,
  43. vma.backing_block->data() + vma.offset + size);
  44. // Unmap the existing pages
  45. vm_manager.UnmapRange(address, size);
  46. // Map our own block into the address space
  47. vm_manager.MapMemoryBlock(address, shared_memory->backing_block, 0, size, MemoryState::Shared);
  48. // Reprotect the block with the new permissions
  49. vm_manager.ReprotectRange(address, size, ConvertPermissions(permissions));
  50. }
  51. shared_memory->base_address = address;
  52. return shared_memory;
  53. }
  54. ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions,
  55. MemoryPermission other_permissions) {
  56. MemoryPermission own_other_permissions = target_process == owner_process ? this->permissions : this->other_permissions;
  57. // Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
  58. if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
  59. return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  60. }
  61. // Error out if the requested permissions don't match what the creator process allows.
  62. if (static_cast<u32>(permissions) & ~static_cast<u32>(own_other_permissions)) {
  63. return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  64. }
  65. // Heap-backed memory blocks can not be mapped with other_permissions = DontCare
  66. if (base_address != 0 && other_permissions == MemoryPermission::DontCare) {
  67. return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  68. }
  69. // Error out if the provided permissions are not compatible with what the creator process needs.
  70. if (other_permissions != MemoryPermission::DontCare &&
  71. static_cast<u32>(this->permissions) & ~static_cast<u32>(other_permissions)) {
  72. return ResultCode(ErrorDescription::WrongPermission, ErrorModule::OS, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  73. }
  74. // TODO(Subv): Check for the Shared Device Mem flag in the creator process.
  75. /*if (was_created_with_shared_device_mem && address != 0) {
  76. return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  77. }*/
  78. // TODO(Subv): The same process that created a SharedMemory object
  79. // can not map it in its own address space unless it was created with addr=0, result 0xD900182C.
  80. if (address != 0) {
  81. if (address < Memory::HEAP_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) {
  82. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s, invalid address",
  83. GetObjectId(), address, name.c_str());
  84. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS,
  85. ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  86. }
  87. }
  88. VAddr target_address = address;
  89. if (base_address == 0 && target_address == 0) {
  90. // Calculate the address at which to map the memory block.
  91. target_address = Memory::PhysicalToVirtualAddress(linear_heap_phys_address);
  92. }
  93. // Map the memory block into the target process
  94. auto result = target_process->vm_manager.MapMemoryBlock(target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
  95. if (result.Failed())
  96. return result.Code();
  97. return target_process->vm_manager.ReprotectRange(target_address, size, ConvertPermissions(permissions));
  98. }
  99. ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) {
  100. // TODO(Subv): Verify what happens if the application tries to unmap an address that is not mapped to a SharedMemory.
  101. return target_process->vm_manager.UnmapRange(address, size);
  102. }
  103. VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
  104. u32 masked_permissions = static_cast<u32>(permission) & static_cast<u32>(MemoryPermission::ReadWriteExecute);
  105. return static_cast<VMAPermission>(masked_permissions);
  106. };
  107. u8* SharedMemory::GetPointer(u32 offset) {
  108. return backing_block->data() + backing_block_offset + offset;
  109. }
  110. } // namespace