diff options
Diffstat (limited to 'src/core/hle/kernel')
23 files changed, 854 insertions, 160 deletions
diff --git a/src/core/hle/kernel/k_code_memory.cpp b/src/core/hle/kernel/k_code_memory.cpp index 3583bee44..7454be55c 100644 --- a/src/core/hle/kernel/k_code_memory.cpp +++ b/src/core/hle/kernel/k_code_memory.cpp @@ -25,7 +25,7 @@ Result KCodeMemory::Initialize(Core::DeviceMemory& device_memory, KProcessAddres m_owner = GetCurrentProcessPointer(m_kernel); // Get the owner page table. - auto& page_table = m_owner->PageTable(); + auto& page_table = m_owner->GetPageTable(); // Construct the page group. m_page_group.emplace(m_kernel, page_table.GetBlockInfoManager()); @@ -53,7 +53,7 @@ void KCodeMemory::Finalize() { // Unlock. if (!m_is_mapped && !m_is_owner_mapped) { const size_t size = m_page_group->GetNumPages() * PageSize; - m_owner->PageTable().UnlockForCodeMemory(m_address, size, *m_page_group); + m_owner->GetPageTable().UnlockForCodeMemory(m_address, size, *m_page_group); } // Close the page group. @@ -75,7 +75,7 @@ Result KCodeMemory::Map(KProcessAddress address, size_t size) { R_UNLESS(!m_is_mapped, ResultInvalidState); // Map the memory. - R_TRY(GetCurrentProcess(m_kernel).PageTable().MapPageGroup( + R_TRY(GetCurrentProcess(m_kernel).GetPageTable().MapPageGroup( address, *m_page_group, KMemoryState::CodeOut, KMemoryPermission::UserReadWrite)); // Mark ourselves as mapped. @@ -92,8 +92,8 @@ Result KCodeMemory::Unmap(KProcessAddress address, size_t size) { KScopedLightLock lk(m_lock); // Unmap the memory. - R_TRY(GetCurrentProcess(m_kernel).PageTable().UnmapPageGroup(address, *m_page_group, - KMemoryState::CodeOut)); + R_TRY(GetCurrentProcess(m_kernel).GetPageTable().UnmapPageGroup(address, *m_page_group, + KMemoryState::CodeOut)); // Mark ourselves as unmapped. m_is_mapped = false; @@ -126,8 +126,8 @@ Result KCodeMemory::MapToOwner(KProcessAddress address, size_t size, Svc::Memory } // Map the memory. - R_TRY(m_owner->PageTable().MapPageGroup(address, *m_page_group, KMemoryState::GeneratedCode, - k_perm)); + R_TRY(m_owner->GetPageTable().MapPageGroup(address, *m_page_group, KMemoryState::GeneratedCode, + k_perm)); // Mark ourselves as mapped. m_is_owner_mapped = true; @@ -143,7 +143,8 @@ Result KCodeMemory::UnmapFromOwner(KProcessAddress address, size_t size) { KScopedLightLock lk(m_lock); // Unmap the memory. - R_TRY(m_owner->PageTable().UnmapPageGroup(address, *m_page_group, KMemoryState::GeneratedCode)); + R_TRY(m_owner->GetPageTable().UnmapPageGroup(address, *m_page_group, + KMemoryState::GeneratedCode)); // Mark ourselves as unmapped. m_is_owner_mapped = false; diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index 022d15f35..b9e8c6042 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -388,39 +388,6 @@ public: constexpr size_t GetHeapSize() const { return m_current_heap_end - m_heap_region_start; } - constexpr bool IsInsideAddressSpace(KProcessAddress address, size_t size) const { - return m_address_space_start <= address && address + size - 1 <= m_address_space_end - 1; - } - constexpr bool IsOutsideAliasRegion(KProcessAddress address, size_t size) const { - return m_alias_region_start > address || address + size - 1 > m_alias_region_end - 1; - } - constexpr bool IsOutsideStackRegion(KProcessAddress address, size_t size) const { - return m_stack_region_start > address || address + size - 1 > m_stack_region_end - 1; - } - constexpr bool IsInvalidRegion(KProcessAddress address, size_t size) const { - return address + size - 1 > GetAliasCodeRegionStart() + GetAliasCodeRegionSize() - 1; - } - constexpr bool IsInsideHeapRegion(KProcessAddress address, size_t size) const { - return address + size > m_heap_region_start && m_heap_region_end > address; - } - constexpr bool IsInsideAliasRegion(KProcessAddress address, size_t size) const { - return address + size > m_alias_region_start && m_alias_region_end > address; - } - constexpr bool IsOutsideASLRRegion(KProcessAddress address, size_t size) const { - if (IsInvalidRegion(address, size)) { - return true; - } - if (IsInsideHeapRegion(address, size)) { - return true; - } - if (IsInsideAliasRegion(address, size)) { - return true; - } - return {}; - } - constexpr bool IsInsideASLRRegion(KProcessAddress address, size_t size) const { - return !IsOutsideASLRRegion(address, size); - } constexpr size_t GetNumGuardPages() const { return IsKernel() ? 1 : 4; } @@ -436,6 +403,14 @@ public: return m_address_space_start <= addr && addr < addr + size && addr + size - 1 <= m_address_space_end - 1; } + constexpr bool IsInAliasRegion(KProcessAddress addr, size_t size) const { + return this->Contains(addr, size) && m_alias_region_start <= addr && + addr + size - 1 <= m_alias_region_end - 1; + } + constexpr bool IsInHeapRegion(KProcessAddress addr, size_t size) const { + return this->Contains(addr, size) && m_heap_region_start <= addr && + addr + size - 1 <= m_heap_region_end - 1; + } public: static KVirtualAddress GetLinearMappedVirtualAddress(const KMemoryLayout& layout, diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index efe86ad27..44c7cb22f 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -38,7 +38,7 @@ namespace { */ void SetupMainThread(Core::System& system, KProcess& owner_process, u32 priority, KProcessAddress stack_top) { - const KProcessAddress entry_point = owner_process.PageTable().GetCodeRegionStart(); + const KProcessAddress entry_point = owner_process.GetPageTable().GetCodeRegionStart(); ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::ThreadCountMax, 1)); KThread* thread = KThread::Create(system.Kernel()); diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h index 925981d06..c9b37e138 100644 --- a/src/core/hle/kernel/k_process.h +++ b/src/core/hle/kernel/k_process.h @@ -110,16 +110,6 @@ public: ProcessType type, KResourceLimit* res_limit); /// Gets a reference to the process' page table. - KPageTable& PageTable() { - return m_page_table; - } - - /// Gets const a reference to the process' page table. - const KPageTable& PageTable() const { - return m_page_table; - } - - /// Gets a reference to the process' page table. KPageTable& GetPageTable() { return m_page_table; } diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index c66aff501..c64ceb530 100644 --- a/src/core/hle/kernel/k_server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -20,12 +20,132 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread_queue.h" #include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/message_buffer.h" #include "core/hle/service/hle_ipc.h" #include "core/hle/service/ipc_helpers.h" #include "core/memory.h" namespace Kernel { +namespace { + +template <bool MoveHandleAllowed> +Result ProcessMessageSpecialData(KProcess& dst_process, KProcess& src_process, KThread& src_thread, + MessageBuffer& dst_msg, const MessageBuffer& src_msg, + MessageBuffer::SpecialHeader& src_special_header) { + // Copy the special header to the destination. + s32 offset = dst_msg.Set(src_special_header); + + // Copy the process ID. + if (src_special_header.GetHasProcessId()) { + offset = dst_msg.SetProcessId(offset, src_process.GetProcessId()); + } + + // Prepare to process handles. + auto& dst_handle_table = dst_process.GetHandleTable(); + auto& src_handle_table = src_process.GetHandleTable(); + Result result = ResultSuccess; + + // Process copy handles. + for (auto i = 0; i < src_special_header.GetCopyHandleCount(); ++i) { + // Get the handles. + const Handle src_handle = src_msg.GetHandle(offset); + Handle dst_handle = Svc::InvalidHandle; + + // If we're in a success state, try to move the handle to the new table. + if (R_SUCCEEDED(result) && src_handle != Svc::InvalidHandle) { + KScopedAutoObject obj = + src_handle_table.GetObjectForIpc(src_handle, std::addressof(src_thread)); + if (obj.IsNotNull()) { + Result add_result = + dst_handle_table.Add(std::addressof(dst_handle), obj.GetPointerUnsafe()); + if (R_FAILED(add_result)) { + result = add_result; + dst_handle = Svc::InvalidHandle; + } + } else { + result = ResultInvalidHandle; + } + } + + // Set the handle. + offset = dst_msg.SetHandle(offset, dst_handle); + } + + // Process move handles. + if constexpr (MoveHandleAllowed) { + for (auto i = 0; i < src_special_header.GetMoveHandleCount(); ++i) { + // Get the handles. + const Handle src_handle = src_msg.GetHandle(offset); + Handle dst_handle = Svc::InvalidHandle; + + // Whether or not we've succeeded, we need to remove the handles from the source table. + if (src_handle != Svc::InvalidHandle) { + if (R_SUCCEEDED(result)) { + KScopedAutoObject obj = + src_handle_table.GetObjectForIpcWithoutPseudoHandle(src_handle); + if (obj.IsNotNull()) { + Result add_result = dst_handle_table.Add(std::addressof(dst_handle), + obj.GetPointerUnsafe()); + + src_handle_table.Remove(src_handle); + + if (R_FAILED(add_result)) { + result = add_result; + dst_handle = Svc::InvalidHandle; + } + } else { + result = ResultInvalidHandle; + } + } else { + src_handle_table.Remove(src_handle); + } + } + + // Set the handle. + offset = dst_msg.SetHandle(offset, dst_handle); + } + } + + R_RETURN(result); +} + +void CleanupSpecialData(KProcess& dst_process, u32* dst_msg_ptr, size_t dst_buffer_size) { + // Parse the message. + const MessageBuffer dst_msg(dst_msg_ptr, dst_buffer_size); + const MessageBuffer::MessageHeader dst_header(dst_msg); + const MessageBuffer::SpecialHeader dst_special_header(dst_msg, dst_header); + + // Check that the size is big enough. + if (MessageBuffer::GetMessageBufferSize(dst_header, dst_special_header) > dst_buffer_size) { + return; + } + + // Set the special header. + int offset = dst_msg.Set(dst_special_header); + + // Clear the process id, if needed. + if (dst_special_header.GetHasProcessId()) { + offset = dst_msg.SetProcessId(offset, 0); + } + + // Clear handles, as relevant. + auto& dst_handle_table = dst_process.GetHandleTable(); + for (auto i = 0; + i < (dst_special_header.GetCopyHandleCount() + dst_special_header.GetMoveHandleCount()); + ++i) { + const Handle handle = dst_msg.GetHandle(offset); + + if (handle != Svc::InvalidHandle) { + dst_handle_table.Remove(handle); + } + + offset = dst_msg.SetHandle(offset, Svc::InvalidHandle); + } +} + +} // namespace + using ThreadQueueImplForKServerSessionRequest = KThreadQueue; KServerSession::KServerSession(KernelCore& kernel) @@ -223,12 +343,27 @@ Result KServerSession::SendReply(bool is_hle) { // the reply has already been written in this case. } else { Core::Memory::Memory& memory{client_thread->GetOwnerProcess()->GetMemory()}; - KThread* server_thread{GetCurrentThreadPointer(m_kernel)}; + KThread* server_thread = GetCurrentThreadPointer(m_kernel); + KProcess& src_process = *client_thread->GetOwnerProcess(); + KProcess& dst_process = *server_thread->GetOwnerProcess(); UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess()); - auto* src_msg_buffer = memory.GetPointer(server_thread->GetTlsAddress()); - auto* dst_msg_buffer = memory.GetPointer(client_message); + auto* src_msg_buffer = memory.GetPointer<u32>(server_thread->GetTlsAddress()); + auto* dst_msg_buffer = memory.GetPointer<u32>(client_message); std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size); + + // Translate special header ad-hoc. + MessageBuffer src_msg(src_msg_buffer, client_buffer_size); + MessageBuffer::MessageHeader src_header(src_msg); + MessageBuffer::SpecialHeader src_special_header(src_msg, src_header); + if (src_header.GetHasSpecialHeader()) { + MessageBuffer dst_msg(dst_msg_buffer, client_buffer_size); + result = ProcessMessageSpecialData<true>(dst_process, src_process, *server_thread, + dst_msg, src_msg, src_special_header); + if (R_FAILED(result)) { + CleanupSpecialData(dst_process, dst_msg_buffer, client_buffer_size); + } + } } } else { result = ResultSessionClosed; @@ -330,12 +465,28 @@ Result KServerSession::ReceiveRequest(std::shared_ptr<Service::HLERequestContext ->PopulateFromIncomingCommandBuffer(client_thread->GetOwnerProcess()->GetHandleTable(), cmd_buf); } else { - KThread* server_thread{GetCurrentThreadPointer(m_kernel)}; - UNIMPLEMENTED_IF(server_thread->GetOwnerProcess() != client_thread->GetOwnerProcess()); + KThread* server_thread = GetCurrentThreadPointer(m_kernel); + KProcess& src_process = *client_thread->GetOwnerProcess(); + KProcess& dst_process = *server_thread->GetOwnerProcess(); + UNIMPLEMENTED_IF(client_thread->GetOwnerProcess() != server_thread->GetOwnerProcess()); - auto* src_msg_buffer = memory.GetPointer(client_message); - auto* dst_msg_buffer = memory.GetPointer(server_thread->GetTlsAddress()); + auto* src_msg_buffer = memory.GetPointer<u32>(client_message); + auto* dst_msg_buffer = memory.GetPointer<u32>(server_thread->GetTlsAddress()); std::memcpy(dst_msg_buffer, src_msg_buffer, client_buffer_size); + + // Translate special header ad-hoc. + // TODO: fix this mess + MessageBuffer src_msg(src_msg_buffer, client_buffer_size); + MessageBuffer::MessageHeader src_header(src_msg); + MessageBuffer::SpecialHeader src_special_header(src_msg, src_header); + if (src_header.GetHasSpecialHeader()) { + MessageBuffer dst_msg(dst_msg_buffer, client_buffer_size); + Result res = ProcessMessageSpecialData<false>(dst_process, src_process, *client_thread, + dst_msg, src_msg, src_special_header); + if (R_FAILED(res)) { + CleanupSpecialData(dst_process, dst_msg_buffer, client_buffer_size); + } + } } // We succeeded. diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index efb5699de..f713968f6 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -90,8 +90,8 @@ Result KSharedMemory::Map(KProcess& target_process, KProcessAddress address, std R_UNLESS(map_perm == test_perm, ResultInvalidNewMemoryPermission); } - R_RETURN(target_process.PageTable().MapPageGroup(address, *m_page_group, KMemoryState::Shared, - ConvertToKMemoryPermission(map_perm))); + R_RETURN(target_process.GetPageTable().MapPageGroup( + address, *m_page_group, KMemoryState::Shared, ConvertToKMemoryPermission(map_perm))); } Result KSharedMemory::Unmap(KProcess& target_process, KProcessAddress address, @@ -100,7 +100,7 @@ Result KSharedMemory::Unmap(KProcess& target_process, KProcessAddress address, R_UNLESS(m_size == unmap_size, ResultInvalidSize); R_RETURN( - target_process.PageTable().UnmapPageGroup(address, *m_page_group, KMemoryState::Shared)); + target_process.GetPageTable().UnmapPageGroup(address, *m_page_group, KMemoryState::Shared)); } } // namespace Kernel diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index adb6ec581..d88909889 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -302,12 +302,12 @@ Result KThread::InitializeServiceThread(Core::System& system, KThread* thread, std::function<void()>&& func, s32 prio, s32 virt_core, KProcess* owner) { system.Kernel().GlobalSchedulerContext().AddThread(thread); - std::function<void()> func2{[&system, func{std::move(func)}] { + std::function<void()> func2{[&system, func_{std::move(func)}] { // Similar to UserModeThreadStarter. system.Kernel().CurrentScheduler()->OnThreadStart(); // Run the guest function. - func(); + func_(); // Exit. Svc::ExitThread(system); diff --git a/src/core/hle/kernel/k_thread_local_page.cpp b/src/core/hle/kernel/k_thread_local_page.cpp index b4a1e3cdb..2c45b4232 100644 --- a/src/core/hle/kernel/k_thread_local_page.cpp +++ b/src/core/hle/kernel/k_thread_local_page.cpp @@ -25,9 +25,9 @@ Result KThreadLocalPage::Initialize(KernelCore& kernel, KProcess* process) { // Map the address in. const auto phys_addr = kernel.System().DeviceMemory().GetPhysicalAddr(page_buf); - R_TRY(m_owner->PageTable().MapPages(std::addressof(m_virt_addr), 1, PageSize, phys_addr, - KMemoryState::ThreadLocal, - KMemoryPermission::UserReadWrite)); + R_TRY(m_owner->GetPageTable().MapPages(std::addressof(m_virt_addr), 1, PageSize, phys_addr, + KMemoryState::ThreadLocal, + KMemoryPermission::UserReadWrite)); // We succeeded. page_buf_guard.Cancel(); @@ -37,11 +37,11 @@ Result KThreadLocalPage::Initialize(KernelCore& kernel, KProcess* process) { Result KThreadLocalPage::Finalize() { // Get the physical address of the page. - const KPhysicalAddress phys_addr = m_owner->PageTable().GetPhysicalAddr(m_virt_addr); + const KPhysicalAddress phys_addr = m_owner->GetPageTable().GetPhysicalAddr(m_virt_addr); ASSERT(phys_addr); // Unmap the page. - R_TRY(m_owner->PageTable().UnmapPages(this->GetAddress(), 1, KMemoryState::ThreadLocal)); + R_TRY(m_owner->GetPageTable().UnmapPages(this->GetAddress(), 1, KMemoryState::ThreadLocal)); // Free the page. KPageBuffer::Free(*m_kernel, KPageBuffer::FromPhysicalAddress(m_kernel->System(), phys_addr)); diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index f33600ca5..ebe7582c6 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -1089,15 +1089,15 @@ static std::jthread RunHostThreadFunc(KernelCore& kernel, KProcess* process, KThread::Register(kernel, thread); return std::jthread( - [&kernel, thread, thread_name{std::move(thread_name)}, func{std::move(func)}] { + [&kernel, thread, thread_name_{std::move(thread_name)}, func_{std::move(func)}] { // Set the thread name. - Common::SetCurrentThreadName(thread_name.c_str()); + Common::SetCurrentThreadName(thread_name_.c_str()); // Set the thread as current. kernel.RegisterHostThread(thread); // Run the callback. - func(); + func_(); // Close the thread. // This will free the process if it is the last reference. diff --git a/src/core/hle/kernel/message_buffer.h b/src/core/hle/kernel/message_buffer.h new file mode 100644 index 000000000..75b275310 --- /dev/null +++ b/src/core/hle/kernel/message_buffer.h @@ -0,0 +1,612 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/alignment.h" +#include "common/bit_field.h" +#include "core/hle/kernel/k_thread.h" + +namespace Kernel { + +constexpr inline size_t MessageBufferSize = 0x100; + +class MessageBuffer { +public: + class MessageHeader { + private: + static constexpr inline u64 NullTag = 0; + + public: + enum class ReceiveListCountType : u32 { + None = 0, + ToMessageBuffer = 1, + ToSingleBuffer = 2, + + CountOffset = 2, + CountMax = 13, + }; + + private: + union { + std::array<u32, 2> raw; + + struct { + // Define fields for the first header word. + union { + BitField<0, 16, u16> tag; + BitField<16, 4, u32> pointer_count; + BitField<20, 4, u32> send_count; + BitField<24, 4, u32> receive_count; + BitField<28, 4, u32> exchange_count; + }; + + // Define fields for the second header word. + union { + BitField<0, 10, u32> raw_count; + BitField<10, 4, ReceiveListCountType> receive_list_count; + BitField<14, 6, u32> reserved0; + BitField<20, 11, u32> receive_list_offset; + BitField<31, 1, u32> has_special_header; + }; + }; + } m_header; + + public: + constexpr MessageHeader() : m_header{} {} + + constexpr MessageHeader(u16 tag, bool special, s32 ptr, s32 send, s32 recv, s32 exch, + s32 raw, ReceiveListCountType recv_list) + : m_header{} { + m_header.raw[0] = 0; + m_header.raw[1] = 0; + + m_header.tag.Assign(tag); + m_header.pointer_count.Assign(ptr); + m_header.send_count.Assign(send); + m_header.receive_count.Assign(recv); + m_header.exchange_count.Assign(exch); + + m_header.raw_count.Assign(raw); + m_header.receive_list_count.Assign(recv_list); + m_header.has_special_header.Assign(special); + } + + explicit MessageHeader(const MessageBuffer& buf) : m_header{} { + buf.Get(0, m_header.raw.data(), 2); + } + + explicit MessageHeader(const u32* msg) : m_header{{msg[0], msg[1]}} {} + + constexpr u16 GetTag() const { + return m_header.tag; + } + + constexpr s32 GetPointerCount() const { + return m_header.pointer_count; + } + + constexpr s32 GetSendCount() const { + return m_header.send_count; + } + + constexpr s32 GetReceiveCount() const { + return m_header.receive_count; + } + + constexpr s32 GetExchangeCount() const { + return m_header.exchange_count; + } + + constexpr s32 GetMapAliasCount() const { + return this->GetSendCount() + this->GetReceiveCount() + this->GetExchangeCount(); + } + + constexpr s32 GetRawCount() const { + return m_header.raw_count; + } + + constexpr ReceiveListCountType GetReceiveListCount() const { + return m_header.receive_list_count; + } + + constexpr s32 GetReceiveListOffset() const { + return m_header.receive_list_offset; + } + + constexpr bool GetHasSpecialHeader() const { + return m_header.has_special_header.Value() != 0; + } + + constexpr void SetReceiveListCount(ReceiveListCountType recv_list) { + m_header.receive_list_count.Assign(recv_list); + } + + constexpr const u32* GetData() const { + return m_header.raw.data(); + } + + static constexpr size_t GetDataSize() { + return sizeof(m_header); + } + }; + + class SpecialHeader { + private: + union { + std::array<u32, 1> raw; + + // Define fields for the header word. + BitField<0, 1, u32> has_process_id; + BitField<1, 4, u32> copy_handle_count; + BitField<5, 4, u32> move_handle_count; + } m_header; + bool m_has_header; + + public: + constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move) + : m_header{}, m_has_header(true) { + m_header.has_process_id.Assign(pid); + m_header.copy_handle_count.Assign(copy); + m_header.move_handle_count.Assign(move); + } + + constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move, bool _has_header) + : m_header{}, m_has_header(_has_header) { + m_header.has_process_id.Assign(pid); + m_header.copy_handle_count.Assign(copy); + m_header.move_handle_count.Assign(move); + } + + explicit SpecialHeader(const MessageBuffer& buf, const MessageHeader& hdr) + : m_header{}, m_has_header(hdr.GetHasSpecialHeader()) { + if (m_has_header) { + buf.Get(static_cast<s32>(MessageHeader::GetDataSize() / sizeof(u32)), + m_header.raw.data(), sizeof(m_header) / sizeof(u32)); + } + } + + constexpr bool GetHasProcessId() const { + return m_header.has_process_id.Value() != 0; + } + + constexpr s32 GetCopyHandleCount() const { + return m_header.copy_handle_count; + } + + constexpr s32 GetMoveHandleCount() const { + return m_header.move_handle_count; + } + + constexpr const u32* GetHeader() const { + return m_header.raw.data(); + } + + constexpr size_t GetHeaderSize() const { + if (m_has_header) { + return sizeof(m_header); + } else { + return 0; + } + } + + constexpr size_t GetDataSize() const { + if (m_has_header) { + return (this->GetHasProcessId() ? sizeof(u64) : 0) + + (this->GetCopyHandleCount() * sizeof(Handle)) + + (this->GetMoveHandleCount() * sizeof(Handle)); + } else { + return 0; + } + } + }; + + class MapAliasDescriptor { + public: + enum class Attribute : u32 { + Ipc = 0, + NonSecureIpc = 1, + NonDeviceIpc = 3, + }; + + private: + static constexpr u32 SizeLowCount = 32; + static constexpr u32 SizeHighCount = 4; + static constexpr u32 AddressLowCount = 32; + static constexpr u32 AddressMidCount = 4; + + constexpr u32 GetAddressMid(u64 address) { + return static_cast<u32>(address >> AddressLowCount) & ((1U << AddressMidCount) - 1); + } + + constexpr u32 GetAddressHigh(u64 address) { + return static_cast<u32>(address >> (AddressLowCount + AddressMidCount)); + } + + private: + union { + std::array<u32, 3> raw; + + struct { + // Define fields for the first two words. + u32 size_low; + u32 address_low; + + // Define fields for the packed descriptor word. + union { + BitField<0, 2, Attribute> attributes; + BitField<2, 3, u32> address_high; + BitField<5, 19, u32> reserved; + BitField<24, 4, u32> size_high; + BitField<28, 4, u32> address_mid; + }; + }; + } m_data; + + public: + constexpr MapAliasDescriptor() : m_data{} {} + + MapAliasDescriptor(const void* buffer, size_t _size, Attribute attr = Attribute::Ipc) + : m_data{} { + const u64 address = reinterpret_cast<u64>(buffer); + const u64 size = static_cast<u64>(_size); + m_data.size_low = static_cast<u32>(size); + m_data.address_low = static_cast<u32>(address); + m_data.attributes.Assign(attr); + m_data.address_mid.Assign(GetAddressMid(address)); + m_data.size_high.Assign(static_cast<u32>(size >> SizeLowCount)); + m_data.address_high.Assign(GetAddressHigh(address)); + } + + MapAliasDescriptor(const MessageBuffer& buf, s32 index) : m_data{} { + buf.Get(index, m_data.raw.data(), 3); + } + + constexpr uintptr_t GetAddress() const { + return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid) + << AddressLowCount) | + m_data.address_low; + } + + constexpr uintptr_t GetSize() const { + return (static_cast<u64>(m_data.size_high) << SizeLowCount) | m_data.size_low; + } + + constexpr Attribute GetAttribute() const { + return m_data.attributes; + } + + constexpr const u32* GetData() const { + return m_data.raw.data(); + } + + static constexpr size_t GetDataSize() { + return sizeof(m_data); + } + }; + + class PointerDescriptor { + private: + static constexpr u32 AddressLowCount = 32; + static constexpr u32 AddressMidCount = 4; + + constexpr u32 GetAddressMid(u64 address) { + return static_cast<u32>(address >> AddressLowCount) & ((1u << AddressMidCount) - 1); + } + + constexpr u32 GetAddressHigh(u64 address) { + return static_cast<u32>(address >> (AddressLowCount + AddressMidCount)); + } + + private: + union { + std::array<u32, 2> raw; + + struct { + // Define fields for the packed descriptor word. + union { + BitField<0, 4, u32> index; + BitField<4, 2, u32> reserved0; + BitField<6, 3, u32> address_high; + BitField<9, 3, u32> reserved1; + BitField<12, 4, u32> address_mid; + BitField<16, 16, u32> size; + }; + + // Define fields for the second word. + u32 address_low; + }; + } m_data; + + public: + constexpr PointerDescriptor() : m_data{} {} + + PointerDescriptor(const void* buffer, size_t size, s32 index) : m_data{} { + const u64 address = reinterpret_cast<u64>(buffer); + + m_data.index.Assign(index); + m_data.address_high.Assign(GetAddressHigh(address)); + m_data.address_mid.Assign(GetAddressMid(address)); + m_data.size.Assign(static_cast<u32>(size)); + + m_data.address_low = static_cast<u32>(address); + } + + PointerDescriptor(const MessageBuffer& buf, s32 index) : m_data{} { + buf.Get(index, m_data.raw.data(), 2); + } + + constexpr s32 GetIndex() const { + return m_data.index; + } + + constexpr uintptr_t GetAddress() const { + return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid) + << AddressLowCount) | + m_data.address_low; + } + + constexpr size_t GetSize() const { + return m_data.size; + } + + constexpr const u32* GetData() const { + return m_data.raw.data(); + } + + static constexpr size_t GetDataSize() { + return sizeof(m_data); + } + }; + + class ReceiveListEntry { + private: + static constexpr u32 AddressLowCount = 32; + + constexpr u32 GetAddressHigh(u64 address) { + return static_cast<u32>(address >> (AddressLowCount)); + } + + private: + union { + std::array<u32, 2> raw; + + struct { + // Define fields for the first word. + u32 address_low; + + // Define fields for the packed descriptor word. + union { + BitField<0, 7, u32> address_high; + BitField<7, 9, u32> reserved; + BitField<16, 16, u32> size; + }; + }; + } m_data; + + public: + constexpr ReceiveListEntry() : m_data{} {} + + ReceiveListEntry(const void* buffer, size_t size) : m_data{} { + const u64 address = reinterpret_cast<u64>(buffer); + + m_data.address_low = static_cast<u32>(address); + + m_data.address_high.Assign(GetAddressHigh(address)); + m_data.size.Assign(static_cast<u32>(size)); + } + + ReceiveListEntry(u32 a, u32 b) : m_data{{a, b}} {} + + constexpr uintptr_t GetAddress() const { + return (static_cast<u64>(m_data.address_high) << AddressLowCount) | m_data.address_low; + } + + constexpr size_t GetSize() const { + return m_data.size; + } + + constexpr const u32* GetData() const { + return m_data.raw.data(); + } + + static constexpr size_t GetDataSize() { + return sizeof(m_data); + } + }; + +private: + u32* m_buffer; + size_t m_size; + +public: + constexpr MessageBuffer(u32* b, size_t sz) : m_buffer(b), m_size(sz) {} + constexpr explicit MessageBuffer(u32* b) : m_buffer(b), m_size(MessageBufferSize) {} + + constexpr void* GetBufferForDebug() const { + return m_buffer; + } + + constexpr size_t GetBufferSize() const { + return m_size; + } + + void Get(s32 index, u32* dst, size_t count) const { + // Ensure that this doesn't get re-ordered. + std::atomic_thread_fence(std::memory_order_seq_cst); + + // Get the words. + static_assert(sizeof(*dst) == sizeof(*m_buffer)); + + memcpy(dst, m_buffer + index, count * sizeof(*dst)); + } + + s32 Set(s32 index, u32* src, size_t count) const { + // Ensure that this doesn't get re-ordered. + std::atomic_thread_fence(std::memory_order_seq_cst); + + // Set the words. + memcpy(m_buffer + index, src, count * sizeof(*src)); + + // Ensure that this doesn't get re-ordered. + std::atomic_thread_fence(std::memory_order_seq_cst); + + return static_cast<s32>(index + count); + } + + template <typename T> + const T& GetRaw(s32 index) const { + return *reinterpret_cast<const T*>(m_buffer + index); + } + + template <typename T> + s32 SetRaw(s32 index, const T& val) const { + *reinterpret_cast<const T*>(m_buffer + index) = val; + return index + (Common::AlignUp(sizeof(val), sizeof(*m_buffer)) / sizeof(*m_buffer)); + } + + void GetRawArray(s32 index, void* dst, size_t len) const { + memcpy(dst, m_buffer + index, len); + } + + void SetRawArray(s32 index, const void* src, size_t len) const { + memcpy(m_buffer + index, src, len); + } + + void SetNull() const { + this->Set(MessageHeader()); + } + + s32 Set(const MessageHeader& hdr) const { + memcpy(m_buffer, hdr.GetData(), hdr.GetDataSize()); + return static_cast<s32>(hdr.GetDataSize() / sizeof(*m_buffer)); + } + + s32 Set(const SpecialHeader& spc) const { + const s32 index = static_cast<s32>(MessageHeader::GetDataSize() / sizeof(*m_buffer)); + memcpy(m_buffer + index, spc.GetHeader(), spc.GetHeaderSize()); + return static_cast<s32>(index + (spc.GetHeaderSize() / sizeof(*m_buffer))); + } + + s32 SetHandle(s32 index, const Handle& hnd) const { + memcpy(m_buffer + index, std::addressof(hnd), sizeof(hnd)); + return static_cast<s32>(index + (sizeof(hnd) / sizeof(*m_buffer))); + } + + s32 SetProcessId(s32 index, const u64 pid) const { + memcpy(m_buffer + index, std::addressof(pid), sizeof(pid)); + return static_cast<s32>(index + (sizeof(pid) / sizeof(*m_buffer))); + } + + s32 Set(s32 index, const MapAliasDescriptor& desc) const { + memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize()); + return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer))); + } + + s32 Set(s32 index, const PointerDescriptor& desc) const { + memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize()); + return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer))); + } + + s32 Set(s32 index, const ReceiveListEntry& desc) const { + memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize()); + return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer))); + } + + s32 Set(s32 index, const u32 val) const { + memcpy(m_buffer + index, std::addressof(val), sizeof(val)); + return static_cast<s32>(index + (sizeof(val) / sizeof(*m_buffer))); + } + + Result GetAsyncResult() const { + MessageHeader hdr(m_buffer); + MessageHeader null{}; + if (memcmp(hdr.GetData(), null.GetData(), MessageHeader::GetDataSize()) != 0) [[unlikely]] { + R_SUCCEED(); + } + return Result(m_buffer[MessageHeader::GetDataSize() / sizeof(*m_buffer)]); + } + + void SetAsyncResult(Result res) const { + const s32 index = this->Set(MessageHeader()); + const auto value = res.raw; + memcpy(m_buffer + index, std::addressof(value), sizeof(value)); + } + + u32 Get32(s32 index) const { + return m_buffer[index]; + } + + u64 Get64(s32 index) const { + u64 value; + memcpy(std::addressof(value), m_buffer + index, sizeof(value)); + return value; + } + + u64 GetProcessId(s32 index) const { + return this->Get64(index); + } + + Handle GetHandle(s32 index) const { + static_assert(sizeof(Handle) == sizeof(*m_buffer)); + return Handle(m_buffer[index]); + } + + static constexpr s32 GetSpecialDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) { + return static_cast<s32>((MessageHeader::GetDataSize() / sizeof(u32)) + + (spc.GetHeaderSize() / sizeof(u32))); + } + + static constexpr s32 GetPointerDescriptorIndex(const MessageHeader& hdr, + const SpecialHeader& spc) { + return static_cast<s32>(GetSpecialDataIndex(hdr, spc) + (spc.GetDataSize() / sizeof(u32))); + } + + static constexpr s32 GetMapAliasDescriptorIndex(const MessageHeader& hdr, + const SpecialHeader& spc) { + return GetPointerDescriptorIndex(hdr, spc) + + static_cast<s32>(hdr.GetPointerCount() * PointerDescriptor::GetDataSize() / + sizeof(u32)); + } + + static constexpr s32 GetRawDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) { + return GetMapAliasDescriptorIndex(hdr, spc) + + static_cast<s32>(hdr.GetMapAliasCount() * MapAliasDescriptor::GetDataSize() / + sizeof(u32)); + } + + static constexpr s32 GetReceiveListIndex(const MessageHeader& hdr, const SpecialHeader& spc) { + if (const s32 recv_list_index = hdr.GetReceiveListOffset()) { + return recv_list_index; + } else { + return GetRawDataIndex(hdr, spc) + hdr.GetRawCount(); + } + } + + static constexpr size_t GetMessageBufferSize(const MessageHeader& hdr, + const SpecialHeader& spc) { + // Get the size of the plain message. + size_t msg_size = GetReceiveListIndex(hdr, spc) * sizeof(u32); + + // Add the size of the receive list. + const auto count = hdr.GetReceiveListCount(); + switch (count) { + case MessageHeader::ReceiveListCountType::None: + break; + case MessageHeader::ReceiveListCountType::ToMessageBuffer: + break; + case MessageHeader::ReceiveListCountType::ToSingleBuffer: + msg_size += ReceiveListEntry::GetDataSize(); + break; + default: + msg_size += (static_cast<s32>(count) - + static_cast<s32>(MessageHeader::ReceiveListCountType::CountOffset)) * + ReceiveListEntry::GetDataSize(); + break; + } + + return msg_size; + } +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/physical_core.cpp b/src/core/hle/kernel/physical_core.cpp index 2e0c36129..5ee869fa2 100644 --- a/src/core/hle/kernel/physical_core.cpp +++ b/src/core/hle/kernel/physical_core.cpp @@ -17,7 +17,9 @@ PhysicalCore::PhysicalCore(std::size_t core_index, Core::System& system, KSchedu // a 32-bit instance of Dynarmic. This should be abstracted out to a CPU manager. auto& kernel = system.Kernel(); m_arm_interface = std::make_unique<Core::ARM_Dynarmic_64>( - system, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), m_core_index); + system, kernel.IsMulticore(), + reinterpret_cast<Core::DynarmicExclusiveMonitor&>(kernel.GetExclusiveMonitor()), + m_core_index); #else #error Platform not supported yet. #endif @@ -31,7 +33,9 @@ void PhysicalCore::Initialize(bool is_64_bit) { if (!is_64_bit) { // We already initialized a 64-bit core, replace with a 32-bit one. m_arm_interface = std::make_unique<Core::ARM_Dynarmic_32>( - m_system, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), m_core_index); + m_system, kernel.IsMulticore(), + reinterpret_cast<Core::DynarmicExclusiveMonitor&>(kernel.GetExclusiveMonitor()), + m_core_index); } #else #error Platform not supported yet. diff --git a/src/core/hle/kernel/svc/svc_cache.cpp b/src/core/hle/kernel/svc/svc_cache.cpp index 082942dab..c2c8be10f 100644 --- a/src/core/hle/kernel/svc/svc_cache.cpp +++ b/src/core/hle/kernel/svc/svc_cache.cpp @@ -42,7 +42,7 @@ Result FlushProcessDataCache(Core::System& system, Handle process_handle, u64 ad R_UNLESS(process.IsNotNull(), ResultInvalidHandle); // Verify the region is within range. - auto& page_table = process->PageTable(); + auto& page_table = process->GetPageTable(); R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Perform the operation. diff --git a/src/core/hle/kernel/svc/svc_code_memory.cpp b/src/core/hle/kernel/svc/svc_code_memory.cpp index 687baff82..bae4cb0cd 100644 --- a/src/core/hle/kernel/svc/svc_code_memory.cpp +++ b/src/core/hle/kernel/svc/svc_code_memory.cpp @@ -48,7 +48,7 @@ Result CreateCodeMemory(Core::System& system, Handle* out, u64 address, uint64_t SCOPE_EXIT({ code_mem->Close(); }); // Verify that the region is in range. - R_UNLESS(GetCurrentProcess(system.Kernel()).PageTable().Contains(address, size), + R_UNLESS(GetCurrentProcess(system.Kernel()).GetPageTable().Contains(address, size), ResultInvalidCurrentMemory); // Initialize the code memory. @@ -92,7 +92,7 @@ Result ControlCodeMemory(Core::System& system, Handle code_memory_handle, case CodeMemoryOperation::Map: { // Check that the region is in range. R_UNLESS(GetCurrentProcess(system.Kernel()) - .PageTable() + .GetPageTable() .CanContain(address, size, KMemoryState::CodeOut), ResultInvalidMemoryRegion); @@ -105,7 +105,7 @@ Result ControlCodeMemory(Core::System& system, Handle code_memory_handle, case CodeMemoryOperation::Unmap: { // Check that the region is in range. R_UNLESS(GetCurrentProcess(system.Kernel()) - .PageTable() + .GetPageTable() .CanContain(address, size, KMemoryState::CodeOut), ResultInvalidMemoryRegion); @@ -117,8 +117,8 @@ Result ControlCodeMemory(Core::System& system, Handle code_memory_handle, } break; case CodeMemoryOperation::MapToOwner: { // Check that the region is in range. - R_UNLESS(code_mem->GetOwner()->PageTable().CanContain(address, size, - KMemoryState::GeneratedCode), + R_UNLESS(code_mem->GetOwner()->GetPageTable().CanContain(address, size, + KMemoryState::GeneratedCode), ResultInvalidMemoryRegion); // Check the memory permission. @@ -129,8 +129,8 @@ Result ControlCodeMemory(Core::System& system, Handle code_memory_handle, } break; case CodeMemoryOperation::UnmapFromOwner: { // Check that the region is in range. - R_UNLESS(code_mem->GetOwner()->PageTable().CanContain(address, size, - KMemoryState::GeneratedCode), + R_UNLESS(code_mem->GetOwner()->GetPageTable().CanContain(address, size, + KMemoryState::GeneratedCode), ResultInvalidMemoryRegion); // Check the memory permission. diff --git a/src/core/hle/kernel/svc/svc_device_address_space.cpp b/src/core/hle/kernel/svc/svc_device_address_space.cpp index ec3143e67..42add9473 100644 --- a/src/core/hle/kernel/svc/svc_device_address_space.cpp +++ b/src/core/hle/kernel/svc/svc_device_address_space.cpp @@ -107,7 +107,7 @@ Result MapDeviceAddressSpaceByForce(Core::System& system, Handle das_handle, Han R_UNLESS(process.IsNotNull(), ResultInvalidHandle); // Validate that the process address is within range. - auto& page_table = process->PageTable(); + auto& page_table = process->GetPageTable(); R_UNLESS(page_table.Contains(process_address, size), ResultInvalidCurrentMemory); // Map. @@ -148,7 +148,7 @@ Result MapDeviceAddressSpaceAligned(Core::System& system, Handle das_handle, Han R_UNLESS(process.IsNotNull(), ResultInvalidHandle); // Validate that the process address is within range. - auto& page_table = process->PageTable(); + auto& page_table = process->GetPageTable(); R_UNLESS(page_table.Contains(process_address, size), ResultInvalidCurrentMemory); // Map. @@ -180,7 +180,7 @@ Result UnmapDeviceAddressSpace(Core::System& system, Handle das_handle, Handle p R_UNLESS(process.IsNotNull(), ResultInvalidHandle); // Validate that the process address is within range. - auto& page_table = process->PageTable(); + auto& page_table = process->GetPageTable(); R_UNLESS(page_table.Contains(process_address, size), ResultInvalidCurrentMemory); R_RETURN(das->Unmap(std::addressof(page_table), process_address, size, device_address)); diff --git a/src/core/hle/kernel/svc/svc_info.cpp b/src/core/hle/kernel/svc/svc_info.cpp index 445cdd87b..f99964028 100644 --- a/src/core/hle/kernel/svc/svc_info.cpp +++ b/src/core/hle/kernel/svc/svc_info.cpp @@ -54,35 +54,35 @@ Result GetInfo(Core::System& system, u64* result, InfoType info_id_type, Handle R_SUCCEED(); case InfoType::AliasRegionAddress: - *result = GetInteger(process->PageTable().GetAliasRegionStart()); + *result = GetInteger(process->GetPageTable().GetAliasRegionStart()); R_SUCCEED(); case InfoType::AliasRegionSize: - *result = process->PageTable().GetAliasRegionSize(); + *result = process->GetPageTable().GetAliasRegionSize(); R_SUCCEED(); case InfoType::HeapRegionAddress: - *result = GetInteger(process->PageTable().GetHeapRegionStart()); + *result = GetInteger(process->GetPageTable().GetHeapRegionStart()); R_SUCCEED(); case InfoType::HeapRegionSize: - *result = process->PageTable().GetHeapRegionSize(); + *result = process->GetPageTable().GetHeapRegionSize(); R_SUCCEED(); case InfoType::AslrRegionAddress: - *result = GetInteger(process->PageTable().GetAliasCodeRegionStart()); + *result = GetInteger(process->GetPageTable().GetAliasCodeRegionStart()); R_SUCCEED(); case InfoType::AslrRegionSize: - *result = process->PageTable().GetAliasCodeRegionSize(); + *result = process->GetPageTable().GetAliasCodeRegionSize(); R_SUCCEED(); case InfoType::StackRegionAddress: - *result = GetInteger(process->PageTable().GetStackRegionStart()); + *result = GetInteger(process->GetPageTable().GetStackRegionStart()); R_SUCCEED(); case InfoType::StackRegionSize: - *result = process->PageTable().GetStackRegionSize(); + *result = process->GetPageTable().GetStackRegionSize(); R_SUCCEED(); case InfoType::TotalMemorySize: diff --git a/src/core/hle/kernel/svc/svc_memory.cpp b/src/core/hle/kernel/svc/svc_memory.cpp index 5dcb7f045..2cab74127 100644 --- a/src/core/hle/kernel/svc/svc_memory.cpp +++ b/src/core/hle/kernel/svc/svc_memory.cpp @@ -63,36 +63,13 @@ Result MapUnmapMemorySanityChecks(const KPageTable& manager, u64 dst_addr, u64 s R_THROW(ResultInvalidCurrentMemory); } - if (!manager.IsInsideAddressSpace(src_addr, size)) { + if (!manager.Contains(src_addr, size)) { LOG_ERROR(Kernel_SVC, "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", src_addr, size); R_THROW(ResultInvalidCurrentMemory); } - if (manager.IsOutsideStackRegion(dst_addr, size)) { - LOG_ERROR(Kernel_SVC, - "Destination is not within the stack region, addr=0x{:016X}, size=0x{:016X}", - dst_addr, size); - R_THROW(ResultInvalidMemoryRegion); - } - - if (manager.IsInsideHeapRegion(dst_addr, size)) { - LOG_ERROR(Kernel_SVC, - "Destination does not fit within the heap region, addr=0x{:016X}, " - "size=0x{:016X}", - dst_addr, size); - R_THROW(ResultInvalidMemoryRegion); - } - - if (manager.IsInsideAliasRegion(dst_addr, size)) { - LOG_ERROR(Kernel_SVC, - "Destination does not fit within the map region, addr=0x{:016X}, " - "size=0x{:016X}", - dst_addr, size); - R_THROW(ResultInvalidMemoryRegion); - } - R_SUCCEED(); } @@ -112,7 +89,7 @@ Result SetMemoryPermission(Core::System& system, u64 address, u64 size, MemoryPe R_UNLESS(IsValidSetMemoryPermission(perm), ResultInvalidNewMemoryPermission); // Validate that the region is in range for the current process. - auto& page_table = GetCurrentProcess(system.Kernel()).PageTable(); + auto& page_table = GetCurrentProcess(system.Kernel()).GetPageTable(); R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Set the memory attribute. @@ -136,7 +113,7 @@ Result SetMemoryAttribute(Core::System& system, u64 address, u64 size, u32 mask, R_UNLESS((mask | attr | SupportedMask) == SupportedMask, ResultInvalidCombination); // Validate that the region is in range for the current process. - auto& page_table{GetCurrentProcess(system.Kernel()).PageTable()}; + auto& page_table{GetCurrentProcess(system.Kernel()).GetPageTable()}; R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Set the memory attribute. @@ -148,7 +125,7 @@ Result MapMemory(Core::System& system, u64 dst_addr, u64 src_addr, u64 size) { LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr, src_addr, size); - auto& page_table{GetCurrentProcess(system.Kernel()).PageTable()}; + auto& page_table{GetCurrentProcess(system.Kernel()).GetPageTable()}; if (const Result result{MapUnmapMemorySanityChecks(page_table, dst_addr, src_addr, size)}; result.IsError()) { @@ -163,7 +140,7 @@ Result UnmapMemory(Core::System& system, u64 dst_addr, u64 src_addr, u64 size) { LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr, src_addr, size); - auto& page_table{GetCurrentProcess(system.Kernel()).PageTable()}; + auto& page_table{GetCurrentProcess(system.Kernel()).GetPageTable()}; if (const Result result{MapUnmapMemorySanityChecks(page_table, dst_addr, src_addr, size)}; result.IsError()) { diff --git a/src/core/hle/kernel/svc/svc_physical_memory.cpp b/src/core/hle/kernel/svc/svc_physical_memory.cpp index c2fbfb59a..d3545f232 100644 --- a/src/core/hle/kernel/svc/svc_physical_memory.cpp +++ b/src/core/hle/kernel/svc/svc_physical_memory.cpp @@ -16,7 +16,7 @@ Result SetHeapSize(Core::System& system, u64* out_address, u64 size) { R_UNLESS(size < MainMemorySizeMax, ResultInvalidSize); // Set the heap size. - R_RETURN(GetCurrentProcess(system.Kernel()).PageTable().SetHeapSize(out_address, size)); + R_RETURN(GetCurrentProcess(system.Kernel()).GetPageTable().SetHeapSize(out_address, size)); } /// Maps memory at a desired address @@ -44,21 +44,21 @@ Result MapPhysicalMemory(Core::System& system, u64 addr, u64 size) { } KProcess* const current_process{GetCurrentProcessPointer(system.Kernel())}; - auto& page_table{current_process->PageTable()}; + auto& page_table{current_process->GetPageTable()}; if (current_process->GetSystemResourceSize() == 0) { LOG_ERROR(Kernel_SVC, "System Resource Size is zero"); R_THROW(ResultInvalidState); } - if (!page_table.IsInsideAddressSpace(addr, size)) { + if (!page_table.Contains(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, size); R_THROW(ResultInvalidMemoryRegion); } - if (page_table.IsOutsideAliasRegion(addr, size)) { + if (!page_table.IsInAliasRegion(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr, size); @@ -93,21 +93,21 @@ Result UnmapPhysicalMemory(Core::System& system, u64 addr, u64 size) { } KProcess* const current_process{GetCurrentProcessPointer(system.Kernel())}; - auto& page_table{current_process->PageTable()}; + auto& page_table{current_process->GetPageTable()}; if (current_process->GetSystemResourceSize() == 0) { LOG_ERROR(Kernel_SVC, "System Resource Size is zero"); R_THROW(ResultInvalidState); } - if (!page_table.IsInsideAddressSpace(addr, size)) { + if (!page_table.Contains(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, size); R_THROW(ResultInvalidMemoryRegion); } - if (page_table.IsOutsideAliasRegion(addr, size)) { + if (!page_table.IsInAliasRegion(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr, size); diff --git a/src/core/hle/kernel/svc/svc_process.cpp b/src/core/hle/kernel/svc/svc_process.cpp index 619ed16a3..caa8bee9a 100644 --- a/src/core/hle/kernel/svc/svc_process.cpp +++ b/src/core/hle/kernel/svc/svc_process.cpp @@ -66,8 +66,8 @@ Result GetProcessList(Core::System& system, s32* out_num_processes, u64 out_proc auto& kernel = system.Kernel(); const auto total_copy_size = out_process_ids_size * sizeof(u64); - if (out_process_ids_size > 0 && !GetCurrentProcess(kernel).PageTable().IsInsideAddressSpace( - out_process_ids, total_copy_size)) { + if (out_process_ids_size > 0 && + !GetCurrentProcess(kernel).GetPageTable().Contains(out_process_ids, total_copy_size)) { LOG_ERROR(Kernel_SVC, "Address range outside address space. begin=0x{:016X}, end=0x{:016X}", out_process_ids, out_process_ids + total_copy_size); R_THROW(ResultInvalidCurrentMemory); diff --git a/src/core/hle/kernel/svc/svc_process_memory.cpp b/src/core/hle/kernel/svc/svc_process_memory.cpp index aee0f2f36..07cd48175 100644 --- a/src/core/hle/kernel/svc/svc_process_memory.cpp +++ b/src/core/hle/kernel/svc/svc_process_memory.cpp @@ -49,7 +49,7 @@ Result SetProcessMemoryPermission(Core::System& system, Handle process_handle, u R_UNLESS(process.IsNotNull(), ResultInvalidHandle); // Validate that the address is in range. - auto& page_table = process->PageTable(); + auto& page_table = process->GetPageTable(); R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Set the memory permission. @@ -77,8 +77,8 @@ Result MapProcessMemory(Core::System& system, u64 dst_address, Handle process_ha R_UNLESS(src_process.IsNotNull(), ResultInvalidHandle); // Get the page tables. - auto& dst_pt = dst_process->PageTable(); - auto& src_pt = src_process->PageTable(); + auto& dst_pt = dst_process->GetPageTable(); + auto& src_pt = src_process->GetPageTable(); // Validate that the mapping is in range. R_UNLESS(src_pt.Contains(src_address, size), ResultInvalidCurrentMemory); @@ -118,8 +118,8 @@ Result UnmapProcessMemory(Core::System& system, u64 dst_address, Handle process_ R_UNLESS(src_process.IsNotNull(), ResultInvalidHandle); // Get the page tables. - auto& dst_pt = dst_process->PageTable(); - auto& src_pt = src_process->PageTable(); + auto& dst_pt = dst_process->GetPageTable(); + auto& src_pt = src_process->GetPageTable(); // Validate that the mapping is in range. R_UNLESS(src_pt.Contains(src_address, size), ResultInvalidCurrentMemory); @@ -178,8 +178,8 @@ Result MapProcessCodeMemory(Core::System& system, Handle process_handle, u64 dst R_THROW(ResultInvalidHandle); } - auto& page_table = process->PageTable(); - if (!page_table.IsInsideAddressSpace(src_address, size)) { + auto& page_table = process->GetPageTable(); + if (!page_table.Contains(src_address, size)) { LOG_ERROR(Kernel_SVC, "Source address range is not within the address space (src_address=0x{:016X}, " "size=0x{:016X}).", @@ -187,14 +187,6 @@ Result MapProcessCodeMemory(Core::System& system, Handle process_handle, u64 dst R_THROW(ResultInvalidCurrentMemory); } - if (!page_table.IsInsideASLRRegion(dst_address, size)) { - LOG_ERROR(Kernel_SVC, - "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " - "size=0x{:016X}).", - dst_address, size); - R_THROW(ResultInvalidMemoryRegion); - } - R_RETURN(page_table.MapCodeMemory(dst_address, src_address, size)); } @@ -246,8 +238,8 @@ Result UnmapProcessCodeMemory(Core::System& system, Handle process_handle, u64 d R_THROW(ResultInvalidHandle); } - auto& page_table = process->PageTable(); - if (!page_table.IsInsideAddressSpace(src_address, size)) { + auto& page_table = process->GetPageTable(); + if (!page_table.Contains(src_address, size)) { LOG_ERROR(Kernel_SVC, "Source address range is not within the address space (src_address=0x{:016X}, " "size=0x{:016X}).", @@ -255,14 +247,6 @@ Result UnmapProcessCodeMemory(Core::System& system, Handle process_handle, u64 d R_THROW(ResultInvalidCurrentMemory); } - if (!page_table.IsInsideASLRRegion(dst_address, size)) { - LOG_ERROR(Kernel_SVC, - "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " - "size=0x{:016X}).", - dst_address, size); - R_THROW(ResultInvalidMemoryRegion); - } - R_RETURN(page_table.UnmapCodeMemory(dst_address, src_address, size, KPageTable::ICacheInvalidationStrategy::InvalidateAll)); } diff --git a/src/core/hle/kernel/svc/svc_query_memory.cpp b/src/core/hle/kernel/svc/svc_query_memory.cpp index 4d9fcd25f..51af06e97 100644 --- a/src/core/hle/kernel/svc/svc_query_memory.cpp +++ b/src/core/hle/kernel/svc/svc_query_memory.cpp @@ -31,7 +31,7 @@ Result QueryProcessMemory(Core::System& system, uint64_t out_memory_info, PageIn } auto& current_memory{GetCurrentMemory(system.Kernel())}; - const auto memory_info{process->PageTable().QueryInfo(address).GetSvcMemoryInfo()}; + const auto memory_info{process->GetPageTable().QueryInfo(address).GetSvcMemoryInfo()}; current_memory.WriteBlock(out_memory_info, std::addressof(memory_info), sizeof(memory_info)); diff --git a/src/core/hle/kernel/svc/svc_shared_memory.cpp b/src/core/hle/kernel/svc/svc_shared_memory.cpp index a698596aa..012b1ae2b 100644 --- a/src/core/hle/kernel/svc/svc_shared_memory.cpp +++ b/src/core/hle/kernel/svc/svc_shared_memory.cpp @@ -43,7 +43,7 @@ Result MapSharedMemory(Core::System& system, Handle shmem_handle, u64 address, u // Get the current process. auto& process = GetCurrentProcess(system.Kernel()); - auto& page_table = process.PageTable(); + auto& page_table = process.GetPageTable(); // Get the shared memory. KScopedAutoObject shmem = process.GetHandleTable().GetObject<KSharedMemory>(shmem_handle); @@ -73,7 +73,7 @@ Result UnmapSharedMemory(Core::System& system, Handle shmem_handle, u64 address, // Get the current process. auto& process = GetCurrentProcess(system.Kernel()); - auto& page_table = process.PageTable(); + auto& page_table = process.GetPageTable(); // Get the shared memory. KScopedAutoObject shmem = process.GetHandleTable().GetObject<KSharedMemory>(shmem_handle); diff --git a/src/core/hle/kernel/svc/svc_thread.cpp b/src/core/hle/kernel/svc/svc_thread.cpp index 36b94e6bf..92bcea72b 100644 --- a/src/core/hle/kernel/svc/svc_thread.cpp +++ b/src/core/hle/kernel/svc/svc_thread.cpp @@ -236,7 +236,7 @@ Result GetThreadList(Core::System& system, s32* out_num_threads, u64 out_thread_ const auto total_copy_size = out_thread_ids_size * sizeof(u64); if (out_thread_ids_size > 0 && - !current_process->PageTable().IsInsideAddressSpace(out_thread_ids, total_copy_size)) { + !current_process->GetPageTable().Contains(out_thread_ids, total_copy_size)) { LOG_ERROR(Kernel_SVC, "Address range outside address space. begin=0x{:016X}, end=0x{:016X}", out_thread_ids, out_thread_ids + total_copy_size); R_THROW(ResultInvalidCurrentMemory); diff --git a/src/core/hle/kernel/svc/svc_transfer_memory.cpp b/src/core/hle/kernel/svc/svc_transfer_memory.cpp index 82d469a37..7d94e7f09 100644 --- a/src/core/hle/kernel/svc/svc_transfer_memory.cpp +++ b/src/core/hle/kernel/svc/svc_transfer_memory.cpp @@ -55,7 +55,7 @@ Result CreateTransferMemory(Core::System& system, Handle* out, u64 address, u64 SCOPE_EXIT({ trmem->Close(); }); // Ensure that the region is in range. - R_UNLESS(process.PageTable().Contains(address, size), ResultInvalidCurrentMemory); + R_UNLESS(process.GetPageTable().Contains(address, size), ResultInvalidCurrentMemory); // Initialize the transfer memory. R_TRY(trmem->Initialize(address, size, map_perm)); |