Kernel/SharedMemory: Don't take over and unmap the source memory block when creating a shared memory, just reference it.

Also reference the right offset into the backing block for the requested address.
This commit is contained in:
Subv 2017-10-01 13:57:50 -05:00
parent 5ba48c1610
commit b18589ecf7
1 changed files with 12 additions and 15 deletions

View File

@ -55,22 +55,19 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u
Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get()); Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
} }
} else { } else {
// TODO(Subv): What happens if an application tries to create multiple memory blocks
// pointing to the same address?
auto& vm_manager = shared_memory->owner_process->vm_manager; auto& vm_manager = shared_memory->owner_process->vm_manager;
// The memory is already available and mapped in the owner process. // The memory is already available and mapped in the owner process.
auto vma = vm_manager.FindVMA(address)->second; auto vma = vm_manager.FindVMA(address);
// Copy it over to our own storage ASSERT_MSG(vma != vm_manager.vma_map.end(), "Invalid memory address");
shared_memory->backing_block = std::make_shared<std::vector<u8>>( ASSERT_MSG(vma->second.backing_block, "Backing block doesn't exist for address");
vma.backing_block->data() + vma.offset, vma.backing_block->data() + vma.offset + size);
shared_memory->backing_block_offset = 0; // The returned VMA might be a bigger one encompassing the desired address.
// Unmap the existing pages auto vma_offset = address - vma->first;
vm_manager.UnmapRange(address, size); ASSERT_MSG(vma_offset + size <= vma->second.size,
// Map our own block into the address space "Shared memory exceeds bounds of mapped block");
vm_manager.MapMemoryBlock(address, shared_memory->backing_block, 0, size,
MemoryState::Shared); shared_memory->backing_block = vma->second.backing_block;
// Reprotect the block with the new permissions shared_memory->backing_block_offset = vma->second.offset + vma_offset;
vm_manager.ReprotectRange(address, size, ConvertPermissions(permissions));
} }
shared_memory->base_address = address; shared_memory->base_address = address;
@ -184,4 +181,4 @@ u8* SharedMemory::GetPointer(u32 offset) {
return backing_block->data() + backing_block_offset + offset; return backing_block->data() + backing_block_offset + offset;
} }
} // namespace } // namespace Kernel