hle: kernel: Migrate KSharedMemory to KAutoObject.

This commit is contained in:
bunnei 2021-04-03 23:22:07 -07:00
parent 7ccbdd4d8d
commit 086db71e94
16 changed files with 128 additions and 114 deletions

View file

@ -47,9 +47,21 @@ ResultCode HandleTable::SetSize(s32 handle_table_size) {
return RESULT_SUCCESS;
}
ResultVal<Handle> HandleTable::Create(std::shared_ptr<Object> obj) {
ResultVal<Handle> HandleTable::Create(Object* obj) {
DEBUG_ASSERT(obj != nullptr);
switch (obj->GetHandleType()) {
case HandleType::SharedMemory:
case HandleType::Thread:
case HandleType::Process: {
Handle handle{};
Add(&handle, reinterpret_cast<KAutoObject*>(obj), {});
return MakeResult<Handle>(handle);
}
default:
break;
}
const u16 slot = next_free_slot;
if (slot >= table_size) {
LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
@ -66,7 +78,7 @@ ResultVal<Handle> HandleTable::Create(std::shared_ptr<Object> obj) {
}
generations[slot] = generation;
objects[slot] = std::move(obj);
objects[slot] = std::move(SharedFrom(obj));
Handle handle = generation | (slot << 15);
return MakeResult<Handle>(handle);
@ -100,12 +112,12 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) {
}
ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
std::shared_ptr<Object> object = SharedFrom(GetGeneric(handle));
auto object = GetGeneric(handle);
if (object == nullptr) {
LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
return ResultInvalidHandle;
}
return Create(std::move(object));
return Create(object);
}
bool HandleTable::Remove(Handle handle) {

View file

@ -74,7 +74,7 @@ public:
* @return The created Handle or one of the following errors:
* - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded.
*/
ResultVal<Handle> Create(std::shared_ptr<Object> obj);
ResultVal<Handle> Create(Object* obj);
/**
* Returns a new handle that points to the same object as the passed in handle.

View file

@ -225,12 +225,12 @@ ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(KThread& thread) {
// for specific values in each of these descriptors.
for (auto& object : copy_objects) {
ASSERT(object != nullptr);
dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap();
dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap();
}
for (auto& object : move_objects) {
ASSERT(object != nullptr);
dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap();
dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap();
}
}

View file

@ -11,6 +11,7 @@
#include "core/hle/kernel/init/init_slab_setup.h"
#include "core/hle/kernel/k_memory_layout.h"
#include "core/hle/kernel/k_memory_manager.h"
#include "core/hle/kernel/k_shared_memory.h"
#include "core/hle/kernel/k_system_control.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/memory_types.h"
@ -23,7 +24,8 @@ namespace Kernel::Init {
#define FOREACH_SLAB_TYPE(HANDLER, ...) \
HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \
HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__)
HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \
HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__)
namespace {

View file

@ -11,36 +11,53 @@
namespace Kernel {
KSharedMemory::KSharedMemory(KernelCore& kernel, Core::DeviceMemory& device_memory)
: Object{kernel}, device_memory{device_memory} {}
KSharedMemory::KSharedMemory(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {}
KSharedMemory::~KSharedMemory() {
kernel.GetSystemResourceLimit()->Release(LimitableResource::PhysicalMemory, size);
}
std::shared_ptr<KSharedMemory> KSharedMemory::Create(
KernelCore& kernel, Core::DeviceMemory& device_memory, Process* owner_process,
KPageLinkedList&& page_list, KMemoryPermission owner_permission,
KMemoryPermission user_permission, PAddr physical_address, std::size_t size, std::string name) {
ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_,
Process* owner_process_, KPageLinkedList&& page_list_,
KMemoryPermission owner_permission_,
KMemoryPermission user_permission_, PAddr physical_address_,
std::size_t size_, std::string name_) {
const auto resource_limit = kernel.GetSystemResourceLimit();
resource_limit = kernel_.GetSystemResourceLimit();
KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory,
size);
size_);
ASSERT(memory_reservation.Succeeded());
std::shared_ptr<KSharedMemory> shared_memory{
std::make_shared<KSharedMemory>(kernel, device_memory)};
shared_memory->owner_process = owner_process;
shared_memory->page_list = std::move(page_list);
shared_memory->owner_permission = owner_permission;
shared_memory->user_permission = user_permission;
shared_memory->physical_address = physical_address;
shared_memory->size = size;
shared_memory->name = name;
owner_process = owner_process_;
device_memory = &device_memory_;
page_list = std::move(page_list_);
owner_permission = owner_permission_;
user_permission = user_permission_;
physical_address = physical_address_;
size = size_;
name = name_;
is_initialized = true;
memory_reservation.Commit();
return shared_memory;
return RESULT_SUCCESS;
}
void KSharedMemory::Finalize() {
///* Get the number of pages. */
// const size_t num_pages = m_page_group.GetNumPages();
// const size_t size = num_pages * PageSize;
///* Close and finalize the page group. */
// m_page_group.Close();
// m_page_group.Finalize();
// Release the memory reservation.
resource_limit->Release(LimitableResource::PhysicalMemory, size);
resource_limit->Close();
// Perform inherited finalization.
KAutoObjectWithSlabHeapAndContainer<KSharedMemory, KAutoObjectWithList>::Finalize();
}
ResultCode KSharedMemory::Map(Process& target_process, VAddr address, std::size_t size,

View file

@ -11,24 +11,26 @@
#include "core/device_memory.h"
#include "core/hle/kernel/k_memory_block.h"
#include "core/hle/kernel/k_page_linked_list.h"
#include "core/hle/kernel/object.h"
#include "core/hle/kernel/process.h"
#include "core/hle/kernel/slab_helpers.h"
#include "core/hle/result.h"
namespace Kernel {
class KernelCore;
class KSharedMemory final : public Object {
class KSharedMemory final
: public KAutoObjectWithSlabHeapAndContainer<KSharedMemory, KAutoObjectWithList> {
KERNEL_AUTOOBJECT_TRAITS(KSharedMemory, KAutoObject);
public:
explicit KSharedMemory(KernelCore& kernel, Core::DeviceMemory& device_memory);
explicit KSharedMemory(KernelCore& kernel);
~KSharedMemory() override;
static std::shared_ptr<KSharedMemory> Create(
KernelCore& kernel, Core::DeviceMemory& device_memory, Process* owner_process,
KPageLinkedList&& page_list, KMemoryPermission owner_permission,
KMemoryPermission user_permission, PAddr physical_address, std::size_t size,
std::string name);
ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_,
Process* owner_process_, KPageLinkedList&& page_list_,
KMemoryPermission owner_permission_, KMemoryPermission user_permission_,
PAddr physical_address_, std::size_t size_, std::string name_);
std::string GetTypeName() const override {
return "SharedMemory";
@ -59,7 +61,7 @@ public:
* @return A pointer to the shared memory block from the specified offset
*/
u8* GetPointer(std::size_t offset = 0) {
return device_memory.GetPointer(physical_address + offset);
return device_memory->GetPointer(physical_address + offset);
}
/**
@ -68,20 +70,26 @@ public:
* @return A pointer to the shared memory block from the specified offset
*/
const u8* GetPointer(std::size_t offset = 0) const {
return device_memory.GetPointer(physical_address + offset);
return device_memory->GetPointer(physical_address + offset);
}
void Finalize() override {}
virtual void Finalize() override;
virtual bool IsInitialized() const override {
return is_initialized;
}
static void PostDestroy([[maybe_unused]] uintptr_t arg) {}
private:
Core::DeviceMemory& device_memory;
Core::DeviceMemory* device_memory;
Process* owner_process{};
KPageLinkedList page_list;
KMemoryPermission owner_permission{};
KMemoryPermission user_permission{};
PAddr physical_address{};
std::size_t size{};
std::string name;
std::shared_ptr<KResourceLimit> resource_limit;
bool is_initialized{};
};
} // namespace Kernel

View file

@ -291,6 +291,9 @@ void KThread::Finalize() {
parent->DecrementThreadCount();
parent->GetResourceLimit()->Release(LimitableResource::Threads, 1);
}
// Perform inherited finalization.
KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize();
}
bool KThread::IsSignaled() const {

View file

@ -575,22 +575,27 @@ struct KernelCore::Impl {
const PAddr irs_phys_addr{system_pool.GetAddress() + hid_size + font_size};
const PAddr time_phys_addr{system_pool.GetAddress() + hid_size + font_size + irs_size};
hid_shared_mem = Kernel::KSharedMemory::Create(
system.Kernel(), system.DeviceMemory(), nullptr, {hid_phys_addr, hid_size / PageSize},
KMemoryPermission::None, KMemoryPermission::Read, hid_phys_addr, hid_size,
"HID:SharedMemory");
font_shared_mem = Kernel::KSharedMemory::Create(
system.Kernel(), system.DeviceMemory(), nullptr, {font_phys_addr, font_size / PageSize},
KMemoryPermission::None, KMemoryPermission::Read, font_phys_addr, font_size,
"Font:SharedMemory");
irs_shared_mem = Kernel::KSharedMemory::Create(
system.Kernel(), system.DeviceMemory(), nullptr, {irs_phys_addr, irs_size / PageSize},
KMemoryPermission::None, KMemoryPermission::Read, irs_phys_addr, irs_size,
"IRS:SharedMemory");
time_shared_mem = Kernel::KSharedMemory::Create(
system.Kernel(), system.DeviceMemory(), nullptr, {time_phys_addr, time_size / PageSize},
KMemoryPermission::None, KMemoryPermission::Read, time_phys_addr, time_size,
"Time:SharedMemory");
hid_shared_mem = std::make_unique<KSharedMemory>(system.Kernel());
font_shared_mem = std::make_unique<KSharedMemory>(system.Kernel());
irs_shared_mem = std::make_unique<KSharedMemory>(system.Kernel());
time_shared_mem = std::make_unique<KSharedMemory>(system.Kernel());
hid_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr,
{hid_phys_addr, hid_size / PageSize}, KMemoryPermission::None,
KMemoryPermission::Read, hid_phys_addr, hid_size,
"HID:SharedMemory");
font_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr,
{font_phys_addr, font_size / PageSize}, KMemoryPermission::None,
KMemoryPermission::Read, font_phys_addr, font_size,
"Font:SharedMemory");
irs_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr,
{irs_phys_addr, irs_size / PageSize}, KMemoryPermission::None,
KMemoryPermission::Read, irs_phys_addr, irs_size,
"IRS:SharedMemory");
time_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr,
{time_phys_addr, time_size / PageSize}, KMemoryPermission::None,
KMemoryPermission::Read, time_phys_addr, time_size,
"Time:SharedMemory");
}
void InitializePageSlab() {
@ -644,10 +649,10 @@ struct KernelCore::Impl {
std::unique_ptr<KSlabHeap<Page>> user_slab_heap_pages;
// Shared memory for services
std::shared_ptr<Kernel::KSharedMemory> hid_shared_mem;
std::shared_ptr<Kernel::KSharedMemory> font_shared_mem;
std::shared_ptr<Kernel::KSharedMemory> irs_shared_mem;
std::shared_ptr<Kernel::KSharedMemory> time_shared_mem;
std::unique_ptr<Kernel::KSharedMemory> hid_shared_mem;
std::unique_ptr<Kernel::KSharedMemory> font_shared_mem;
std::unique_ptr<Kernel::KSharedMemory> irs_shared_mem;
std::unique_ptr<Kernel::KSharedMemory> time_shared_mem;
// Threads used for services
std::unordered_set<std::shared_ptr<Kernel::ServiceThread>> service_threads;

View file

@ -328,7 +328,7 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle,
// Return the client session
auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
CASCADE_RESULT(*out_handle, handle_table.Create(client_session));
CASCADE_RESULT(*out_handle, handle_table.Create(client_session.get()));
return RESULT_SUCCESS;
}
@ -836,7 +836,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 ha
return RESULT_SUCCESS;
}
const auto table_result = handle_table.Create(resource_limit);
const auto table_result = handle_table.Create(resource_limit.get());
if (table_result.Failed()) {
return table_result.Code();
}
@ -1168,12 +1168,9 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han
return ResultInvalidMemoryRange;
}
auto shared_memory{current_process->GetHandleTable().Get<KSharedMemory>(shared_memory_handle)};
if (!shared_memory) {
LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}",
shared_memory_handle);
return ResultInvalidHandle;
}
auto shared_memory{
current_process->GetHandleTable().GetObject<KSharedMemory>(shared_memory_handle)};
R_UNLESS(shared_memory.IsNotNull(), ResultInvalidHandle);
return shared_memory->Map(*current_process, addr, size,
static_cast<KMemoryPermission>(permission_type));
@ -1817,7 +1814,7 @@ static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAd
}
auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
const auto result{handle_table.Create(std::move(transfer_mem_handle))};
const auto result{handle_table.Create(transfer_mem_handle.get())};
if (result.Failed()) {
return result.Code();
}
@ -1966,7 +1963,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o
event->Initialize();
// Add the writable event to the handle table.
const auto write_create_result = handle_table.Create(event->GetWritableEvent());
const auto write_create_result = handle_table.Create(event->GetWritableEvent().get());
if (write_create_result.Failed()) {
return write_create_result.Code();
}
@ -1976,7 +1973,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o
auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*write_create_result); });
// Add the readable event to the handle table.
const auto read_create_result = handle_table.Create(SharedFrom(event->GetReadableEvent()));
const auto read_create_result = handle_table.Create(event->GetReadableEvent());
if (read_create_result.Failed()) {
return read_create_result.Code();
}
@ -2027,7 +2024,7 @@ static ResultCode CreateResourceLimit(Core::System& system, Handle* out_handle)
auto* const current_process = kernel.CurrentProcess();
ASSERT(current_process != nullptr);
const auto handle = current_process->GetHandleTable().Create(std::move(resource_limit));
const auto handle = current_process->GetHandleTable().Create(resource_limit.get());
if (handle.Failed()) {
return handle.Code();
}

View file

@ -53,9 +53,6 @@ IAppletResource::IAppletResource(Core::System& system_)
};
RegisterHandlers(functions);
auto& kernel = system.Kernel();
shared_mem = SharedFrom(&kernel.GetHidSharedMem());
MakeController<Controller_DebugPad>(HidController::DebugPad);
MakeController<Controller_Touchscreen>(HidController::Touchscreen);
MakeController<Controller_Mouse>(HidController::Mouse);
@ -118,7 +115,7 @@ void IAppletResource::GetSharedMemoryHandle(Kernel::HLERequestContext& ctx) {
IPC::ResponseBuilder rb{ctx, 2, 1};
rb.Push(RESULT_SUCCESS);
rb.PushCopyObjects(shared_mem.get());
rb.PushCopyObjects(&system.Kernel().GetHidSharedMem());
}
void IAppletResource::UpdateControllers(std::uintptr_t user_data,
@ -130,7 +127,8 @@ void IAppletResource::UpdateControllers(std::uintptr_t user_data,
if (should_reload) {
controller->OnLoadInputDevices();
}
controller->OnUpdate(core_timing, shared_mem->GetPointer(), SHARED_MEMORY_SIZE);
controller->OnUpdate(core_timing, system.Kernel().GetHidSharedMem().GetPointer(),
SHARED_MEMORY_SIZE);
}
// If ns_late is higher than the update rate ignore the delay
@ -145,7 +143,7 @@ void IAppletResource::UpdateMotion(std::uintptr_t user_data, std::chrono::nanose
auto& core_timing = system.CoreTiming();
controllers[static_cast<size_t>(HidController::NPad)]->OnMotionUpdate(
core_timing, shared_mem->GetPointer(), SHARED_MEMORY_SIZE);
core_timing, system.Kernel().GetHidSharedMem().GetPointer(), SHARED_MEMORY_SIZE);
// If ns_late is higher than the update rate ignore the delay
if (ns_late > motion_update_ns) {

View file

@ -13,10 +13,6 @@ namespace Core::Timing {
struct EventType;
}
namespace Kernel {
class KSharedMemory;
}
namespace Service::SM {
class ServiceManager;
}
@ -69,8 +65,6 @@ private:
void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late);
void UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late);
std::shared_ptr<Kernel::KSharedMemory> shared_mem;
std::shared_ptr<Core::Timing::EventType> pad_update_event;
std::shared_ptr<Core::Timing::EventType> motion_update_event;

View file

@ -37,10 +37,6 @@ IRS::IRS(Core::System& system_) : ServiceFramework{system_, "irs"} {
// clang-format on
RegisterHandlers(functions);
auto& kernel = system.Kernel();
shared_mem = SharedFrom(&kernel.GetIrsSharedMem());
}
void IRS::ActivateIrsensor(Kernel::HLERequestContext& ctx) {
@ -62,7 +58,7 @@ void IRS::GetIrsensorSharedMemoryHandle(Kernel::HLERequestContext& ctx) {
IPC::ResponseBuilder rb{ctx, 2, 1};
rb.Push(RESULT_SUCCESS);
rb.PushCopyObjects(shared_mem.get());
rb.PushCopyObjects(&system.Kernel().GetIrsSharedMem());
}
void IRS::StopImageProcessor(Kernel::HLERequestContext& ctx) {

View file

@ -11,10 +11,6 @@ namespace Core {
class System;
}
namespace Kernel {
class KSharedMemory;
}
namespace Service::HID {
class IRS final : public ServiceFramework<IRS> {
@ -42,7 +38,6 @@ private:
void StopImageProcessorAsync(Kernel::HLERequestContext& ctx);
void ActivateIrsensorWithFunctionLevel(Kernel::HLERequestContext& ctx);
std::shared_ptr<Kernel::KSharedMemory> shared_mem;
const u32 device_handle{0xABCD};
};

View file

@ -130,9 +130,6 @@ struct PL_U::Impl {
}
}
/// Handle to shared memory region designated for a shared font
std::shared_ptr<Kernel::KSharedMemory> shared_font_mem;
/// Backing memory for the shared font data
std::shared_ptr<Kernel::PhysicalMemory> shared_font;
@ -260,14 +257,13 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
// Create shared font memory object
auto& kernel = system.Kernel();
impl->shared_font_mem = SharedFrom(&kernel.GetFontSharedMem());
std::memcpy(impl->shared_font_mem->GetPointer(), impl->shared_font->data(),
std::memcpy(system.Kernel().GetFontSharedMem().GetPointer(), impl->shared_font->data(),
impl->shared_font->size());
IPC::ResponseBuilder rb{ctx, 2, 1};
rb.Push(RESULT_SUCCESS);
rb.PushCopyObjects(impl->shared_font_mem.get());
rb.PushCopyObjects(&system.Kernel().GetFontSharedMem());
}
void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) {

View file

@ -16,16 +16,11 @@ namespace Service::Time {
static constexpr std::size_t SHARED_MEMORY_SIZE{0x1000};
SharedMemory::SharedMemory(Core::System& system) : system(system) {
shared_memory_holder = SharedFrom(&system.Kernel().GetTimeSharedMem());
std::memset(shared_memory_holder->GetPointer(), 0, SHARED_MEMORY_SIZE);
std::memset(system.Kernel().GetTimeSharedMem().GetPointer(), 0, SHARED_MEMORY_SIZE);
}
SharedMemory::~SharedMemory() = default;
std::shared_ptr<Kernel::KSharedMemory> SharedMemory::GetSharedMemoryHolder() const {
return shared_memory_holder;
}
void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id,
Clock::TimeSpanType current_time_point) {
const Clock::TimeSpanType ticks_time_span{Clock::TimeSpanType::FromTicks(
@ -34,22 +29,22 @@ void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id,
static_cast<u64>(current_time_point.nanoseconds - ticks_time_span.nanoseconds),
clock_source_id};
shared_memory_format.standard_steady_clock_timepoint.StoreData(
shared_memory_holder->GetPointer(), context);
system.Kernel().GetTimeSharedMem().GetPointer(), context);
}
void SharedMemory::UpdateLocalSystemClockContext(const Clock::SystemClockContext& context) {
shared_memory_format.standard_local_system_clock_context.StoreData(
shared_memory_holder->GetPointer(), context);
system.Kernel().GetTimeSharedMem().GetPointer(), context);
}
void SharedMemory::UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context) {
shared_memory_format.standard_network_system_clock_context.StoreData(
shared_memory_holder->GetPointer(), context);
system.Kernel().GetTimeSharedMem().GetPointer(), context);
}
void SharedMemory::SetAutomaticCorrectionEnabled(bool is_enabled) {
shared_memory_format.standard_user_system_clock_automatic_correction.StoreData(
shared_memory_holder->GetPointer(), is_enabled);
system.Kernel().GetTimeSharedMem().GetPointer(), is_enabled);
}
} // namespace Service::Time

View file

@ -17,9 +17,6 @@ public:
explicit SharedMemory(Core::System& system);
~SharedMemory();
// Return the shared memory handle
std::shared_ptr<Kernel::KSharedMemory> GetSharedMemoryHolder() const;
// TODO(ogniK): We have to properly simulate memory barriers, how are we going to do this?
template <typename T, std::size_t Offset>
struct MemoryBarrier {
@ -63,7 +60,6 @@ public:
void SetAutomaticCorrectionEnabled(bool is_enabled);
private:
std::shared_ptr<Kernel::KSharedMemory> shared_memory_holder;
Core::System& system;
Format shared_memory_format{};
};