kernel/shared_memory: Make Map() and Unmap() take the target process by reference rather than as a pointer

Both member functions assume the passed in target process will not be
null. Instead of making this assumption implicit, we can change the
functions to be references and enforce this at the type-system level.
This commit is contained in:
Lioncash 2018-11-19 09:05:04 -05:00 committed by fearlessTobi
parent 0f544af89a
commit 86c36cb176
3 changed files with 13 additions and 13 deletions

View File

@ -95,11 +95,11 @@ SharedPtr<SharedMemory> KernelSystem::CreateSharedMemoryForApplet(
return shared_memory;
}
ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions,
ResultCode SharedMemory::Map(Process& target_process, VAddr address, MemoryPermission permissions,
MemoryPermission other_permissions) {
MemoryPermission own_other_permissions =
target_process == owner_process ? this->permissions : this->other_permissions;
&target_process == owner_process ? this->permissions : this->other_permissions;
// Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
@ -155,7 +155,7 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
target_address = linear_heap_phys_offset + Memory::LINEAR_HEAP_VADDR;
}
auto vma = target_process->vm_manager.FindVMA(target_address);
auto vma = target_process.vm_manager.FindVMA(target_address);
if (vma->second.type != VMAType::Free ||
vma->second.base + vma->second.size < target_address + size) {
LOG_ERROR(Kernel,
@ -167,20 +167,20 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
// Map the memory block into the target process
VAddr interval_target = target_address;
for (const auto& interval : backing_blocks) {
auto vma = target_process->vm_manager.MapBackingMemory(
interval_target, interval.first, interval.second, MemoryState::Shared);
auto vma = target_process.vm_manager.MapBackingMemory(interval_target, interval.first,
interval.second, MemoryState::Shared);
ASSERT(vma.Succeeded());
target_process->vm_manager.Reprotect(vma.Unwrap(), ConvertPermissions(permissions));
target_process.vm_manager.Reprotect(vma.Unwrap(), ConvertPermissions(permissions));
interval_target += interval.second;
}
return RESULT_SUCCESS;
}
ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) {
ResultCode SharedMemory::Unmap(Process& target_process, VAddr address) {
// TODO(Subv): Verify what happens if the application tries to unmap an address that is not
// mapped to a SharedMemory.
return target_process->vm_manager.UnmapRange(address, size);
return target_process.vm_manager.UnmapRange(address, size);
}
VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {

View File

@ -52,16 +52,16 @@ public:
* @param permissions Memory block map permissions (specified by SVC field)
* @param other_permissions Memory block map other permissions (specified by SVC field)
*/
ResultCode Map(Process* target_process, VAddr address, MemoryPermission permissions,
ResultCode Map(Process& target_process, VAddr address, MemoryPermission permissions,
MemoryPermission other_permissions);
/**
* Unmaps a shared memory block from the specified address in system memory
* @param target_process Process from which to umap the memory block.
* @param target_process Process from which to unmap the memory block.
* @param address Address in system memory where the shared memory block is mapped
* @return Result code of the unmap operation
*/
ResultCode Unmap(Process* target_process, VAddr address);
ResultCode Unmap(Process& target_process, VAddr address);
/**
* Gets a pointer to the shared memory block

View File

@ -322,7 +322,7 @@ ResultCode SVC::MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 oth
case MemoryPermission::WriteExecute:
case MemoryPermission::ReadWriteExecute:
case MemoryPermission::DontCare:
return shared_memory->Map(kernel.GetCurrentProcess().get(), addr, permissions_type,
return shared_memory->Map(*kernel.GetCurrentProcess(), addr, permissions_type,
static_cast<MemoryPermission>(other_permissions));
default:
LOG_ERROR(Kernel_SVC, "unknown permissions=0x{:08X}", permissions);
@ -341,7 +341,7 @@ ResultCode SVC::UnmapMemoryBlock(Handle handle, u32 addr) {
if (shared_memory == nullptr)
return ERR_INVALID_HANDLE;
return shared_memory->Unmap(current_process.get(), addr);
return shared_memory->Unmap(*current_process, addr);
}
/// Connect to an OS service given the port name, returns the handle to the port to out