shared_memory.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/hle/kernel/errors.h"
  7. #include "core/hle/kernel/memory.h"
  8. #include "core/hle/kernel/shared_memory.h"
  9. #include "core/memory.h"
  10. namespace Kernel {
  11. SharedMemory::SharedMemory() {}
  12. SharedMemory::~SharedMemory() {}
  13. SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u64 size,
  14. MemoryPermission permissions,
  15. MemoryPermission other_permissions, VAddr address,
  16. MemoryRegion region, std::string name) {
  17. SharedPtr<SharedMemory> shared_memory(new SharedMemory);
  18. shared_memory->owner_process = owner_process;
  19. shared_memory->name = std::move(name);
  20. shared_memory->size = size;
  21. shared_memory->permissions = permissions;
  22. shared_memory->other_permissions = other_permissions;
  23. if (address == 0) {
  24. // We need to allocate a block from the Linear Heap ourselves.
  25. // We'll manually allocate some memory from the linear heap in the specified region.
  26. MemoryRegionInfo* memory_region = GetMemoryRegion(region);
  27. auto& linheap_memory = memory_region->linear_heap_memory;
  28. ASSERT_MSG(linheap_memory->size() + size <= memory_region->size,
  29. "Not enough space in region to allocate shared memory!");
  30. shared_memory->backing_block = linheap_memory;
  31. shared_memory->backing_block_offset = linheap_memory->size();
  32. // Allocate some memory from the end of the linear heap for this region.
  33. linheap_memory->insert(linheap_memory->end(), size, 0);
  34. memory_region->used += size;
  35. shared_memory->linear_heap_phys_address =
  36. Memory::FCRAM_PADDR + memory_region->base +
  37. static_cast<PAddr>(shared_memory->backing_block_offset);
  38. // Increase the amount of used linear heap memory for the owner process.
  39. if (shared_memory->owner_process != nullptr) {
  40. shared_memory->owner_process->linear_heap_used += size;
  41. }
  42. // Refresh the address mappings for the current process.
  43. if (Kernel::g_current_process != nullptr) {
  44. Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
  45. }
  46. } else {
  47. auto& vm_manager = shared_memory->owner_process->vm_manager;
  48. // The memory is already available and mapped in the owner process.
  49. auto vma = vm_manager.FindVMA(address);
  50. ASSERT_MSG(vma != vm_manager.vma_map.end(), "Invalid memory address");
  51. ASSERT_MSG(vma->second.backing_block, "Backing block doesn't exist for address");
  52. // The returned VMA might be a bigger one encompassing the desired address.
  53. auto vma_offset = address - vma->first;
  54. ASSERT_MSG(vma_offset + size <= vma->second.size,
  55. "Shared memory exceeds bounds of mapped block");
  56. shared_memory->backing_block = vma->second.backing_block;
  57. shared_memory->backing_block_offset = vma->second.offset + vma_offset;
  58. }
  59. shared_memory->base_address = address;
  60. return shared_memory;
  61. }
  62. SharedPtr<SharedMemory> SharedMemory::CreateForApplet(std::shared_ptr<std::vector<u8>> heap_block,
  63. u32 offset, u32 size,
  64. MemoryPermission permissions,
  65. MemoryPermission other_permissions,
  66. std::string name) {
  67. SharedPtr<SharedMemory> shared_memory(new SharedMemory);
  68. shared_memory->owner_process = nullptr;
  69. shared_memory->name = std::move(name);
  70. shared_memory->size = size;
  71. shared_memory->permissions = permissions;
  72. shared_memory->other_permissions = other_permissions;
  73. shared_memory->backing_block = heap_block;
  74. shared_memory->backing_block_offset = offset;
  75. shared_memory->base_address = Memory::HEAP_VADDR + offset;
  76. return shared_memory;
  77. }
  78. ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions,
  79. MemoryPermission other_permissions) {
  80. MemoryPermission own_other_permissions =
  81. target_process == owner_process ? this->permissions : this->other_permissions;
  82. // Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
  83. if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
  84. return ERR_INVALID_COMBINATION;
  85. }
  86. // Error out if the requested permissions don't match what the creator process allows.
  87. if (static_cast<u32>(permissions) & ~static_cast<u32>(own_other_permissions)) {
  88. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, permissions don't match",
  89. GetObjectId(), address, name.c_str());
  90. return ERR_INVALID_COMBINATION;
  91. }
  92. // Heap-backed memory blocks can not be mapped with other_permissions = DontCare
  93. if (base_address != 0 && other_permissions == MemoryPermission::DontCare) {
  94. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, permissions don't match",
  95. GetObjectId(), address, name.c_str());
  96. return ERR_INVALID_COMBINATION;
  97. }
  98. // Error out if the provided permissions are not compatible with what the creator process needs.
  99. if (other_permissions != MemoryPermission::DontCare &&
  100. static_cast<u32>(this->permissions) & ~static_cast<u32>(other_permissions)) {
  101. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, permissions don't match",
  102. GetObjectId(), address, name.c_str());
  103. return ERR_WRONG_PERMISSION;
  104. }
  105. // TODO(Subv): Check for the Shared Device Mem flag in the creator process.
  106. /*if (was_created_with_shared_device_mem && address != 0) {
  107. return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS,
  108. ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  109. }*/
  110. // TODO(Subv): The same process that created a SharedMemory object
  111. // can not map it in its own address space unless it was created with addr=0, result 0xD900182C.
  112. if (address != 0) {
  113. // TODO(shinyquagsire23): Check for virtual/mappable memory here too?
  114. if (address >= Memory::HEAP_VADDR && address < Memory::HEAP_VADDR_END) {
  115. LOG_ERROR(Kernel, "cannot map id=%u, address=0x%llx name=%s, invalid address",
  116. GetObjectId(), address, name.c_str());
  117. return ERR_INVALID_ADDRESS;
  118. }
  119. }
  120. VAddr target_address = address;
  121. if (base_address == 0 && target_address == 0) {
  122. // Calculate the address at which to map the memory block.
  123. target_address = Memory::PhysicalToVirtualAddress(linear_heap_phys_address).value();
  124. }
  125. // Map the memory block into the target process
  126. auto result = target_process->vm_manager.MapMemoryBlock(
  127. target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
  128. if (result.Failed()) {
  129. LOG_ERROR(
  130. Kernel,
  131. "cannot map id=%u, target_address=0x%llx name=%s, error mapping to virtual memory",
  132. GetObjectId(), target_address, name.c_str());
  133. return result.Code();
  134. }
  135. return target_process->vm_manager.ReprotectRange(target_address, size,
  136. ConvertPermissions(permissions));
  137. }
  138. ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) {
  139. // TODO(Subv): Verify what happens if the application tries to unmap an address that is not
  140. // mapped to a SharedMemory.
  141. return target_process->vm_manager.UnmapRange(address, size);
  142. }
  143. VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
  144. u32 masked_permissions =
  145. static_cast<u32>(permission) & static_cast<u32>(MemoryPermission::ReadWriteExecute);
  146. return static_cast<VMAPermission>(masked_permissions);
  147. };
  148. u8* SharedMemory::GetPointer(u32 offset) {
  149. return backing_block->data() + backing_block_offset + offset;
  150. }
  151. } // namespace Kernel