shared_memory.cpp 6.9 KB

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