1
0
Fork 0
forked from suyu/suyu

Kernel: Convert SharedMemory to not use Handles

This commit is contained in:
Yuri Kunde Schlesner 2015-01-11 03:43:29 -02:00
parent fc11aff955
commit 4bb33dfc30
8 changed files with 105 additions and 100 deletions

View file

@ -9,68 +9,39 @@
namespace Kernel { namespace Kernel {
class SharedMemory : public Object { ResultVal<SharedPtr<SharedMemory>> SharedMemory::Create(std::string name) {
public: SharedPtr<SharedMemory> shared_memory(new SharedMemory);
std::string GetTypeName() const override { return "SharedMemory"; }
static const HandleType HANDLE_TYPE = HandleType::SharedMemory; // TOOD(yuriks): Don't create Handle (see Thread::Create())
HandleType GetHandleType() const override { return HANDLE_TYPE; } CASCADE_RESULT(auto unused, Kernel::g_handle_table.Create(shared_memory));
u32 base_address; ///< Address of shared memory block in RAM shared_memory->name = std::move(name);
MemoryPermission permissions; ///< Permissions of shared memory block (SVC field) return MakeResult<SharedPtr<SharedMemory>>(std::move(shared_memory));
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;
} }
Handle CreateSharedMemory(const std::string& name) { ResultCode SharedMemory::Map(VAddr address, MemoryPermission permissions,
Handle handle;
CreateSharedMemory(handle, name);
return handle;
}
ResultCode MapSharedMemory(u32 handle, u32 address, MemoryPermission permissions,
MemoryPermission other_permissions) { MemoryPermission other_permissions) {
if (address < Memory::SHARED_MEMORY_VADDR || address >= Memory::SHARED_MEMORY_VADDR_END) { 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!", LOG_ERROR(Kernel, "cannot map handle=0x%08X, address=0x%08X outside of shared mem bounds!",
handle, address); GetHandle(), address);
// TODO: Verify error code with hardware
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel, return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
ErrorSummary::InvalidArgument, ErrorLevel::Permanent); 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; base_address = address;
shared_memory->permissions = permissions; permissions = permissions;
shared_memory->other_permissions = other_permissions; other_permissions = other_permissions;
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
ResultVal<u8*> GetSharedMemoryPointer(Handle handle, u32 offset) { ResultVal<u8*> SharedMemory::GetPointer(u32 offset) {
SharedMemory* shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle).get(); if (base_address != 0)
if (shared_memory == nullptr) return InvalidHandle(ErrorModule::Kernel); return MakeResult<u8*>(Memory::GetPointer(base_address + offset));
if (0 != shared_memory->base_address) LOG_ERROR(Kernel_SVC, "memory block handle=0x%08X not mapped!", GetHandle());
return MakeResult<u8*>(Memory::GetPointer(shared_memory->base_address + offset));
LOG_ERROR(Kernel_SVC, "memory block handle=0x%08X not mapped!", handle);
// TODO(yuriks): Verify error code. // TODO(yuriks): Verify error code.
return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel, return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
ErrorSummary::InvalidState, ErrorLevel::Permanent); ErrorSummary::InvalidState, ErrorLevel::Permanent);

View file

@ -23,29 +23,41 @@ enum class MemoryPermission : u32 {
DontCare = (1u << 28) DontCare = (1u << 28)
}; };
/** class SharedMemory : public Object {
public:
/**
* Creates a shared memory object * Creates a shared memory object
* @param name Optional name of shared memory object * @param name Optional object name, used only for debugging purposes.
* @return Handle of newly created shared memory object
*/ */
Handle CreateSharedMemory(const std::string& name="Unknown"); static ResultVal<SharedPtr<SharedMemory>> Create(std::string name = "Unknown");
/** std::string GetTypeName() const override { return "SharedMemory"; }
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 * 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 address Address in system memory to map shared memory block to
* @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 MapSharedMemory(Handle handle, u32 address, MemoryPermission permissions, ResultCode Map(VAddr address, MemoryPermission permissions, MemoryPermission other_permissions);
MemoryPermission other_permissions);
/** /**
* Gets a pointer to the shared memory block * 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 * @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 Pointer to the shared memory block from the specified offset
*/ */
ResultVal<u8*> GetSharedMemoryPointer(Handle handle, u32 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 } // namespace

View file

@ -26,7 +26,7 @@ namespace APT_U {
static const VAddr SHARED_FONT_VADDR = 0x18000000; static const VAddr SHARED_FONT_VADDR = 0x18000000;
/// Handle to shared memory region designated to for shared system font /// Handle to shared memory region designated to for shared system font
static Handle shared_font_mem = 0; static Kernel::SharedPtr<Kernel::SharedMemory> shared_font_mem;
static Handle lock_handle = 0; static Handle lock_handle = 0;
static Handle notification_event_handle = 0; ///< APT notification event handle static Handle notification_event_handle = 0; ///< APT notification event handle
@ -354,7 +354,7 @@ void GetSharedFont(Service::Interface* self) {
cmd_buff[0] = 0x00440082; cmd_buff[0] = 0x00440082;
cmd_buff[1] = RESULT_SUCCESS.raw; // No error cmd_buff[1] = RESULT_SUCCESS.raw; // No error
cmd_buff[2] = SHARED_FONT_VADDR; cmd_buff[2] = SHARED_FONT_VADDR;
cmd_buff[4] = shared_font_mem; cmd_buff[4] = Kernel::g_handle_table.Create(shared_font_mem).MoveFrom();
} else { } else {
cmd_buff[1] = -1; // Generic error (not really possible to verify this on hardware) cmd_buff[1] = -1; // Generic error (not really possible to verify this on hardware)
LOG_ERROR(Kernel_SVC, "called, but %s has not been loaded!", SHARED_FONT); LOG_ERROR(Kernel_SVC, "called, but %s has not been loaded!", SHARED_FONT);
@ -514,10 +514,10 @@ Interface::Interface() {
file.ReadBytes(shared_font.data(), (size_t)file.GetSize()); file.ReadBytes(shared_font.data(), (size_t)file.GetSize());
// Create shared font memory object // Create shared font memory object
shared_font_mem = Kernel::CreateSharedMemory("APT_U:shared_font_mem"); shared_font_mem = Kernel::SharedMemory::Create("APT_U:shared_font_mem").MoveFrom();
} else { } else {
LOG_WARNING(Service_APT, "Unable to load shared font: %s", filepath.c_str()); LOG_WARNING(Service_APT, "Unable to load shared font: %s", filepath.c_str());
shared_font_mem = 0; shared_font_mem = nullptr;
} }
lock_handle = 0; lock_handle = 0;

View file

@ -23,12 +23,12 @@ GraphicsDebugger g_debugger;
namespace GSP_GPU { namespace GSP_GPU {
Handle g_interrupt_event = 0; ///< Handle to event triggered when GSP interrupt has been signalled Handle g_interrupt_event = 0; ///< Handle to event triggered when GSP interrupt has been signalled
Handle g_shared_memory = 0; ///< Handle to GSP shared memorys Kernel::SharedPtr<Kernel::SharedMemory> g_shared_memory; ///< GSP shared memoryings
u32 g_thread_id = 1; ///< Thread index into interrupt relay queue, 1 is arbitrary u32 g_thread_id = 1; ///< Thread index into interrupt relay queue, 1 is arbitrary
/// Gets a pointer to a thread command buffer in GSP shared memory /// Gets a pointer to a thread command buffer in GSP shared memory
static inline u8* GetCommandBuffer(u32 thread_id) { static inline u8* GetCommandBuffer(u32 thread_id) {
ResultVal<u8*> ptr = Kernel::GetSharedMemoryPointer(g_shared_memory, 0x800 + (thread_id * sizeof(CommandBuffer))); ResultVal<u8*> ptr = g_shared_memory->GetPointer(0x800 + (thread_id * sizeof(CommandBuffer)));
return ptr.ValueOr(nullptr); return ptr.ValueOr(nullptr);
} }
@ -37,13 +37,13 @@ static inline FrameBufferUpdate* GetFrameBufferInfo(u32 thread_id, u32 screen_in
// For each thread there are two FrameBufferUpdate fields // For each thread there are two FrameBufferUpdate fields
u32 offset = 0x200 + (2 * thread_id + screen_index) * sizeof(FrameBufferUpdate); u32 offset = 0x200 + (2 * thread_id + screen_index) * sizeof(FrameBufferUpdate);
ResultVal<u8*> ptr = Kernel::GetSharedMemoryPointer(g_shared_memory, offset); ResultVal<u8*> ptr = g_shared_memory->GetPointer(offset);
return reinterpret_cast<FrameBufferUpdate*>(ptr.ValueOr(nullptr)); return reinterpret_cast<FrameBufferUpdate*>(ptr.ValueOr(nullptr));
} }
/// Gets a pointer to the interrupt relay queue for a given thread index /// Gets a pointer to the interrupt relay queue for a given thread index
static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) { static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) {
ResultVal<u8*> ptr = Kernel::GetSharedMemoryPointer(g_shared_memory, sizeof(InterruptRelayQueue) * thread_id); ResultVal<u8*> ptr = g_shared_memory->GetPointer(sizeof(InterruptRelayQueue) * thread_id);
return reinterpret_cast<InterruptRelayQueue*>(ptr.ValueOr(nullptr)); return reinterpret_cast<InterruptRelayQueue*>(ptr.ValueOr(nullptr));
} }
@ -182,13 +182,15 @@ static void RegisterInterruptRelayQueue(Service::Interface* self) {
u32* cmd_buff = Kernel::GetCommandBuffer(); u32* cmd_buff = Kernel::GetCommandBuffer();
u32 flags = cmd_buff[1]; u32 flags = cmd_buff[1];
g_interrupt_event = cmd_buff[3]; g_interrupt_event = cmd_buff[3];
g_shared_memory = Kernel::CreateSharedMemory("GSPSharedMem"); g_shared_memory = Kernel::SharedMemory::Create("GSPSharedMem").MoveFrom();
_assert_msg_(GSP, (g_interrupt_event != 0), "handle is not valid!"); _assert_msg_(GSP, (g_interrupt_event != 0), "handle is not valid!");
Handle shmem_handle = Kernel::g_handle_table.Create(g_shared_memory).MoveFrom();
cmd_buff[1] = 0x2A07; // Value verified by 3dmoo team, purpose unknown, but needed for GSP init cmd_buff[1] = 0x2A07; // Value verified by 3dmoo team, purpose unknown, but needed for GSP init
cmd_buff[2] = g_thread_id++; // Thread ID cmd_buff[2] = g_thread_id++; // Thread ID
cmd_buff[4] = g_shared_memory; // GSP shared memory cmd_buff[4] = shmem_handle; // GSP shared memory
Kernel::SignalEvent(g_interrupt_event); // TODO(bunnei): Is this correct? Kernel::SignalEvent(g_interrupt_event); // TODO(bunnei): Is this correct?
} }
@ -204,7 +206,7 @@ void SignalInterrupt(InterruptId interrupt_id) {
LOG_WARNING(Service_GSP, "cannot synchronize until GSP event has been created!"); LOG_WARNING(Service_GSP, "cannot synchronize until GSP event has been created!");
return; return;
} }
if (0 == g_shared_memory) { if (nullptr == g_shared_memory) {
LOG_WARNING(Service_GSP, "cannot synchronize until GSP shared memory has been created!"); LOG_WARNING(Service_GSP, "cannot synchronize until GSP shared memory has been created!");
return; return;
} }

View file

@ -12,7 +12,7 @@
namespace Service { namespace Service {
namespace HID { namespace HID {
Handle g_shared_mem = 0; Kernel::SharedPtr<Kernel::SharedMemory> g_shared_mem = nullptr;
Handle g_event_pad_or_touch_1 = 0; Handle g_event_pad_or_touch_1 = 0;
Handle g_event_pad_or_touch_2 = 0; Handle g_event_pad_or_touch_2 = 0;
@ -30,7 +30,7 @@ static s16 next_circle_y = 0;
* Gets a pointer to the PadData structure inside HID shared memory * Gets a pointer to the PadData structure inside HID shared memory
*/ */
static inline PadData* GetPadData() { static inline PadData* GetPadData() {
return reinterpret_cast<PadData*>(Kernel::GetSharedMemoryPointer(g_shared_mem, 0).ValueOr(nullptr)); return reinterpret_cast<PadData*>(g_shared_mem->GetPointer().ValueOr(nullptr));
} }
/** /**
@ -120,7 +120,7 @@ void PadUpdateComplete() {
} }
void HIDInit() { void HIDInit() {
g_shared_mem = Kernel::CreateSharedMemory("HID:SharedMem"); // Create shared memory object g_shared_mem = Kernel::SharedMemory::Create("HID:SharedMem").MoveFrom();
// Create event handles // Create event handles
g_event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID:EventPadOrTouch1"); g_event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID:EventPadOrTouch1");

View file

@ -9,11 +9,15 @@
#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/kernel.h"
#include "common/bit_field.h" #include "common/bit_field.h"
namespace Kernel {
class SharedMemory;
}
namespace Service { namespace Service {
namespace HID { namespace HID {
// Handle to shared memory region designated to HID_User service // Handle to shared memory region designated to HID_User service
extern Handle g_shared_mem; extern Kernel::SharedPtr<Kernel::SharedMemory> g_shared_mem;
// Event handles // Event handles
extern Handle g_event_pad_or_touch_1; extern Handle g_event_pad_or_touch_1;

View file

@ -5,6 +5,7 @@
#include "common/log.h" #include "common/log.h"
#include "core/hle/hle.h" #include "core/hle/hle.h"
#include "core/hle/kernel/shared_memory.h"
#include "core/hle/service/hid/hid.h" #include "core/hle/service/hid/hid.h"
#include "hid_user.h" #include "hid_user.h"
@ -46,7 +47,8 @@ void GetIPCHandles(Service::Interface* self) {
u32* cmd_buff = Kernel::GetCommandBuffer(); u32* cmd_buff = Kernel::GetCommandBuffer();
cmd_buff[1] = 0; // No error cmd_buff[1] = 0; // No error
cmd_buff[3] = Service::HID::g_shared_mem; // TODO(yuriks): Return error from SendSyncRequest is this fails (part of IPC marshalling)
cmd_buff[3] = Kernel::g_handle_table.Create(Service::HID::g_shared_mem).MoveFrom();
cmd_buff[4] = Service::HID::g_event_pad_or_touch_1; cmd_buff[4] = Service::HID::g_event_pad_or_touch_1;
cmd_buff[5] = Service::HID::g_event_pad_or_touch_2; cmd_buff[5] = Service::HID::g_event_pad_or_touch_2;
cmd_buff[6] = Service::HID::g_event_accelerometer; cmd_buff[6] = Service::HID::g_event_accelerometer;

View file

@ -63,21 +63,28 @@ static Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1,
/// Maps a memory block to specified address /// Maps a memory block to specified address
static Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) { static Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) {
using Kernel::SharedMemory;
using Kernel::MemoryPermission;
LOG_TRACE(Kernel_SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d", LOG_TRACE(Kernel_SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d",
handle, addr, permissions, other_permissions); handle, addr, permissions, other_permissions);
Kernel::MemoryPermission permissions_type = static_cast<Kernel::MemoryPermission>(permissions); SharedPtr<SharedMemory> shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
if (shared_memory == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
MemoryPermission permissions_type = static_cast<MemoryPermission>(permissions);
switch (permissions_type) { switch (permissions_type) {
case Kernel::MemoryPermission::Read: case MemoryPermission::Read:
case Kernel::MemoryPermission::Write: case MemoryPermission::Write:
case Kernel::MemoryPermission::ReadWrite: case MemoryPermission::ReadWrite:
case Kernel::MemoryPermission::Execute: case MemoryPermission::Execute:
case Kernel::MemoryPermission::ReadExecute: case MemoryPermission::ReadExecute:
case Kernel::MemoryPermission::WriteExecute: case MemoryPermission::WriteExecute:
case Kernel::MemoryPermission::ReadWriteExecute: case MemoryPermission::ReadWriteExecute:
case Kernel::MemoryPermission::DontCare: case MemoryPermission::DontCare:
Kernel::MapSharedMemory(handle, addr, permissions_type, shared_memory->Map(addr, permissions_type,
static_cast<Kernel::MemoryPermission>(other_permissions)); static_cast<MemoryPermission>(other_permissions));
break; break;
default: default:
LOG_ERROR(Kernel_SVC, "unknown permissions=0x%08X", permissions); LOG_ERROR(Kernel_SVC, "unknown permissions=0x%08X", permissions);
@ -464,11 +471,18 @@ static s64 GetSystemTick() {
/// Creates a memory block at the specified address with the specified permissions and size /// Creates a memory block at the specified address with the specified permissions and size
static Result CreateMemoryBlock(Handle* memblock, u32 addr, u32 size, u32 my_permission, static Result CreateMemoryBlock(Handle* memblock, u32 addr, u32 size, u32 my_permission,
u32 other_permission) { u32 other_permission) {
using Kernel::SharedMemory;
// TODO(Subv): Implement this function // TODO(Subv): Implement this function
Handle shared_memory = Kernel::CreateSharedMemory(); ResultVal<SharedPtr<SharedMemory>> shared_memory_res = SharedMemory::Create();
*memblock = shared_memory; if (shared_memory_res.Failed())
return shared_memory_res.Code().raw;
ResultVal<Handle> handle_res = Kernel::g_handle_table.Create(*shared_memory_res);
if (handle_res.Failed())
return handle_res.Code().raw;
*memblock = *handle_res;
LOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x%08X", addr); LOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x%08X", addr);
return 0; return 0;
} }