Kernel: Convert SharedMemory to not use Handles
This commit is contained in:
parent
fc11aff955
commit
4bb33dfc30
8 changed files with 105 additions and 100 deletions
|
@ -9,68 +9,39 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
class SharedMemory : public Object {
|
||||
public:
|
||||
std::string GetTypeName() const override { return "SharedMemory"; }
|
||||
ResultVal<SharedPtr<SharedMemory>> SharedMemory::Create(std::string name) {
|
||||
SharedPtr<SharedMemory> shared_memory(new SharedMemory);
|
||||
|
||||
static const HandleType HANDLE_TYPE = HandleType::SharedMemory;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
// TOOD(yuriks): Don't create Handle (see Thread::Create())
|
||||
CASCADE_RESULT(auto unused, Kernel::g_handle_table.Create(shared_memory));
|
||||
|
||||
u32 base_address; ///< Address of shared memory block in RAM
|
||||
MemoryPermission permissions; ///< Permissions of shared memory block (SVC field)
|
||||
MemoryPermission other_permissions; ///< Other permissions of shared memory block (SVC field)
|
||||
std::string name; ///< Name of shared memory object (optional)
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Creates a shared memory object
|
||||
* @param handle Handle of newly created shared memory object
|
||||
* @param name Name of shared memory object
|
||||
* @return Pointer to newly created shared memory object
|
||||
*/
|
||||
static SharedMemory* CreateSharedMemory(Handle& handle, const std::string& name) {
|
||||
SharedMemory* shared_memory = new SharedMemory;
|
||||
// TOOD(yuriks): Fix error reporting
|
||||
handle = Kernel::g_handle_table.Create(shared_memory).ValueOr(INVALID_HANDLE);
|
||||
shared_memory->name = name;
|
||||
return shared_memory;
|
||||
shared_memory->name = std::move(name);
|
||||
return MakeResult<SharedPtr<SharedMemory>>(std::move(shared_memory));
|
||||
}
|
||||
|
||||
Handle CreateSharedMemory(const std::string& name) {
|
||||
Handle handle;
|
||||
CreateSharedMemory(handle, name);
|
||||
return handle;
|
||||
}
|
||||
|
||||
ResultCode MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions,
|
||||
MemoryPermission other_permissions) {
|
||||
ResultCode SharedMemory::Map(VAddr address, MemoryPermission permissions,
|
||||
MemoryPermission other_permissions) {
|
||||
|
||||
if (address < Memory::SHARED_MEMORY_VADDR || address >= Memory::SHARED_MEMORY_VADDR_END) {
|
||||
LOG_ERROR(Kernel_SVC, "cannot map handle=0x%08X, address=0x%08X outside of shared mem bounds!",
|
||||
handle, address);
|
||||
LOG_ERROR(Kernel, "cannot map handle=0x%08X, address=0x%08X outside of shared mem bounds!",
|
||||
GetHandle(), address);
|
||||
// TODO: Verify error code with hardware
|
||||
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
||||
ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
||||
}
|
||||
SharedMemory* shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle).get();
|
||||
if (shared_memory == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
|
||||
shared_memory->base_address = address;
|
||||
shared_memory->permissions = permissions;
|
||||
shared_memory->other_permissions = other_permissions;
|
||||
base_address = address;
|
||||
permissions = permissions;
|
||||
other_permissions = other_permissions;
|
||||
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ResultVal<u8*> GetSharedMemoryPointer(Handle handle, u32 offset) {
|
||||
SharedMemory* shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle).get();
|
||||
if (shared_memory == nullptr) return InvalidHandle(ErrorModule::Kernel);
|
||||
ResultVal<u8*> SharedMemory::GetPointer(u32 offset) {
|
||||
if (base_address != 0)
|
||||
return MakeResult<u8*>(Memory::GetPointer(base_address + offset));
|
||||
|
||||
if (0 != shared_memory->base_address)
|
||||
return MakeResult<u8*>(Memory::GetPointer(shared_memory->base_address + offset));
|
||||
|
||||
LOG_ERROR(Kernel_SVC, "memory block handle=0x%08X not mapped!", handle);
|
||||
LOG_ERROR(Kernel_SVC, "memory block handle=0x%08X not mapped!", GetHandle());
|
||||
// TODO(yuriks): Verify error code.
|
||||
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
|
||||
ErrorSummary::InvalidState, ErrorLevel::Permanent);
|
||||
|
|
|
@ -23,29 +23,41 @@ enum class MemoryPermission : u32 {
|
|||
DontCare = (1u << 28)
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a shared memory object
|
||||
* @param name Optional name of shared memory object
|
||||
* @return Handle of newly created shared memory object
|
||||
*/
|
||||
Handle CreateSharedMemory(const std::string& name="Unknown");
|
||||
class SharedMemory : public Object {
|
||||
public:
|
||||
/**
|
||||
* Creates a shared memory object
|
||||
* @param name Optional object name, used only for debugging purposes.
|
||||
*/
|
||||
static ResultVal<SharedPtr<SharedMemory>> Create(std::string name = "Unknown");
|
||||
|
||||
/**
|
||||
* Maps a shared memory block to an address in system memory
|
||||
* @param handle Shared memory block handle
|
||||
* @param address Address in system memory to map shared memory block to
|
||||
* @param permissions Memory block map permissions (specified by SVC field)
|
||||
* @param other_permissions Memory block map other permissions (specified by SVC field)
|
||||
*/
|
||||
ResultCode MapSharedMemory(Handle handle, u32 address, MemoryPermission permissions,
|
||||
MemoryPermission other_permissions);
|
||||
std::string GetTypeName() const override { return "SharedMemory"; }
|
||||
|
||||
/**
|
||||
* Gets a pointer to the shared memory block
|
||||
* @param handle Shared memory block handle
|
||||
* @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
|
||||
*/
|
||||
ResultVal<u8*> GetSharedMemoryPointer(Handle handle, u32 offset);
|
||||
static const HandleType HANDLE_TYPE = HandleType::SharedMemory;
|
||||
HandleType GetHandleType() const override { return HANDLE_TYPE; }
|
||||
|
||||
/**
|
||||
* Maps a shared memory block to an address in system memory
|
||||
* @param address Address in system memory to map shared memory block to
|
||||
* @param permissions Memory block map permissions (specified by SVC field)
|
||||
* @param other_permissions Memory block map other permissions (specified by SVC field)
|
||||
*/
|
||||
ResultCode Map(VAddr address, MemoryPermission permissions, MemoryPermission other_permissions);
|
||||
|
||||
/**
|
||||
* Gets a pointer to the shared memory block
|
||||
* @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
|
||||
*/
|
||||
ResultVal<u8*> GetPointer(u32 offset = 0);
|
||||
|
||||
VAddr base_address; ///< Address of shared memory block in RAM
|
||||
MemoryPermission permissions; ///< Permissions of shared memory block (SVC field)
|
||||
MemoryPermission other_permissions; ///< Other permissions of shared memory block (SVC field)
|
||||
std::string name; ///< Name of shared memory object (optional)
|
||||
|
||||
private:
|
||||
SharedMemory() = default;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue