mirror of
				https://git.suyu.dev/suyu/suyu
				synced 2025-10-30 23:49:01 -05:00 
			
		
		
		
	Kernel: Implemented shared memory permissions.
This commit is contained in:
		| @@ -46,6 +46,7 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u | ||||
|             Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get()); | ||||
|         } | ||||
|     } 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; | ||||
|         // The memory is already available and mapped in the owner process. | ||||
|         auto vma = vm_manager.FindVMA(address)->second; | ||||
| @@ -56,6 +57,8 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u | ||||
|         vm_manager.UnmapRange(address, size); | ||||
|         // Map our own block into the address space | ||||
|         vm_manager.MapMemoryBlock(address, shared_memory->backing_block, 0, size, MemoryState::Shared); | ||||
|         // Reprotect the block with the new permissions | ||||
|         vm_manager.ReprotectRange(address, size, ConvertPermissions(permissions)); | ||||
|     } | ||||
|  | ||||
|     shared_memory->base_address = address; | ||||
| @@ -65,8 +68,28 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u | ||||
| ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermission permissions, | ||||
|         MemoryPermission other_permissions) { | ||||
|  | ||||
|     // TODO(Subv): Return E0E01BEE when permissions and other_permissions don't | ||||
|     // match what was specified when the memory block was created. | ||||
|     MemoryPermission own_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) { | ||||
|         return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); | ||||
|     } | ||||
|  | ||||
|     // Error out if the requested permissions don't match what the creator process allows. | ||||
|     if (static_cast<u32>(permissions) & ~static_cast<u32>(own_other_permissions)) { | ||||
|         return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); | ||||
|     } | ||||
|  | ||||
|     // Heap-backed memory blocks can not be mapped with other_permissions = DontCare | ||||
|     if (base_address != 0 && other_permissions == MemoryPermission::DontCare) { | ||||
|         return ResultCode(ErrorDescription::InvalidCombination, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage); | ||||
|     } | ||||
|  | ||||
|     // Error out if the provided permissions are not compatible with what the creator process needs. | ||||
|     if (other_permissions != MemoryPermission::DontCare && | ||||
|         static_cast<u32>(this->permissions) & ~static_cast<u32>(other_permissions)) { | ||||
|         return ResultCode(ErrorDescription::WrongPermission, ErrorModule::OS, ErrorSummary::WrongArgument, ErrorLevel::Permanent); | ||||
|     } | ||||
|  | ||||
|     // TODO(Subv): Check for the Shared Device Mem flag in the creator process. | ||||
|     /*if (was_created_with_shared_device_mem && address != 0) { | ||||
| @@ -76,11 +99,13 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi | ||||
|     // TODO(Subv): The same process that created a SharedMemory object | ||||
|     // can not map it in its own address space unless it was created with addr=0, result 0xD900182C. | ||||
|  | ||||
|     if (address < Memory::HEAP_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) { | ||||
|         LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s, invalid address", | ||||
|                 GetObjectId(), address, name.c_str()); | ||||
|         return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, | ||||
|                 ErrorSummary::InvalidArgument, ErrorLevel::Usage); | ||||
|     if (address != 0) { | ||||
|         if (address < Memory::HEAP_VADDR || address + size >= Memory::SHARED_MEMORY_VADDR_END) { | ||||
|             LOG_ERROR(Kernel, "cannot map id=%u, address=0x%08X name=%s, invalid address", | ||||
|                       GetObjectId(), address, name.c_str()); | ||||
|             return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::OS, | ||||
|                               ErrorSummary::InvalidArgument, ErrorLevel::Usage); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     VAddr target_address = address; | ||||
| @@ -91,9 +116,11 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi | ||||
|     } | ||||
|  | ||||
|     // Map the memory block into the target process | ||||
|     target_process->vm_manager.MapMemoryBlock(target_address, backing_block, backing_block_offset, size, MemoryState::Shared); | ||||
|     auto result = target_process->vm_manager.MapMemoryBlock(target_address, backing_block, backing_block_offset, size, MemoryState::Shared); | ||||
|     if (result.Failed()) | ||||
|         return result.Code(); | ||||
|  | ||||
|     return RESULT_SUCCESS; | ||||
|     return target_process->vm_manager.ReprotectRange(target_address, size, ConvertPermissions(permissions)); | ||||
| } | ||||
|  | ||||
| ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) { | ||||
| @@ -101,6 +128,11 @@ ResultCode SharedMemory::Unmap(Process* target_process, VAddr address) { | ||||
|     return target_process->vm_manager.UnmapRange(address, size); | ||||
| } | ||||
|  | ||||
| VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) { | ||||
|     u32 masked_permissions = static_cast<u32>(permission) & static_cast<u32>(MemoryPermission::ReadWriteExecute); | ||||
|     return static_cast<VMAPermission>(masked_permissions); | ||||
| }; | ||||
|  | ||||
| u8* SharedMemory::GetPointer(u32 offset) { | ||||
|     return backing_block->data() + backing_block_offset + offset; | ||||
| } | ||||
|   | ||||
| @@ -48,6 +48,12 @@ public: | ||||
|     static const HandleType HANDLE_TYPE = HandleType::SharedMemory; | ||||
|     HandleType GetHandleType() const override { return HANDLE_TYPE; } | ||||
|  | ||||
|     /** | ||||
|      * Converts the specified MemoryPermission into the equivalent VMAPermission. | ||||
|      * @param permission The MemoryPermission to convert. | ||||
|      */ | ||||
|     static VMAPermission ConvertPermissions(MemoryPermission permission); | ||||
|  | ||||
|     /** | ||||
|      * Maps a shared memory block to an address in the target process' address space | ||||
|      * @param target_process Process on which to map the memory block. | ||||
|   | ||||
| @@ -17,6 +17,7 @@ | ||||
| /// Detailed description of the error. This listing is likely incomplete. | ||||
| enum class ErrorDescription : u32 { | ||||
|     Success = 0, | ||||
|     WrongPermission = 46, | ||||
|     OS_InvalidBufferDescriptor = 48, | ||||
|     WrongAddress = 53, | ||||
|     FS_NotFound = 120, | ||||
|   | ||||
| @@ -99,6 +99,7 @@ static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 add | ||||
|     switch (operation & MEMOP_OPERATION_MASK) { | ||||
|     case MEMOP_FREE: | ||||
|     { | ||||
|         // TODO(Subv): What happens if an application tries to FREE a block of memory that has a SharedMemory pointing to it? | ||||
|         if (addr0 >= Memory::HEAP_VADDR && addr0 < Memory::HEAP_VADDR_END) { | ||||
|             ResultCode result = process.HeapFree(addr0, size); | ||||
|             if (result.IsError()) return result; | ||||
| @@ -798,6 +799,7 @@ static ResultCode CreateMemoryBlock(Handle* out_handle, u32 addr, u32 size, u32 | ||||
|         case MemoryPermission::Read: | ||||
|         case MemoryPermission::Write: | ||||
|         case MemoryPermission::ReadWrite: | ||||
|         case MemoryPermission::DontCare: | ||||
|             return true; | ||||
|         default: | ||||
|             return false; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Subv
					Subv