1
0
Fork 0
forked from suyu/suyu

Merge pull request #1734 from lioncash/shared

kernel/shared_memory: Make data members private, plus minor interface changes
This commit is contained in:
bunnei 2018-11-20 16:13:30 -08:00 committed by GitHub
commit aa7e53ab5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 33 deletions

View file

@ -61,7 +61,7 @@ SharedPtr<SharedMemory> SharedMemory::Create(KernelCore& kernel, SharedPtr<Proce
} }
SharedPtr<SharedMemory> SharedMemory::CreateForApplet( SharedPtr<SharedMemory> SharedMemory::CreateForApplet(
KernelCore& kernel, std::shared_ptr<std::vector<u8>> heap_block, u32 offset, u32 size, KernelCore& kernel, std::shared_ptr<std::vector<u8>> heap_block, std::size_t offset, u64 size,
MemoryPermission permissions, MemoryPermission other_permissions, std::string name) { MemoryPermission permissions, MemoryPermission other_permissions, std::string name) {
SharedPtr<SharedMemory> shared_memory(new SharedMemory(kernel)); SharedPtr<SharedMemory> shared_memory(new SharedMemory(kernel));
@ -78,10 +78,10 @@ SharedPtr<SharedMemory> SharedMemory::CreateForApplet(
return shared_memory; 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 other_permissions) {
const MemoryPermission own_other_permissions = const 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 // Automatically allocated memory blocks can only be mapped with other_permissions = DontCare
if (base_address == 0 && other_permissions != MemoryPermission::DontCare) { if (base_address == 0 && other_permissions != MemoryPermission::DontCare) {
@ -106,7 +106,7 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
VAddr target_address = address; VAddr target_address = address;
// Map the memory block into the target process // Map the memory block into the target process
auto result = target_process->VMManager().MapMemoryBlock( auto result = target_process.VMManager().MapMemoryBlock(
target_address, backing_block, backing_block_offset, size, MemoryState::Shared); target_address, backing_block, backing_block_offset, size, MemoryState::Shared);
if (result.Failed()) { if (result.Failed()) {
LOG_ERROR( LOG_ERROR(
@ -116,14 +116,14 @@ ResultCode SharedMemory::Map(Process* target_process, VAddr address, MemoryPermi
return result.Code(); return result.Code();
} }
return target_process->VMManager().ReprotectRange(target_address, size, return target_process.VMManager().ReprotectRange(target_address, size,
ConvertPermissions(permissions)); ConvertPermissions(permissions));
} }
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 // TODO(Subv): Verify what happens if the application tries to unmap an address that is not
// mapped to a SharedMemory. // mapped to a SharedMemory.
return target_process->VMManager().UnmapRange(address, size); return target_process.VMManager().UnmapRange(address, size);
} }
VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) { VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
@ -132,7 +132,11 @@ VMAPermission SharedMemory::ConvertPermissions(MemoryPermission permission) {
return static_cast<VMAPermission>(masked_permissions); return static_cast<VMAPermission>(masked_permissions);
} }
u8* SharedMemory::GetPointer(u32 offset) { u8* SharedMemory::GetPointer(std::size_t offset) {
return backing_block->data() + backing_block_offset + offset;
}
const u8* SharedMemory::GetPointer(std::size_t offset) const {
return backing_block->data() + backing_block_offset + offset; return backing_block->data() + backing_block_offset + offset;
} }

View file

@ -64,7 +64,7 @@ public:
*/ */
static SharedPtr<SharedMemory> CreateForApplet(KernelCore& kernel, static SharedPtr<SharedMemory> CreateForApplet(KernelCore& kernel,
std::shared_ptr<std::vector<u8>> heap_block, std::shared_ptr<std::vector<u8>> heap_block,
u32 offset, u32 size, std::size_t offset, u64 size,
MemoryPermission permissions, MemoryPermission permissions,
MemoryPermission other_permissions, MemoryPermission other_permissions,
std::string name = "Unknown Applet"); std::string name = "Unknown Applet");
@ -81,6 +81,11 @@ public:
return HANDLE_TYPE; return HANDLE_TYPE;
} }
/// Gets the size of the underlying memory block in bytes.
u64 GetSize() const {
return size;
}
/** /**
* Converts the specified MemoryPermission into the equivalent VMAPermission. * Converts the specified MemoryPermission into the equivalent VMAPermission.
* @param permission The MemoryPermission to convert. * @param permission The MemoryPermission to convert.
@ -94,44 +99,51 @@ public:
* @param permissions Memory block map permissions (specified by SVC field) * @param permissions Memory block map permissions (specified by SVC field)
* @param other_permissions Memory block map other 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); MemoryPermission other_permissions);
/** /**
* Unmaps a shared memory block from the specified address in system memory * 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 * @param address Address in system memory where the shared memory block is mapped
* @return Result code of the unmap operation * @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 * Gets a pointer to the shared memory block
* @param offset Offset from the start of the shared memory block to get pointer * @param offset Offset from the start of the shared memory block to get pointer
* @return Pointer to the shared memory block from the specified offset * @return A pointer to the shared memory block from the specified offset
*/ */
u8* GetPointer(u32 offset = 0); u8* GetPointer(std::size_t offset = 0);
/// Process that created this shared memory block. /**
SharedPtr<Process> owner_process; * Gets a constant pointer to the shared memory block
/// Address of shared memory block in the owner process if specified. * @param offset Offset from the start of the shared memory block to get pointer
VAddr base_address; * @return A constant pointer to the shared memory block from the specified offset
/// Backing memory for this shared memory block. */
std::shared_ptr<std::vector<u8>> backing_block; const u8* GetPointer(std::size_t offset = 0) const;
/// Offset into the backing block for this shared memory.
std::size_t backing_block_offset;
/// Size of the memory block. Page-aligned.
u64 size;
/// Permission restrictions applied to the process which created the block.
MemoryPermission permissions;
/// Permission restrictions applied to other processes mapping the block.
MemoryPermission other_permissions;
/// Name of shared memory object.
std::string name;
private: private:
explicit SharedMemory(KernelCore& kernel); explicit SharedMemory(KernelCore& kernel);
~SharedMemory() override; ~SharedMemory() override;
/// Backing memory for this shared memory block.
std::shared_ptr<std::vector<u8>> backing_block;
/// Offset into the backing block for this shared memory.
std::size_t backing_block_offset = 0;
/// Size of the memory block. Page-aligned.
u64 size = 0;
/// Permission restrictions applied to the process which created the block.
MemoryPermission permissions{};
/// Permission restrictions applied to other processes mapping the block.
MemoryPermission other_permissions{};
/// Process that created this shared memory block.
SharedPtr<Process> owner_process;
/// Address of shared memory block in the owner process if specified.
VAddr base_address = 0;
/// Name of shared memory object.
std::string name;
}; };
} // namespace Kernel } // namespace Kernel

View file

@ -789,7 +789,7 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s
return ERR_INVALID_MEMORY_RANGE; return ERR_INVALID_MEMORY_RANGE;
} }
return shared_memory->Map(current_process, addr, permissions_type, MemoryPermission::DontCare); return shared_memory->Map(*current_process, addr, permissions_type, MemoryPermission::DontCare);
} }
static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) { static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) {
@ -819,7 +819,7 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64
return ERR_INVALID_MEMORY_RANGE; return ERR_INVALID_MEMORY_RANGE;
} }
return shared_memory->Unmap(current_process, addr); return shared_memory->Unmap(*current_process, addr);
} }
/// Query process memory /// Query process memory