Skip to content

Commit

Permalink
kernel: Fix memory mapping issue introduced in https://github.com/cit…
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveice10 authored Nov 26, 2023
1 parent 670e993 commit dc8425a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/core/hle/kernel/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,15 @@ ResultCode Process::HeapFree(VAddr target, u32 size) {

// Free heaps block by block
CASCADE_RESULT(auto backing_blocks, vm_manager.GetBackingBlocksForRange(target, size));
for (const auto& backing_block : backing_blocks) {
memory_region->Free(backing_block.lower(), backing_block.upper() - backing_block.lower());
for (const auto& [backing_memory, block_size] : backing_blocks) {
const auto backing_offset = kernel.memory.GetFCRAMOffset(backing_memory.GetPtr());
memory_region->Free(backing_offset, block_size);
holding_memory -= MemoryRegionInfo::Interval(backing_offset, backing_offset + block_size);
}

ResultCode result = vm_manager.UnmapRange(target, size);
ASSERT(result.IsSuccess());

holding_memory -= backing_blocks;
memory_used -= size;
resource_limit->current_commit -= size;

Expand Down Expand Up @@ -504,9 +505,7 @@ ResultCode Process::Map(VAddr target, VAddr source, u32 size, VMAPermission perm

CASCADE_RESULT(auto backing_blocks, vm_manager.GetBackingBlocksForRange(source, size));
VAddr interval_target = target;
for (const auto& backing_block : backing_blocks) {
auto backing_memory = kernel.memory.GetFCRAMRef(backing_block.lower());
auto block_size = backing_block.upper() - backing_block.lower();
for (const auto& [backing_memory, block_size] : backing_blocks) {
auto target_vma =
vm_manager.MapBackingMemory(interval_target, backing_memory, block_size, target_state);
ASSERT(target_vma.Succeeded());
Expand Down
5 changes: 1 addition & 4 deletions src/core/hle/kernel/shared_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ ResultVal<std::shared_ptr<SharedMemory>> KernelSystem::CreateSharedMemory(

auto backing_blocks = vm_manager.GetBackingBlocksForRange(address, size);
ASSERT(backing_blocks.Succeeded()); // should success after verifying memory state above
for (const auto& interval : backing_blocks.Unwrap()) {
shared_memory->backing_blocks.emplace_back(memory.GetFCRAMRef(interval.lower()),
interval.upper() - interval.lower());
}
shared_memory->backing_blocks = std::move(backing_blocks).Unwrap();
}

shared_memory->base_address = address;
Expand Down
12 changes: 5 additions & 7 deletions src/core/hle/kernel/vm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
plgldr->OnMemoryChanged(process, Core::System::GetInstance().Kernel());
}

ResultVal<MemoryRegionInfo::IntervalSet> VMManager::GetBackingBlocksForRange(VAddr address,
u32 size) {
MemoryRegionInfo::IntervalSet backing_blocks;
ResultVal<std::vector<std::pair<MemoryRef, u32>>> VMManager::GetBackingBlocksForRange(VAddr address,
u32 size) {
std::vector<std::pair<MemoryRef, u32>> backing_blocks;
VAddr interval_target = address;
while (interval_target != address + size) {
auto vma = FindVMA(interval_target);
Expand All @@ -404,10 +404,8 @@ ResultVal<MemoryRegionInfo::IntervalSet> VMManager::GetBackingBlocksForRange(VAd

VAddr interval_end = std::min(address + size, vma->second.base + vma->second.size);
u32 interval_size = interval_end - interval_target;
auto backing_memory = memory.GetFCRAMOffset(vma->second.backing_memory +
(interval_target - vma->second.base));
backing_blocks +=
MemoryRegionInfo::Interval(backing_memory, backing_memory + interval_size);
auto backing_memory = vma->second.backing_memory + (interval_target - vma->second.base);
backing_blocks.push_back({backing_memory, interval_size});

interval_target += interval_size;
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/hle/kernel/vm_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ class VMManager final {
void LogLayout(Common::Log::Level log_level) const;

/// Gets a list of backing memory blocks for the specified range
ResultVal<MemoryRegionInfo::IntervalSet> GetBackingBlocksForRange(VAddr address, u32 size);
ResultVal<std::vector<std::pair<MemoryRef, u32>>> GetBackingBlocksForRange(VAddr address,
u32 size);

/// Each VMManager has its own page table, which is set as the main one when the owning process
/// is scheduled.
Expand Down

0 comments on commit dc8425a

Please sign in to comment.