diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/hle/kernel/k_shared_memory.cpp | 69 | ||||
-rw-r--r-- | src/core/hle/kernel/k_shared_memory.h | 8 | ||||
-rw-r--r-- | src/core/hle/kernel/kernel.cpp | 43 | ||||
-rw-r--r-- | src/core/hle/service/time/clock_types.h | 1 | ||||
-rw-r--r-- | src/core/hle/service/time/time_sharedmemory.cpp | 17 | ||||
-rw-r--r-- | src/core/hle/service/time/time_sharedmemory.h | 87 |
6 files changed, 127 insertions, 98 deletions
diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index 10cd4c43d..0aa68103c 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -6,6 +6,7 @@ #include "core/hle/kernel/k_page_table.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_shared_memory.h" +#include "core/hle/kernel/k_system_resource.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/svc_results.h" @@ -18,19 +19,19 @@ KSharedMemory::~KSharedMemory() { } Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* owner_process_, - KPageGroup&& page_list_, Svc::MemoryPermission owner_permission_, - Svc::MemoryPermission user_permission_, PAddr physical_address_, - std::size_t size_, std::string name_) { + Svc::MemoryPermission owner_permission_, + Svc::MemoryPermission user_permission_, std::size_t size_, + std::string name_) { // Set members. 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_; + size = Common::AlignUp(size_, PageSize); name = std::move(name_); + const size_t num_pages = Common::DivideUp(size, PageSize); + // Get the resource limit. KResourceLimit* reslimit = kernel.GetSystemResourceLimit(); @@ -39,6 +40,17 @@ Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* o size_); R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached); + // Allocate the memory. + + //! HACK: Open continuous mapping from sysmodule pool. + auto option = KMemoryManager::EncodeOption(KMemoryManager::Pool::Secure, + KMemoryManager::Direction::FromBack); + physical_address = kernel.MemoryManager().AllocateAndOpenContinuous(num_pages, 1, option); + R_UNLESS(physical_address != 0, ResultOutOfMemory); + + //! Insert the result into our page group. + page_group.emplace(physical_address, num_pages); + // Commit our reservation. memory_reservation.Commit(); @@ -50,12 +62,23 @@ Result KSharedMemory::Initialize(Core::DeviceMemory& device_memory_, KProcess* o is_initialized = true; // Clear all pages in the memory. - std::memset(device_memory_.GetPointer<void>(physical_address_), 0, size_); + for (const auto& block : page_group->Nodes()) { + std::memset(device_memory_.GetPointer<void>(block.GetAddress()), 0, block.GetSize()); + } return ResultSuccess; } void KSharedMemory::Finalize() { + // Close and finalize the page group. + // page_group->Close(); + // page_group->Finalize(); + + //! HACK: Manually close. + for (const auto& block : page_group->Nodes()) { + kernel.MemoryManager().Close(block.GetAddress(), block.GetNumPages()); + } + // Release the memory reservation. resource_limit->Release(LimitableResource::PhysicalMemoryMax, size); resource_limit->Close(); @@ -65,32 +88,28 @@ void KSharedMemory::Finalize() { } Result KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t map_size, - Svc::MemoryPermission permissions) { - const u64 page_count{(map_size + PageSize - 1) / PageSize}; - - if (page_list.GetNumPages() != page_count) { - UNIMPLEMENTED_MSG("Page count does not match"); - } + Svc::MemoryPermission map_perm) { + // Validate the size. + R_UNLESS(size == map_size, ResultInvalidSize); - const Svc::MemoryPermission expected = + // Validate the permission. + const Svc::MemoryPermission test_perm = &target_process == owner_process ? owner_permission : user_permission; - - if (permissions != expected) { - UNIMPLEMENTED_MSG("Permission does not match"); + if (test_perm == Svc::MemoryPermission::DontCare) { + ASSERT(map_perm == Svc::MemoryPermission::Read || map_perm == Svc::MemoryPermission::Write); + } else { + R_UNLESS(map_perm == test_perm, ResultInvalidNewMemoryPermission); } - return target_process.PageTable().MapPages(address, page_list, KMemoryState::Shared, - ConvertToKMemoryPermission(permissions)); + return target_process.PageTable().MapPages(address, *page_group, KMemoryState::Shared, + ConvertToKMemoryPermission(map_perm)); } Result KSharedMemory::Unmap(KProcess& target_process, VAddr address, std::size_t unmap_size) { - const u64 page_count{(unmap_size + PageSize - 1) / PageSize}; - - if (page_list.GetNumPages() != page_count) { - UNIMPLEMENTED_MSG("Page count does not match"); - } + // Validate the size. + R_UNLESS(size == unmap_size, ResultInvalidSize); - return target_process.PageTable().UnmapPages(address, page_list, KMemoryState::Shared); + return target_process.PageTable().UnmapPages(address, *page_group, KMemoryState::Shared); } } // namespace Kernel diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index a96c55a3e..8b29f0b4a 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -3,6 +3,7 @@ #pragma once +#include <optional> #include <string> #include "common/common_types.h" @@ -26,9 +27,8 @@ public: ~KSharedMemory() override; Result Initialize(Core::DeviceMemory& device_memory_, KProcess* owner_process_, - KPageGroup&& page_list_, Svc::MemoryPermission owner_permission_, - Svc::MemoryPermission user_permission_, PAddr physical_address_, - std::size_t size_, std::string name_); + Svc::MemoryPermission owner_permission_, + Svc::MemoryPermission user_permission_, std::size_t size_, std::string name_); /** * Maps a shared memory block to an address in the target process' address space @@ -76,7 +76,7 @@ public: private: Core::DeviceMemory* device_memory{}; KProcess* owner_process{}; - KPageGroup page_list; + std::optional<KPageGroup> page_group{}; Svc::MemoryPermission owner_permission{}; Svc::MemoryPermission user_permission{}; PAddr physical_address{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index b75bac5df..1fb25f221 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -94,6 +94,7 @@ struct KernelCore::Impl { pt_heap_region.GetSize()); } + InitializeHackSharedMemory(); RegisterHostThread(nullptr); default_service_thread = &CreateServiceThread(kernel, "DefaultServiceThread"); @@ -726,14 +727,14 @@ struct KernelCore::Impl { } void InitializeMemoryLayout() { - const auto system_pool = memory_layout->GetKernelSystemPoolRegionPhysicalExtents(); - // Initialize the memory manager. memory_manager = std::make_unique<KMemoryManager>(system); const auto& management_region = memory_layout->GetPoolManagementRegion(); ASSERT(management_region.GetEndAddress() != 0); memory_manager->Initialize(management_region.GetAddress(), management_region.GetSize()); + } + void InitializeHackSharedMemory() { // Setup memory regions for emulated processes // TODO(bunnei): These should not be hardcoded regions initialized within the kernel constexpr std::size_t hid_size{0x40000}; @@ -742,39 +743,23 @@ struct KernelCore::Impl { constexpr std::size_t time_size{0x1000}; constexpr std::size_t hidbus_size{0x1000}; - const PAddr hid_phys_addr{system_pool.GetAddress()}; - const PAddr font_phys_addr{system_pool.GetAddress() + hid_size}; - 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}; - const PAddr hidbus_phys_addr{system_pool.GetAddress() + hid_size + font_size + irs_size + - time_size}; - hid_shared_mem = KSharedMemory::Create(system.Kernel()); font_shared_mem = KSharedMemory::Create(system.Kernel()); irs_shared_mem = KSharedMemory::Create(system.Kernel()); time_shared_mem = KSharedMemory::Create(system.Kernel()); hidbus_shared_mem = KSharedMemory::Create(system.Kernel()); - hid_shared_mem->Initialize(system.DeviceMemory(), nullptr, - {hid_phys_addr, hid_size / PageSize}, - Svc::MemoryPermission::None, Svc::MemoryPermission::Read, - hid_phys_addr, hid_size, "HID:SharedMemory"); - font_shared_mem->Initialize(system.DeviceMemory(), nullptr, - {font_phys_addr, font_size / PageSize}, - Svc::MemoryPermission::None, Svc::MemoryPermission::Read, - font_phys_addr, font_size, "Font:SharedMemory"); - irs_shared_mem->Initialize(system.DeviceMemory(), nullptr, - {irs_phys_addr, irs_size / PageSize}, - Svc::MemoryPermission::None, Svc::MemoryPermission::Read, - irs_phys_addr, irs_size, "IRS:SharedMemory"); - time_shared_mem->Initialize(system.DeviceMemory(), nullptr, - {time_phys_addr, time_size / PageSize}, - Svc::MemoryPermission::None, Svc::MemoryPermission::Read, - time_phys_addr, time_size, "Time:SharedMemory"); - hidbus_shared_mem->Initialize(system.DeviceMemory(), nullptr, - {hidbus_phys_addr, hidbus_size / PageSize}, - Svc::MemoryPermission::None, Svc::MemoryPermission::Read, - hidbus_phys_addr, hidbus_size, "HidBus:SharedMemory"); + hid_shared_mem->Initialize(system.DeviceMemory(), nullptr, Svc::MemoryPermission::None, + Svc::MemoryPermission::Read, hid_size, "HID:SharedMemory"); + font_shared_mem->Initialize(system.DeviceMemory(), nullptr, Svc::MemoryPermission::None, + Svc::MemoryPermission::Read, font_size, "Font:SharedMemory"); + irs_shared_mem->Initialize(system.DeviceMemory(), nullptr, Svc::MemoryPermission::None, + Svc::MemoryPermission::Read, irs_size, "IRS:SharedMemory"); + time_shared_mem->Initialize(system.DeviceMemory(), nullptr, Svc::MemoryPermission::None, + Svc::MemoryPermission::Read, time_size, "Time:SharedMemory"); + hidbus_shared_mem->Initialize(system.DeviceMemory(), nullptr, Svc::MemoryPermission::None, + Svc::MemoryPermission::Read, hidbus_size, + "HidBus:SharedMemory"); } KClientPort* CreateNamedServicePort(std::string name) { diff --git a/src/core/hle/service/time/clock_types.h b/src/core/hle/service/time/clock_types.h index ef070f32f..ed1eb5b2d 100644 --- a/src/core/hle/service/time/clock_types.h +++ b/src/core/hle/service/time/clock_types.h @@ -49,6 +49,7 @@ struct SteadyClockContext { static_assert(sizeof(SteadyClockContext) == 0x18, "SteadyClockContext is incorrect size"); static_assert(std::is_trivially_copyable_v<SteadyClockContext>, "SteadyClockContext must be trivially copyable"); +using StandardSteadyClockTimePointType = SteadyClockContext; struct SystemClockContext { s64 offset; diff --git a/src/core/hle/service/time/time_sharedmemory.cpp b/src/core/hle/service/time/time_sharedmemory.cpp index a3aa0e77f..ff53a7d6f 100644 --- a/src/core/hle/service/time/time_sharedmemory.cpp +++ b/src/core/hle/service/time/time_sharedmemory.cpp @@ -26,23 +26,24 @@ void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id, const Clock::SteadyClockContext context{ static_cast<u64>(current_time_point.nanoseconds - ticks_time_span.nanoseconds), clock_source_id}; - shared_memory_format.standard_steady_clock_timepoint.StoreData( - system.Kernel().GetTimeSharedMem().GetPointer(), context); + StoreToLockFreeAtomicType(&GetFormat()->standard_steady_clock_timepoint, context); } void SharedMemory::UpdateLocalSystemClockContext(const Clock::SystemClockContext& context) { - shared_memory_format.standard_local_system_clock_context.StoreData( - system.Kernel().GetTimeSharedMem().GetPointer(), context); + StoreToLockFreeAtomicType(&GetFormat()->standard_local_system_clock_context, context); } void SharedMemory::UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context) { - shared_memory_format.standard_network_system_clock_context.StoreData( - system.Kernel().GetTimeSharedMem().GetPointer(), context); + StoreToLockFreeAtomicType(&GetFormat()->standard_network_system_clock_context, context); } void SharedMemory::SetAutomaticCorrectionEnabled(bool is_enabled) { - shared_memory_format.standard_user_system_clock_automatic_correction.StoreData( - system.Kernel().GetTimeSharedMem().GetPointer(), is_enabled); + StoreToLockFreeAtomicType( + &GetFormat()->is_standard_user_system_clock_automatic_correction_enabled, is_enabled); +} + +SharedMemory::Format* SharedMemory::GetFormat() { + return reinterpret_cast<SharedMemory::Format*>(system.Kernel().GetTimeSharedMem().GetPointer()); } } // namespace Service::Time diff --git a/src/core/hle/service/time/time_sharedmemory.h b/src/core/hle/service/time/time_sharedmemory.h index 561685acd..044a4d24e 100644 --- a/src/core/hle/service/time/time_sharedmemory.h +++ b/src/core/hle/service/time/time_sharedmemory.h @@ -10,45 +10,68 @@ namespace Service::Time { +// Note: this type is not safe for concurrent writes. +template <typename T> +struct LockFreeAtomicType { + u32 counter_; + std::array<T, 2> value_; +}; + +template <typename T> +static inline void StoreToLockFreeAtomicType(LockFreeAtomicType<T>* p, const T& value) { + // Get the current counter. + auto counter = p->counter_; + + // Increment the counter. + ++counter; + + // Store the updated value. + p->value_[counter % 2] = value; + + // Fence memory. + std::atomic_thread_fence(std::memory_order_release); + + // Set the updated counter. + p->counter_ = counter; +} + +template <typename T> +static inline T LoadFromLockFreeAtomicType(const LockFreeAtomicType<T>* p) { + while (true) { + // Get the counter. + auto counter = p->counter_; + + // Get the value. + auto value = p->value_[counter % 2]; + + // Fence memory. + std::atomic_thread_fence(std::memory_order_acquire); + + // Check that the counter matches. + if (counter == p->counter_) { + return value; + } + } +} + class SharedMemory final { public: explicit SharedMemory(Core::System& system_); ~SharedMemory(); - // 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 { - static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); - u32_le read_attempt{}; - std::array<T, 2> data{}; - - // These are not actually memory barriers at the moment as we don't have multicore and all - // HLE is mutexed. This will need to properly be implemented when we start updating the time - // points on threads. As of right now, we'll be updated both values synchronously and just - // incrementing the read_attempt to indicate that we waited. - void StoreData(u8* shared_memory, T data_to_store) { - std::memcpy(this, shared_memory + Offset, sizeof(*this)); - read_attempt++; - data[read_attempt & 1] = data_to_store; - std::memcpy(shared_memory + Offset, this, sizeof(*this)); - } - - // For reading we're just going to read the last stored value. If there was no value stored - // it will just end up reading an empty value as intended. - T ReadData(u8* shared_memory) { - std::memcpy(this, shared_memory + Offset, sizeof(*this)); - return data[(read_attempt - 1) & 1]; - } - }; - // Shared memory format struct Format { - MemoryBarrier<Clock::SteadyClockContext, 0x0> standard_steady_clock_timepoint; - MemoryBarrier<Clock::SystemClockContext, 0x38> standard_local_system_clock_context; - MemoryBarrier<Clock::SystemClockContext, 0x80> standard_network_system_clock_context; - MemoryBarrier<bool, 0xc8> standard_user_system_clock_automatic_correction; - u32_le format_version; + LockFreeAtomicType<Clock::StandardSteadyClockTimePointType> standard_steady_clock_timepoint; + LockFreeAtomicType<Clock::SystemClockContext> standard_local_system_clock_context; + LockFreeAtomicType<Clock::SystemClockContext> standard_network_system_clock_context; + LockFreeAtomicType<bool> is_standard_user_system_clock_automatic_correction_enabled; + u32 format_version; }; + static_assert(offsetof(Format, standard_steady_clock_timepoint) == 0x0); + static_assert(offsetof(Format, standard_local_system_clock_context) == 0x38); + static_assert(offsetof(Format, standard_network_system_clock_context) == 0x80); + static_assert(offsetof(Format, is_standard_user_system_clock_automatic_correction_enabled) == + 0xc8); static_assert(sizeof(Format) == 0xd8, "Format is an invalid size"); void SetupStandardSteadyClock(const Common::UUID& clock_source_id, @@ -56,10 +79,10 @@ public: void UpdateLocalSystemClockContext(const Clock::SystemClockContext& context); void UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context); void SetAutomaticCorrectionEnabled(bool is_enabled); + Format* GetFormat(); private: Core::System& system; - Format shared_memory_format{}; }; } // namespace Service::Time |