diff options
| author | 2015-07-17 23:19:16 -0300 | |
|---|---|---|
| committer | 2015-08-16 01:03:45 -0300 | |
| commit | cdeeecf0807d0005356f30db0f7164c5891a9245 (patch) | |
| tree | e32e6d673cdac358df0abd3d3ece13f37c1c28d5 /src | |
| parent | Memory: Move PAGE_MASK and PAGE_BITS to memory.h (diff) | |
| download | yuzu-cdeeecf0807d0005356f30db0f7164c5891a9245.tar.gz yuzu-cdeeecf0807d0005356f30db0f7164c5891a9245.tar.xz yuzu-cdeeecf0807d0005356f30db0f7164c5891a9245.zip | |
Kernel: Properly implement ControlMemory FREE and COMMIT
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/kernel/process.cpp | 120 | ||||
| -rw-r--r-- | src/core/hle/kernel/process.h | 31 | ||||
| -rw-r--r-- | src/core/hle/kernel/vm_manager.cpp | 91 | ||||
| -rw-r--r-- | src/core/hle/kernel/vm_manager.h | 24 | ||||
| -rw-r--r-- | src/core/hle/svc.cpp | 108 | ||||
| -rw-r--r-- | src/core/mem_map.cpp | 2 |
6 files changed, 338 insertions, 38 deletions
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index ad953cdbf..1db763999 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp | |||
| @@ -36,8 +36,7 @@ SharedPtr<Process> Process::Create(SharedPtr<CodeSet> code_set) { | |||
| 36 | process->codeset = std::move(code_set); | 36 | process->codeset = std::move(code_set); |
| 37 | process->flags.raw = 0; | 37 | process->flags.raw = 0; |
| 38 | process->flags.memory_region = MemoryRegion::APPLICATION; | 38 | process->flags.memory_region = MemoryRegion::APPLICATION; |
| 39 | process->address_space = Common::make_unique<VMManager>(); | 39 | Memory::InitLegacyAddressSpace(process->vm_manager); |
| 40 | Memory::InitLegacyAddressSpace(*process->address_space); | ||
| 41 | 40 | ||
| 42 | return process; | 41 | return process; |
| 43 | } | 42 | } |
| @@ -104,19 +103,130 @@ void Process::ParseKernelCaps(const u32* kernel_caps, size_t len) { | |||
| 104 | 103 | ||
| 105 | void Process::Run(s32 main_thread_priority, u32 stack_size) { | 104 | void Process::Run(s32 main_thread_priority, u32 stack_size) { |
| 106 | auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions, MemoryState memory_state) { | 105 | auto MapSegment = [&](CodeSet::Segment& segment, VMAPermission permissions, MemoryState memory_state) { |
| 107 | auto vma = address_space->MapMemoryBlock(segment.addr, codeset->memory, | 106 | auto vma = vm_manager.MapMemoryBlock(segment.addr, codeset->memory, |
| 108 | segment.offset, segment.size, memory_state).Unwrap(); | 107 | segment.offset, segment.size, memory_state).Unwrap(); |
| 109 | address_space->Reprotect(vma, permissions); | 108 | vm_manager.Reprotect(vma, permissions); |
| 110 | }; | 109 | }; |
| 111 | 110 | ||
| 111 | // Map CodeSet segments | ||
| 112 | MapSegment(codeset->code, VMAPermission::ReadExecute, MemoryState::Code); | 112 | MapSegment(codeset->code, VMAPermission::ReadExecute, MemoryState::Code); |
| 113 | MapSegment(codeset->rodata, VMAPermission::Read, MemoryState::Code); | 113 | MapSegment(codeset->rodata, VMAPermission::Read, MemoryState::Code); |
| 114 | MapSegment(codeset->data, VMAPermission::ReadWrite, MemoryState::Private); | 114 | MapSegment(codeset->data, VMAPermission::ReadWrite, MemoryState::Private); |
| 115 | 115 | ||
| 116 | address_space->LogLayout(Log::Level::Debug); | 116 | // Allocate and map stack |
| 117 | vm_manager.MapMemoryBlock(Memory::HEAP_VADDR_END - stack_size, | ||
| 118 | std::make_shared<std::vector<u8>>(stack_size, 0), 0, stack_size, MemoryState::Locked | ||
| 119 | ).Unwrap(); | ||
| 120 | |||
| 121 | vm_manager.LogLayout(Log::Level::Debug); | ||
| 117 | Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority); | 122 | Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority); |
| 118 | } | 123 | } |
| 119 | 124 | ||
| 125 | ResultVal<VAddr> Process::HeapAllocate(VAddr target, u32 size, VMAPermission perms) { | ||
| 126 | if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END || target + size < target) { | ||
| 127 | return ERR_INVALID_ADDRESS; | ||
| 128 | } | ||
| 129 | |||
| 130 | if (heap_memory == nullptr) { | ||
| 131 | // Initialize heap | ||
| 132 | heap_memory = std::make_shared<std::vector<u8>>(); | ||
| 133 | heap_start = heap_end = target; | ||
| 134 | } | ||
| 135 | |||
| 136 | // If necessary, expand backing vector to cover new heap extents. | ||
| 137 | if (target < heap_start) { | ||
| 138 | heap_memory->insert(begin(*heap_memory), heap_start - target, 0); | ||
| 139 | heap_start = target; | ||
| 140 | vm_manager.RefreshMemoryBlockMappings(heap_memory.get()); | ||
| 141 | } | ||
| 142 | if (target + size > heap_end) { | ||
| 143 | heap_memory->insert(end(*heap_memory), (target + size) - heap_end, 0); | ||
| 144 | heap_end = target + size; | ||
| 145 | vm_manager.RefreshMemoryBlockMappings(heap_memory.get()); | ||
| 146 | } | ||
| 147 | ASSERT(heap_end - heap_start == heap_memory->size()); | ||
| 148 | |||
| 149 | CASCADE_RESULT(auto vma, vm_manager.MapMemoryBlock(target, heap_memory, target - heap_start, size, MemoryState::Private)); | ||
| 150 | vm_manager.Reprotect(vma, perms); | ||
| 151 | |||
| 152 | return MakeResult<VAddr>(heap_end - size); | ||
| 153 | } | ||
| 154 | |||
| 155 | ResultCode Process::HeapFree(VAddr target, u32 size) { | ||
| 156 | if (target < Memory::HEAP_VADDR || target + size > Memory::HEAP_VADDR_END || target + size < target) { | ||
| 157 | return ERR_INVALID_ADDRESS; | ||
| 158 | } | ||
| 159 | |||
| 160 | ResultCode result = vm_manager.UnmapRange(target, size); | ||
| 161 | if (result.IsError()) return result; | ||
| 162 | |||
| 163 | return RESULT_SUCCESS; | ||
| 164 | } | ||
| 165 | |||
| 166 | ResultVal<VAddr> Process::LinearAllocate(VAddr target, u32 size, VMAPermission perms) { | ||
| 167 | if (linear_heap_memory == nullptr) { | ||
| 168 | // Initialize heap | ||
| 169 | linear_heap_memory = std::make_shared<std::vector<u8>>(); | ||
| 170 | } | ||
| 171 | |||
| 172 | VAddr heap_end = Memory::LINEAR_HEAP_VADDR + (u32)linear_heap_memory->size(); | ||
| 173 | // Games and homebrew only ever seem to pass 0 here (which lets the kernel decide the address), | ||
| 174 | // but explicit addresses are also accepted and respected. | ||
| 175 | if (target == 0) { | ||
| 176 | target = heap_end; | ||
| 177 | } | ||
| 178 | |||
| 179 | if (target < Memory::LINEAR_HEAP_VADDR || target + size > Memory::LINEAR_HEAP_VADDR_END || | ||
| 180 | target > heap_end || target + size < target) { | ||
| 181 | |||
| 182 | return ERR_INVALID_ADDRESS; | ||
| 183 | } | ||
| 184 | |||
| 185 | // Expansion of the linear heap is only allowed if you do an allocation immediatelly at its | ||
| 186 | // end. It's possible to free gaps in the middle of the heap and then reallocate them later, | ||
| 187 | // but expansions are only allowed at the end. | ||
| 188 | if (target == heap_end) { | ||
| 189 | linear_heap_memory->insert(linear_heap_memory->end(), size, 0); | ||
| 190 | vm_manager.RefreshMemoryBlockMappings(linear_heap_memory.get()); | ||
| 191 | } | ||
| 192 | |||
| 193 | size_t offset = target - Memory::LINEAR_HEAP_VADDR; | ||
| 194 | CASCADE_RESULT(auto vma, vm_manager.MapMemoryBlock(target, linear_heap_memory, offset, size, MemoryState::Continuous)); | ||
| 195 | vm_manager.Reprotect(vma, perms); | ||
| 196 | |||
| 197 | return MakeResult<VAddr>(target); | ||
| 198 | } | ||
| 199 | |||
| 200 | ResultCode Process::LinearFree(VAddr target, u32 size) { | ||
| 201 | if (linear_heap_memory == nullptr || target < Memory::LINEAR_HEAP_VADDR || | ||
| 202 | target + size > Memory::LINEAR_HEAP_VADDR_END || target + size < target) { | ||
| 203 | |||
| 204 | return ERR_INVALID_ADDRESS; | ||
| 205 | } | ||
| 206 | |||
| 207 | VAddr heap_end = Memory::LINEAR_HEAP_VADDR + (u32)linear_heap_memory->size(); | ||
| 208 | if (target + size > heap_end) { | ||
| 209 | return ERR_INVALID_ADDRESS_STATE; | ||
| 210 | } | ||
| 211 | |||
| 212 | ResultCode result = vm_manager.UnmapRange(target, size); | ||
| 213 | if (result.IsError()) return result; | ||
| 214 | |||
| 215 | if (target + size == heap_end) { | ||
| 216 | // End of linear heap has been freed, so check what's the last allocated block in it and | ||
| 217 | // reduce the size. | ||
| 218 | auto vma = vm_manager.FindVMA(target); | ||
| 219 | ASSERT(vma != vm_manager.vma_map.end()); | ||
| 220 | ASSERT(vma->second.type == VMAType::Free); | ||
| 221 | VAddr new_end = vma->second.base; | ||
| 222 | if (new_end >= Memory::LINEAR_HEAP_VADDR) { | ||
| 223 | linear_heap_memory->resize(new_end - Memory::LINEAR_HEAP_VADDR); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | return RESULT_SUCCESS; | ||
| 228 | } | ||
| 229 | |||
| 120 | Kernel::Process::Process() {} | 230 | Kernel::Process::Process() {} |
| 121 | Kernel::Process::~Process() {} | 231 | Kernel::Process::~Process() {} |
| 122 | 232 | ||
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 83d3aceae..567d5df18 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "common/common_types.h" | 15 | #include "common/common_types.h" |
| 16 | 16 | ||
| 17 | #include "core/hle/kernel/kernel.h" | 17 | #include "core/hle/kernel/kernel.h" |
| 18 | #include "core/hle/kernel/vm_manager.h" | ||
| 18 | 19 | ||
| 19 | namespace Kernel { | 20 | namespace Kernel { |
| 20 | 21 | ||
| @@ -48,7 +49,6 @@ union ProcessFlags { | |||
| 48 | }; | 49 | }; |
| 49 | 50 | ||
| 50 | class ResourceLimit; | 51 | class ResourceLimit; |
| 51 | class VMManager; | ||
| 52 | 52 | ||
| 53 | struct CodeSet final : public Object { | 53 | struct CodeSet final : public Object { |
| 54 | static SharedPtr<CodeSet> Create(std::string name, u64 program_id); | 54 | static SharedPtr<CodeSet> Create(std::string name, u64 program_id); |
| @@ -108,10 +108,6 @@ public: | |||
| 108 | /// The id of this process | 108 | /// The id of this process |
| 109 | u32 process_id = next_process_id++; | 109 | u32 process_id = next_process_id++; |
| 110 | 110 | ||
| 111 | /// Bitmask of the used TLS slots | ||
| 112 | std::bitset<300> used_tls_slots; | ||
| 113 | std::unique_ptr<VMManager> address_space; | ||
| 114 | |||
| 115 | /** | 111 | /** |
| 116 | * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them | 112 | * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them |
| 117 | * to this process. | 113 | * to this process. |
| @@ -123,6 +119,31 @@ public: | |||
| 123 | */ | 119 | */ |
| 124 | void Run(s32 main_thread_priority, u32 stack_size); | 120 | void Run(s32 main_thread_priority, u32 stack_size); |
| 125 | 121 | ||
| 122 | |||
| 123 | /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 124 | // Memory Management | ||
| 125 | |||
| 126 | VMManager vm_manager; | ||
| 127 | |||
| 128 | // Memory used to back the allocations in the regular heap. A single vector is used to cover | ||
| 129 | // the entire virtual address space extents that bound the allocations, including any holes. | ||
| 130 | // This makes deallocation and reallocation of holes fast and keeps process memory contiguous | ||
| 131 | // in the emulator address space, allowing Memory::GetPointer to be reasonably safe. | ||
| 132 | std::shared_ptr<std::vector<u8>> heap_memory; | ||
| 133 | // The left/right bounds of the address space covered by heap_memory. | ||
| 134 | VAddr heap_start = 0, heap_end = 0; | ||
| 135 | |||
| 136 | std::shared_ptr<std::vector<u8>> linear_heap_memory; | ||
| 137 | |||
| 138 | /// Bitmask of the used TLS slots | ||
| 139 | std::bitset<300> used_tls_slots; | ||
| 140 | |||
| 141 | ResultVal<VAddr> HeapAllocate(VAddr target, u32 size, VMAPermission perms); | ||
| 142 | ResultCode HeapFree(VAddr target, u32 size); | ||
| 143 | |||
| 144 | ResultVal<VAddr> LinearAllocate(VAddr target, u32 size, VMAPermission perms); | ||
| 145 | ResultCode LinearFree(VAddr target, u32 size); | ||
| 146 | |||
| 126 | private: | 147 | private: |
| 127 | Process(); | 148 | Process(); |
| 128 | ~Process() override; | 149 | ~Process() override; |
diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index 65395476b..2610acf76 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp | |||
| @@ -60,7 +60,11 @@ void VMManager::Reset() { | |||
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | VMManager::VMAHandle VMManager::FindVMA(VAddr target) const { | 62 | VMManager::VMAHandle VMManager::FindVMA(VAddr target) const { |
| 63 | return std::prev(vma_map.upper_bound(target)); | 63 | if (target >= MAX_ADDRESS) { |
| 64 | return vma_map.end(); | ||
| 65 | } else { | ||
| 66 | return std::prev(vma_map.upper_bound(target)); | ||
| 67 | } | ||
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | ResultVal<VMManager::VMAHandle> VMManager::MapMemoryBlock(VAddr target, | 70 | ResultVal<VMManager::VMAHandle> VMManager::MapMemoryBlock(VAddr target, |
| @@ -115,10 +119,8 @@ ResultVal<VMManager::VMAHandle> VMManager::MapMMIO(VAddr target, PAddr paddr, u3 | |||
| 115 | return MakeResult<VMAHandle>(MergeAdjacent(vma_handle)); | 119 | return MakeResult<VMAHandle>(MergeAdjacent(vma_handle)); |
| 116 | } | 120 | } |
| 117 | 121 | ||
| 118 | void VMManager::Unmap(VMAHandle vma_handle) { | 122 | VMManager::VMAIter VMManager::Unmap(VMAIter vma_handle) { |
| 119 | VMAIter iter = StripIterConstness(vma_handle); | 123 | VirtualMemoryArea& vma = vma_handle->second; |
| 120 | |||
| 121 | VirtualMemoryArea& vma = iter->second; | ||
| 122 | vma.type = VMAType::Free; | 124 | vma.type = VMAType::Free; |
| 123 | vma.permissions = VMAPermission::None; | 125 | vma.permissions = VMAPermission::None; |
| 124 | vma.meminfo_state = MemoryState::Free; | 126 | vma.meminfo_state = MemoryState::Free; |
| @@ -130,17 +132,57 @@ void VMManager::Unmap(VMAHandle vma_handle) { | |||
| 130 | 132 | ||
| 131 | UpdatePageTableForVMA(vma); | 133 | UpdatePageTableForVMA(vma); |
| 132 | 134 | ||
| 133 | MergeAdjacent(iter); | 135 | return MergeAdjacent(vma_handle); |
| 136 | } | ||
| 137 | |||
| 138 | ResultCode VMManager::UnmapRange(VAddr target, u32 size) { | ||
| 139 | CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size)); | ||
| 140 | VAddr target_end = target + size; | ||
| 141 | |||
| 142 | VMAIter end = vma_map.end(); | ||
| 143 | // The comparison against the end of the range must be done using addresses since VMAs can be | ||
| 144 | // merged during this process, causing invalidation of the iterators. | ||
| 145 | while (vma != end && vma->second.base < target_end) { | ||
| 146 | vma = std::next(Unmap(vma)); | ||
| 147 | } | ||
| 148 | |||
| 149 | ASSERT(FindVMA(target)->second.size >= size); | ||
| 150 | return RESULT_SUCCESS; | ||
| 134 | } | 151 | } |
| 135 | 152 | ||
| 136 | void VMManager::Reprotect(VMAHandle vma_handle, VMAPermission new_perms) { | 153 | VMManager::VMAHandle VMManager::Reprotect(VMAHandle vma_handle, VMAPermission new_perms) { |
| 137 | VMAIter iter = StripIterConstness(vma_handle); | 154 | VMAIter iter = StripIterConstness(vma_handle); |
| 138 | 155 | ||
| 139 | VirtualMemoryArea& vma = iter->second; | 156 | VirtualMemoryArea& vma = iter->second; |
| 140 | vma.permissions = new_perms; | 157 | vma.permissions = new_perms; |
| 141 | UpdatePageTableForVMA(vma); | 158 | UpdatePageTableForVMA(vma); |
| 142 | 159 | ||
| 143 | MergeAdjacent(iter); | 160 | return MergeAdjacent(iter); |
| 161 | } | ||
| 162 | |||
| 163 | ResultCode VMManager::ReprotectRange(VAddr target, u32 size, VMAPermission new_perms) { | ||
| 164 | CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size)); | ||
| 165 | VAddr target_end = target + size; | ||
| 166 | |||
| 167 | VMAIter end = vma_map.end(); | ||
| 168 | // The comparison against the end of the range must be done using addresses since VMAs can be | ||
| 169 | // merged during this process, causing invalidation of the iterators. | ||
| 170 | while (vma != end && vma->second.base < target_end) { | ||
| 171 | vma = std::next(StripIterConstness(Reprotect(vma, new_perms))); | ||
| 172 | } | ||
| 173 | |||
| 174 | return RESULT_SUCCESS; | ||
| 175 | } | ||
| 176 | |||
| 177 | void VMManager::RefreshMemoryBlockMappings(const std::vector<u8>* block) { | ||
| 178 | // If this ever proves to have a noticeable performance impact, allow users of the function to | ||
| 179 | // specify a specific range of addresses to limit the scan to. | ||
| 180 | for (const auto& p : vma_map) { | ||
| 181 | const VirtualMemoryArea& vma = p.second; | ||
| 182 | if (block == vma.backing_block.get()) { | ||
| 183 | UpdatePageTableForVMA(vma); | ||
| 184 | } | ||
| 185 | } | ||
| 144 | } | 186 | } |
| 145 | 187 | ||
| 146 | void VMManager::LogLayout(Log::Level log_level) const { | 188 | void VMManager::LogLayout(Log::Level log_level) const { |
| @@ -161,8 +203,8 @@ VMManager::VMAIter VMManager::StripIterConstness(const VMAHandle & iter) { | |||
| 161 | } | 203 | } |
| 162 | 204 | ||
| 163 | ResultVal<VMManager::VMAIter> VMManager::CarveVMA(VAddr base, u32 size) { | 205 | ResultVal<VMManager::VMAIter> VMManager::CarveVMA(VAddr base, u32 size) { |
| 164 | ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: %8X", size); | 206 | ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x%8X", size); |
| 165 | ASSERT_MSG((base & Memory::PAGE_MASK) == 0, "non-page aligned base: %08X", base); | 207 | ASSERT_MSG((base & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x%08X", base); |
| 166 | 208 | ||
| 167 | VMAIter vma_handle = StripIterConstness(FindVMA(base)); | 209 | VMAIter vma_handle = StripIterConstness(FindVMA(base)); |
| 168 | if (vma_handle == vma_map.end()) { | 210 | if (vma_handle == vma_map.end()) { |
| @@ -196,6 +238,35 @@ ResultVal<VMManager::VMAIter> VMManager::CarveVMA(VAddr base, u32 size) { | |||
| 196 | return MakeResult<VMAIter>(vma_handle); | 238 | return MakeResult<VMAIter>(vma_handle); |
| 197 | } | 239 | } |
| 198 | 240 | ||
| 241 | ResultVal<VMManager::VMAIter> VMManager::CarveVMARange(VAddr target, u32 size) { | ||
| 242 | ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x%8X", size); | ||
| 243 | ASSERT_MSG((target & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x%08X", target); | ||
| 244 | |||
| 245 | VAddr target_end = target + size; | ||
| 246 | ASSERT(target_end >= target); | ||
| 247 | ASSERT(target_end <= MAX_ADDRESS); | ||
| 248 | ASSERT(size > 0); | ||
| 249 | |||
| 250 | VMAIter begin_vma = StripIterConstness(FindVMA(target)); | ||
| 251 | VMAIter i_end = vma_map.lower_bound(target_end); | ||
| 252 | for (auto i = begin_vma; i != i_end; ++i) { | ||
| 253 | if (i->second.type == VMAType::Free) { | ||
| 254 | return ERR_INVALID_ADDRESS_STATE; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | if (target != begin_vma->second.base) { | ||
| 259 | begin_vma = SplitVMA(begin_vma, target - begin_vma->second.base); | ||
| 260 | } | ||
| 261 | |||
| 262 | VMAIter end_vma = StripIterConstness(FindVMA(target_end)); | ||
| 263 | if (end_vma != vma_map.end() && target_end != end_vma->second.base) { | ||
| 264 | end_vma = SplitVMA(end_vma, target_end - end_vma->second.base); | ||
| 265 | } | ||
| 266 | |||
| 267 | return MakeResult<VMAIter>(begin_vma); | ||
| 268 | } | ||
| 269 | |||
| 199 | VMManager::VMAIter VMManager::SplitVMA(VMAIter vma_handle, u32 offset_in_vma) { | 270 | VMManager::VMAIter VMManager::SplitVMA(VMAIter vma_handle, u32 offset_in_vma) { |
| 200 | VirtualMemoryArea& old_vma = vma_handle->second; | 271 | VirtualMemoryArea& old_vma = vma_handle->second; |
| 201 | VirtualMemoryArea new_vma = old_vma; // Make a copy of the VMA | 272 | VirtualMemoryArea new_vma = old_vma; // Make a copy of the VMA |
diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index 15c10e413..4e95f1f0c 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h | |||
| @@ -171,11 +171,20 @@ public: | |||
| 171 | */ | 171 | */ |
| 172 | ResultVal<VMAHandle> MapMMIO(VAddr target, PAddr paddr, u32 size, MemoryState state); | 172 | ResultVal<VMAHandle> MapMMIO(VAddr target, PAddr paddr, u32 size, MemoryState state); |
| 173 | 173 | ||
| 174 | /// Unmaps the given VMA. | 174 | /// Unmaps a range of addresses, splitting VMAs as necessary. |
| 175 | void Unmap(VMAHandle vma); | 175 | ResultCode UnmapRange(VAddr target, u32 size); |
| 176 | 176 | ||
| 177 | /// Changes the permissions of the given VMA. | 177 | /// Changes the permissions of the given VMA. |
| 178 | void Reprotect(VMAHandle vma, VMAPermission new_perms); | 178 | VMAHandle Reprotect(VMAHandle vma, VMAPermission new_perms); |
| 179 | |||
| 180 | /// Changes the permissions of a range of addresses, splitting VMAs as necessary. | ||
| 181 | ResultCode ReprotectRange(VAddr target, u32 size, VMAPermission new_perms); | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Scans all VMAs and updates the page table range of any that use the given vector as backing | ||
| 185 | * memory. This should be called after any operation that causes reallocation of the vector. | ||
| 186 | */ | ||
| 187 | void RefreshMemoryBlockMappings(const std::vector<u8>* block); | ||
| 179 | 188 | ||
| 180 | /// Dumps the address space layout to the log, for debugging | 189 | /// Dumps the address space layout to the log, for debugging |
| 181 | void LogLayout(Log::Level log_level) const; | 190 | void LogLayout(Log::Level log_level) const; |
| @@ -186,6 +195,9 @@ private: | |||
| 186 | /// Converts a VMAHandle to a mutable VMAIter. | 195 | /// Converts a VMAHandle to a mutable VMAIter. |
| 187 | VMAIter StripIterConstness(const VMAHandle& iter); | 196 | VMAIter StripIterConstness(const VMAHandle& iter); |
| 188 | 197 | ||
| 198 | /// Unmaps the given VMA. | ||
| 199 | VMAIter Unmap(VMAIter vma); | ||
| 200 | |||
| 189 | /** | 201 | /** |
| 190 | * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing | 202 | * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing |
| 191 | * the appropriate error checking. | 203 | * the appropriate error checking. |
| @@ -193,6 +205,12 @@ private: | |||
| 193 | ResultVal<VMAIter> CarveVMA(VAddr base, u32 size); | 205 | ResultVal<VMAIter> CarveVMA(VAddr base, u32 size); |
| 194 | 206 | ||
| 195 | /** | 207 | /** |
| 208 | * Splits the edges of the given range of non-Free VMAs so that there is a VMA split at each | ||
| 209 | * end of the range. | ||
| 210 | */ | ||
| 211 | ResultVal<VMAIter> CarveVMARange(VAddr base, u32 size); | ||
| 212 | |||
| 213 | /** | ||
| 196 | * Splits a VMA in two, at the specified offset. | 214 | * Splits a VMA in two, at the specified offset. |
| 197 | * @returns the right side of the split, with the original iterator becoming the left side. | 215 | * @returns the right side of the split, with the original iterator becoming the left side. |
| 198 | */ | 216 | */ |
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp index bb64fdfb7..91260fe71 100644 --- a/src/core/hle/svc.cpp +++ b/src/core/hle/svc.cpp | |||
| @@ -41,32 +41,114 @@ const ResultCode ERR_NOT_FOUND(ErrorDescription::NotFound, ErrorModule::Kernel, | |||
| 41 | const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS, | 41 | const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS, |
| 42 | ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E0181E | 42 | ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E0181E |
| 43 | 43 | ||
| 44 | const ResultCode ERR_MISALIGNED_ADDRESS{ // 0xE0E01BF1 | ||
| 45 | ErrorDescription::MisalignedAddress, ErrorModule::OS, | ||
| 46 | ErrorSummary::InvalidArgument, ErrorLevel::Usage}; | ||
| 47 | const ResultCode ERR_MISALIGNED_SIZE{ // 0xE0E01BF2 | ||
| 48 | ErrorDescription::MisalignedSize, ErrorModule::OS, | ||
| 49 | ErrorSummary::InvalidArgument, ErrorLevel::Usage}; | ||
| 50 | const ResultCode ERR_INVALID_COMBINATION{ // 0xE0E01BEE | ||
| 51 | ErrorDescription::InvalidCombination, ErrorModule::OS, | ||
| 52 | ErrorSummary::InvalidArgument, ErrorLevel::Usage}; | ||
| 53 | |||
| 44 | enum ControlMemoryOperation { | 54 | enum ControlMemoryOperation { |
| 45 | MEMORY_OPERATION_HEAP = 0x00000003, | 55 | MEMOP_FREE = 1, |
| 46 | MEMORY_OPERATION_GSP_HEAP = 0x00010003, | 56 | MEMOP_RESERVE = 2, // This operation seems to be unsupported in the kernel |
| 57 | MEMOP_COMMIT = 3, | ||
| 58 | MEMOP_MAP = 4, | ||
| 59 | MEMOP_UNMAP = 5, | ||
| 60 | MEMOP_PROTECT = 6, | ||
| 61 | MEMOP_OPERATION_MASK = 0xFF, | ||
| 62 | |||
| 63 | MEMOP_REGION_APP = 0x100, | ||
| 64 | MEMOP_REGION_SYSTEM = 0x200, | ||
| 65 | MEMOP_REGION_BASE = 0x300, | ||
| 66 | MEMOP_REGION_MASK = 0xF00, | ||
| 67 | |||
| 68 | MEMOP_LINEAR = 0x10000, | ||
| 47 | }; | 69 | }; |
| 48 | 70 | ||
| 49 | /// Map application or GSP heap memory | 71 | /// Map application or GSP heap memory |
| 50 | static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) { | 72 | static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) { |
| 51 | LOG_TRACE(Kernel_SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X", | 73 | using namespace Kernel; |
| 74 | |||
| 75 | LOG_DEBUG(Kernel_SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=0x%X, permissions=0x%08X", | ||
| 52 | operation, addr0, addr1, size, permissions); | 76 | operation, addr0, addr1, size, permissions); |
| 53 | 77 | ||
| 54 | switch (operation) { | 78 | if ((addr0 & Memory::PAGE_MASK) != 0 || (addr1 & Memory::PAGE_MASK) != 0) { |
| 79 | return ERR_MISALIGNED_ADDRESS; | ||
| 80 | } | ||
| 81 | if ((size & Memory::PAGE_MASK) != 0) { | ||
| 82 | return ERR_MISALIGNED_SIZE; | ||
| 83 | } | ||
| 84 | |||
| 85 | u32 region = operation & MEMOP_REGION_MASK; | ||
| 86 | operation &= ~MEMOP_REGION_MASK; | ||
| 87 | |||
| 88 | if (region != 0) { | ||
| 89 | LOG_WARNING(Kernel_SVC, "ControlMemory with specified region not supported, region=%X", region); | ||
| 90 | } | ||
| 55 | 91 | ||
| 56 | // Map normal heap memory | 92 | if ((permissions & (u32)MemoryPermission::ReadWrite) != permissions) { |
| 57 | case MEMORY_OPERATION_HEAP: | 93 | return ERR_INVALID_COMBINATION; |
| 58 | *out_addr = Memory::MapBlock_Heap(size, operation, permissions); | 94 | } |
| 95 | VMAPermission vma_permissions = (VMAPermission)permissions; | ||
| 96 | |||
| 97 | auto& process = *g_current_process; | ||
| 98 | |||
| 99 | switch (operation & MEMOP_OPERATION_MASK) { | ||
| 100 | case MEMOP_FREE: | ||
| 101 | { | ||
| 102 | if (addr0 >= Memory::HEAP_VADDR && addr0 < Memory::HEAP_VADDR_END) { | ||
| 103 | ResultCode result = process.HeapFree(addr0, size); | ||
| 104 | if (result.IsError()) return result; | ||
| 105 | } else if (addr0 >= Memory::LINEAR_HEAP_VADDR && addr0 < Memory::LINEAR_HEAP_VADDR_END) { | ||
| 106 | ResultCode result = process.LinearFree(addr0, size); | ||
| 107 | if (result.IsError()) return result; | ||
| 108 | } else { | ||
| 109 | return ERR_INVALID_ADDRESS; | ||
| 110 | } | ||
| 111 | *out_addr = addr0; | ||
| 59 | break; | 112 | break; |
| 113 | } | ||
| 60 | 114 | ||
| 61 | // Map GSP heap memory | 115 | case MEMOP_COMMIT: |
| 62 | case MEMORY_OPERATION_GSP_HEAP: | 116 | { |
| 63 | *out_addr = Memory::MapBlock_HeapLinear(size, operation, permissions); | 117 | if (operation & MEMOP_LINEAR) { |
| 118 | CASCADE_RESULT(*out_addr, process.LinearAllocate(addr0, size, vma_permissions)); | ||
| 119 | } else { | ||
| 120 | CASCADE_RESULT(*out_addr, process.HeapAllocate(addr0, size, vma_permissions)); | ||
| 121 | } | ||
| 64 | break; | 122 | break; |
| 123 | } | ||
| 124 | |||
| 125 | case MEMOP_MAP: // TODO: This is just a hack to avoid regressions until memory aliasing is implemented | ||
| 126 | { | ||
| 127 | CASCADE_RESULT(*out_addr, process.HeapAllocate(addr0, size, vma_permissions)); | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | |||
| 131 | case MEMOP_UNMAP: // TODO: This is just a hack to avoid regressions until memory aliasing is implemented | ||
| 132 | { | ||
| 133 | ResultCode result = process.HeapFree(addr0, size); | ||
| 134 | if (result.IsError()) return result; | ||
| 135 | break; | ||
| 136 | } | ||
| 137 | |||
| 138 | case MEMOP_PROTECT: | ||
| 139 | { | ||
| 140 | ResultCode result = process.vm_manager.ReprotectRange(addr0, size, vma_permissions); | ||
| 141 | if (result.IsError()) return result; | ||
| 142 | break; | ||
| 143 | } | ||
| 65 | 144 | ||
| 66 | // Unknown ControlMemory operation | ||
| 67 | default: | 145 | default: |
| 68 | LOG_ERROR(Kernel_SVC, "unknown operation=0x%08X", operation); | 146 | LOG_ERROR(Kernel_SVC, "unknown operation=0x%08X", operation); |
| 147 | return ERR_INVALID_COMBINATION; | ||
| 69 | } | 148 | } |
| 149 | |||
| 150 | process.vm_manager.LogLayout(Log::Level::Trace); | ||
| 151 | |||
| 70 | return RESULT_SUCCESS; | 152 | return RESULT_SUCCESS; |
| 71 | } | 153 | } |
| 72 | 154 | ||
| @@ -537,9 +619,9 @@ static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* page_inf | |||
| 537 | if (process == nullptr) | 619 | if (process == nullptr) |
| 538 | return ERR_INVALID_HANDLE; | 620 | return ERR_INVALID_HANDLE; |
| 539 | 621 | ||
| 540 | auto vma = process->address_space->FindVMA(addr); | 622 | auto vma = process->vm_manager.FindVMA(addr); |
| 541 | 623 | ||
| 542 | if (vma == process->address_space->vma_map.end()) | 624 | if (vma == Kernel::g_current_process->vm_manager.vma_map.end()) |
| 543 | return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); | 625 | return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); |
| 544 | 626 | ||
| 545 | memory_info->base_address = vma->second.base; | 627 | memory_info->base_address = vma->second.base; |
diff --git a/src/core/mem_map.cpp b/src/core/mem_map.cpp index cbe993fbe..c95dea064 100644 --- a/src/core/mem_map.cpp +++ b/src/core/mem_map.cpp | |||
| @@ -32,9 +32,7 @@ struct MemoryArea { | |||
| 32 | 32 | ||
| 33 | // We don't declare the IO regions in here since its handled by other means. | 33 | // We don't declare the IO regions in here since its handled by other means. |
| 34 | static MemoryArea memory_areas[] = { | 34 | static MemoryArea memory_areas[] = { |
| 35 | {HEAP_VADDR, HEAP_SIZE, "Heap"}, // Application heap (main memory) | ||
| 36 | {SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE, "Shared Memory"}, // Shared memory | 35 | {SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE, "Shared Memory"}, // Shared memory |
| 37 | {LINEAR_HEAP_VADDR, LINEAR_HEAP_SIZE, "Linear Heap"}, // Linear heap (main memory) | ||
| 38 | {VRAM_VADDR, VRAM_SIZE, "VRAM"}, // Video memory (VRAM) | 36 | {VRAM_VADDR, VRAM_SIZE, "VRAM"}, // Video memory (VRAM) |
| 39 | {DSP_RAM_VADDR, DSP_RAM_SIZE, "DSP RAM"}, // DSP memory | 37 | {DSP_RAM_VADDR, DSP_RAM_SIZE, "DSP RAM"}, // DSP memory |
| 40 | {TLS_AREA_VADDR, TLS_AREA_SIZE, "TLS Area"}, // TLS memory | 38 | {TLS_AREA_VADDR, TLS_AREA_SIZE, "TLS Area"}, // TLS memory |