From 081669c3349812eb308be03107541377ac2dbc11 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 8 Jan 2022 23:07:07 -0800 Subject: hle: kernel: k_page_table: Rename *ProcessCodeMemory -> *CodeMemory. --- src/core/hle/kernel/k_page_table.cpp | 4 ++-- src/core/hle/kernel/k_page_table.h | 4 ++-- src/core/hle/kernel/svc.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 6077985b5..9be9f5e2b 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -298,7 +298,7 @@ ResultCode KPageTable::MapProcessCode(VAddr addr, std::size_t num_pages, KMemory return ResultSuccess; } -ResultCode KPageTable::MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { +ResultCode KPageTable::MapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { std::lock_guard lock{page_table_lock}; const std::size_t num_pages{size / PageSize}; @@ -335,7 +335,7 @@ ResultCode KPageTable::MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std: return ResultSuccess; } -ResultCode KPageTable::UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { +ResultCode KPageTable::UnmapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size) { std::lock_guard lock{page_table_lock}; if (!size) { diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index 9235b822a..a596bf381 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -31,8 +31,8 @@ public: KMemoryManager::Pool pool); ResultCode MapProcessCode(VAddr addr, std::size_t pages_count, KMemoryState state, KMemoryPermission perm); - ResultCode MapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); - ResultCode UnmapProcessCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); + ResultCode MapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); + ResultCode UnmapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t size); ResultCode UnmapProcessMemory(VAddr dst_addr, std::size_t size, KPageTable& src_page_table, VAddr src_addr); ResultCode MapPhysicalMemory(VAddr addr, std::size_t size); diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 4362508a3..55ce0236f 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1626,7 +1626,7 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand return ResultInvalidMemoryRegion; } - return page_table.MapProcessCodeMemory(dst_address, src_address, size); + return page_table.MapCodeMemory(dst_address, src_address, size); } static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_handle, @@ -1694,7 +1694,7 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha return ResultInvalidMemoryRegion; } - return page_table.UnmapProcessCodeMemory(dst_address, src_address, size); + return page_table.UnmapCodeMemory(dst_address, src_address, size); } /// Exits the current process -- cgit v1.2.3 From 6123b6ea456d77ba69a261f668c2d7caa5c567f5 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 8 Jan 2022 23:20:16 -0800 Subject: hle: kernel: k_page_table: ReadAndWrite -> UserReadWrite. --- src/core/hle/kernel/k_page_table.cpp | 28 ++++++++++++++-------------- src/core/hle/kernel/k_process.cpp | 6 +++--- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 9be9f5e2b..23734b501 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -307,7 +307,7 @@ ResultCode KPageTable::MapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size_t KMemoryPermission perm{}; CASCADE_CODE(CheckMemoryState(&state, &perm, nullptr, nullptr, src_addr, size, KMemoryState::All, KMemoryState::Normal, KMemoryPermission::All, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::Mask, + KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); if (IsRegionMapped(dst_addr, size)) { @@ -361,7 +361,7 @@ ResultCode KPageTable::UnmapCodeMemory(VAddr dst_addr, VAddr src_addr, std::size block_manager->Update(dst_addr, num_pages, KMemoryState::Free); block_manager->Update(src_addr, num_pages, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite); + KMemoryPermission::UserReadWrite); system.InvalidateCpuInstructionCacheRange(dst_addr, size); @@ -416,7 +416,7 @@ void KPageTable::MapPhysicalMemory(KPageLinkedList& page_linked_list, VAddr star } const std::size_t num_pages{std::min(src_num_pages, dst_num_pages)}; - Operate(dst_addr, num_pages, KMemoryPermission::ReadAndWrite, OperationType::Map, + Operate(dst_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::Map, map_addr); dst_addr += num_pages * PageSize; @@ -470,7 +470,7 @@ ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { const std::size_t num_pages{size / PageSize}; block_manager->Update(addr, num_pages, KMemoryState::Free, KMemoryPermission::None, KMemoryAttribute::None, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::None); + KMemoryPermission::UserReadWrite, KMemoryAttribute::None); return ResultSuccess; } @@ -554,7 +554,7 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { KMemoryState src_state{}; CASCADE_CODE(CheckMemoryState( &src_state, nullptr, nullptr, nullptr, src_addr, size, KMemoryState::FlagCanAlias, - KMemoryState::FlagCanAlias, KMemoryPermission::All, KMemoryPermission::ReadAndWrite, + KMemoryState::FlagCanAlias, KMemoryPermission::All, KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); if (IsRegionMapped(dst_addr, size)) { @@ -568,13 +568,13 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { { auto block_guard = detail::ScopeExit([&] { - Operate(src_addr, num_pages, KMemoryPermission::ReadAndWrite, + Operate(src_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::ChangePermissions); }); CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::None, OperationType::ChangePermissions)); - CASCADE_CODE(MapPages(dst_addr, page_linked_list, KMemoryPermission::ReadAndWrite)); + CASCADE_CODE(MapPages(dst_addr, page_linked_list, KMemoryPermission::UserReadWrite)); block_guard.Cancel(); } @@ -582,7 +582,7 @@ ResultCode KPageTable::Map(VAddr dst_addr, VAddr src_addr, std::size_t size) { block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::None, KMemoryAttribute::Locked); block_manager->Update(dst_addr, num_pages, KMemoryState::Stack, - KMemoryPermission::ReadAndWrite); + KMemoryPermission::UserReadWrite); return ResultSuccess; } @@ -617,13 +617,13 @@ ResultCode KPageTable::Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size) { auto block_guard = detail::ScopeExit([&] { MapPages(dst_addr, dst_pages, dst_perm); }); CASCADE_CODE(Operate(dst_addr, num_pages, KMemoryPermission::None, OperationType::Unmap)); - CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::ReadAndWrite, + CASCADE_CODE(Operate(src_addr, num_pages, KMemoryPermission::UserReadWrite, OperationType::ChangePermissions)); block_guard.Cancel(); } - block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::ReadAndWrite); + block_manager->Update(src_addr, num_pages, src_state, KMemoryPermission::UserReadWrite); block_manager->Update(dst_addr, num_pages, KMemoryState::Free); return ResultSuccess; @@ -785,7 +785,7 @@ ResultCode KPageTable::ReserveTransferMemory(VAddr addr, std::size_t size, KMemo &state, nullptr, &attribute, nullptr, addr, size, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryState::FlagCanTransfer | KMemoryState::FlagReferenceCounted, KMemoryPermission::All, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, + KMemoryPermission::UserReadWrite, KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); block_manager->Update(addr, size / PageSize, state, perm, attribute | KMemoryAttribute::Locked); @@ -805,7 +805,7 @@ ResultCode KPageTable::ResetTransferMemory(VAddr addr, std::size_t size) { KMemoryPermission::None, KMemoryPermission::None, KMemoryAttribute::Mask, KMemoryAttribute::Locked, KMemoryAttribute::IpcAndDeviceMapped)); - block_manager->Update(addr, size / PageSize, state, KMemoryPermission::ReadAndWrite); + block_manager->Update(addr, size / PageSize, state, KMemoryPermission::UserReadWrite); return ResultSuccess; } @@ -906,7 +906,7 @@ ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { R_TRY(this->CheckMemoryState(std::addressof(num_allocator_blocks), heap_region_start + size, GetHeapSize() - size, KMemoryState::All, KMemoryState::Normal, - KMemoryPermission::All, KMemoryPermission::ReadAndWrite, + KMemoryPermission::All, KMemoryPermission::UserReadWrite, KMemoryAttribute::All, KMemoryAttribute::None)); // Unmap the end of the heap. @@ -981,7 +981,7 @@ ResultCode KPageTable::SetHeapSize(VAddr* out, std::size_t size) { // Apply the memory block update. block_manager->Update(current_heap_end, num_pages, KMemoryState::Normal, - KMemoryPermission::ReadAndWrite, KMemoryAttribute::None); + KMemoryPermission::UserReadWrite, KMemoryAttribute::None); // Update the current heap end. current_heap_end = heap_region_start + size; diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index bf98a51e2..856c200d3 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -509,7 +509,7 @@ VAddr KProcess::CreateTLSRegion() { const VAddr tls_page_addr{page_table ->AllocateAndMapMemory(1, PageSize, true, start, size / PageSize, KMemoryState::ThreadLocal, - KMemoryPermission::ReadAndWrite, + KMemoryPermission::UserReadWrite, tls_map_addr) .ValueOr(0)}; @@ -550,7 +550,7 @@ void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); - ReprotectSegment(code_set.DataSegment(), KMemoryPermission::ReadAndWrite); + ReprotectSegment(code_set.DataSegment(), KMemoryPermission::UserReadWrite); } bool KProcess::IsSignaled() const { @@ -587,7 +587,7 @@ ResultCode KProcess::AllocateMainThreadStack(std::size_t stack_size) { CASCADE_RESULT(main_thread_stack_top, page_table->AllocateAndMapMemory( main_thread_stack_size / PageSize, PageSize, false, start, size / PageSize, - KMemoryState::Stack, KMemoryPermission::ReadAndWrite)); + KMemoryState::Stack, KMemoryPermission::UserReadWrite)); main_thread_stack_top += main_thread_stack_size; -- cgit v1.2.3 From 49a0e4330ee37bdfa503918f841ab9599ccc1c24 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 9 Jan 2022 02:17:17 -0800 Subject: hle: kernel: k_page_table: Update SetProcessMemoryPermission. --- src/core/hle/kernel/k_page_table.cpp | 73 ++++++++++++++++++++--------------- src/core/hle/kernel/k_page_table.h | 3 +- src/core/hle/kernel/k_process.cpp | 8 ++-- src/core/hle/kernel/physical_core.cpp | 18 ++++++--- src/core/hle/kernel/svc.cpp | 4 +- 5 files changed, 64 insertions(+), 42 deletions(-) (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 23734b501..27d86c9a4 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -713,50 +713,61 @@ ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, } ResultCode KPageTable::SetProcessMemoryPermission(VAddr addr, std::size_t size, - KMemoryPermission perm) { + Svc::MemoryPermission svc_perm) { + const size_t num_pages = size / PageSize; + // Lock the table. std::lock_guard lock{page_table_lock}; - KMemoryState prev_state{}; - KMemoryPermission prev_perm{}; - - CASCADE_CODE(CheckMemoryState( - &prev_state, &prev_perm, nullptr, nullptr, addr, size, KMemoryState::FlagCode, - KMemoryState::FlagCode, KMemoryPermission::None, KMemoryPermission::None, - KMemoryAttribute::Mask, KMemoryAttribute::None, KMemoryAttribute::IpcAndDeviceMapped)); - - KMemoryState state{prev_state}; + // Verify we can change the memory permission. + KMemoryState old_state; + KMemoryPermission old_perm; + size_t num_allocator_blocks; + R_TRY(this->CheckMemoryState(std::addressof(old_state), std::addressof(old_perm), nullptr, + std::addressof(num_allocator_blocks), addr, size, + KMemoryState::FlagCode, KMemoryState::FlagCode, + KMemoryPermission::None, KMemoryPermission::None, + KMemoryAttribute::All, KMemoryAttribute::None)); - // Ensure state is mutable if permission allows write - if ((perm & KMemoryPermission::Write) != KMemoryPermission::None) { - if (prev_state == KMemoryState::Code) { - state = KMemoryState::CodeData; - } else if (prev_state == KMemoryState::AliasCode) { - state = KMemoryState::AliasCodeData; - } else { + // Determine new perm/state. + const KMemoryPermission new_perm = ConvertToKMemoryPermission(svc_perm); + KMemoryState new_state = old_state; + const bool is_w = (new_perm & KMemoryPermission::UserWrite) == KMemoryPermission::UserWrite; + const bool is_x = (new_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute; + const bool was_x = + (old_perm & KMemoryPermission::UserExecute) == KMemoryPermission::UserExecute; + ASSERT(!(is_w && is_x)); + + if (is_w) { + switch (old_state) { + case KMemoryState::Code: + new_state = KMemoryState::CodeData; + break; + case KMemoryState::AliasCode: + new_state = KMemoryState::AliasCodeData; + break; + default: UNREACHABLE(); } } - // Return early if there is nothing to change - if (state == prev_state && perm == prev_perm) { - return ResultSuccess; - } + // Succeed if there's nothing to do. + R_SUCCEED_IF(old_perm == new_perm && old_state == new_state); - if ((prev_perm & KMemoryPermission::Execute) != (perm & KMemoryPermission::Execute)) { + // Perform mapping operation. + const auto operation = + was_x ? OperationType::ChangePermissionsAndRefresh : OperationType::ChangePermissions; + R_TRY(Operate(addr, num_pages, new_perm, operation)); + + // Update the blocks. + block_manager->Update(addr, num_pages, new_state, new_perm, KMemoryAttribute::None); + + // Ensure cache coherency, if we're setting pages as executable. + if (is_x) { // Memory execution state is changing, invalidate CPU cache range system.InvalidateCpuInstructionCacheRange(addr, size); } - const std::size_t num_pages{size / PageSize}; - const OperationType operation{(perm & KMemoryPermission::Execute) != KMemoryPermission::None - ? OperationType::ChangePermissionsAndRefresh - : OperationType::ChangePermissions}; - - CASCADE_CODE(Operate(addr, num_pages, perm, operation)); - - block_manager->Update(addr, num_pages, state, perm); - return ResultSuccess; } diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index a596bf381..274644181 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -43,7 +43,8 @@ public: ResultCode MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state, KMemoryPermission perm); ResultCode UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state); - ResultCode SetProcessMemoryPermission(VAddr addr, std::size_t size, KMemoryPermission perm); + ResultCode SetProcessMemoryPermission(VAddr addr, std::size_t size, + Svc::MemoryPermission svc_perm); KMemoryInfo QueryInfo(VAddr addr); ResultCode ReserveTransferMemory(VAddr addr, std::size_t size, KMemoryPermission perm); ResultCode ResetTransferMemory(VAddr addr, std::size_t size); diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index 856c200d3..cca405fed 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -541,16 +541,16 @@ void KProcess::FreeTLSRegion(VAddr tls_address) { void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { const auto ReprotectSegment = [&](const CodeSet::Segment& segment, - KMemoryPermission permission) { + Svc::MemoryPermission permission) { page_table->SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission); }; kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), code_set.memory.size()); - ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); - ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); - ReprotectSegment(code_set.DataSegment(), KMemoryPermission::UserReadWrite); + ReprotectSegment(code_set.CodeSegment(), Svc::MemoryPermission::ReadExecute); + ReprotectSegment(code_set.RODataSegment(), Svc::MemoryPermission::Read); + ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite); } bool KProcess::IsSignaled() const { diff --git a/src/core/hle/kernel/physical_core.cpp b/src/core/hle/kernel/physical_core.cpp index 7f02d9471..7477668e4 100644 --- a/src/core/hle/kernel/physical_core.cpp +++ b/src/core/hle/kernel/physical_core.cpp @@ -16,17 +16,25 @@ namespace Kernel { PhysicalCore::PhysicalCore(std::size_t core_index_, Core::System& system_, KScheduler& scheduler_, Core::CPUInterrupts& interrupts_) : core_index{core_index_}, system{system_}, scheduler{scheduler_}, - interrupts{interrupts_}, guard{std::make_unique()} {} + interrupts{interrupts_}, guard{std::make_unique()} { +#ifdef ARCHITECTURE_x86_64 + // TODO(bunnei): Initialization relies on a core being available. We may later replace this with + // a 32-bit instance of Dynarmic. This should be abstracted out to a CPU manager. + auto& kernel = system.Kernel(); + arm_interface = std::make_unique( + system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); +#else +#error Platform not supported yet. +#endif +} PhysicalCore::~PhysicalCore() = default; void PhysicalCore::Initialize([[maybe_unused]] bool is_64_bit) { #ifdef ARCHITECTURE_x86_64 auto& kernel = system.Kernel(); - if (is_64_bit) { - arm_interface = std::make_unique( - system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); - } else { + if (!is_64_bit) { + // We already initialized a 64-bit core, replace with a 32-bit one. arm_interface = std::make_unique( system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index); } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 55ce0236f..c7f5140f4 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1309,6 +1309,8 @@ static ResultCode SetProcessMemoryPermission(Core::System& system, Handle proces R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); R_UNLESS(size > 0, ResultInvalidSize); R_UNLESS((address < address + size), ResultInvalidCurrentMemory); + R_UNLESS(address == static_cast(address), ResultInvalidCurrentMemory); + R_UNLESS(size == static_cast(size), ResultInvalidCurrentMemory); // Validate the memory permission. R_UNLESS(IsValidProcessMemoryPermission(perm), ResultInvalidNewMemoryPermission); @@ -1323,7 +1325,7 @@ static ResultCode SetProcessMemoryPermission(Core::System& system, Handle proces R_UNLESS(page_table.Contains(address, size), ResultInvalidCurrentMemory); // Set the memory permission. - return page_table.SetProcessMemoryPermission(address, size, ConvertToKMemoryPermission(perm)); + return page_table.SetProcessMemoryPermission(address, size, perm); } static ResultCode MapProcessMemory(Core::System& system, VAddr dst_address, Handle process_handle, -- cgit v1.2.3