From 94a170c437be14e41593f0244d2a792d950a67a6 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 28 Jul 2020 18:10:43 -0700 Subject: [PATCH] kern: QueryMemory64From32, QueryProcessMemory64From32 --- .../source/svc/kern_svc_query_memory.cpp | 58 ++++++++++++------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/libraries/libmesosphere/source/svc/kern_svc_query_memory.cpp b/libraries/libmesosphere/source/svc/kern_svc_query_memory.cpp index f893322fe..3f75d3104 100644 --- a/libraries/libmesosphere/source/svc/kern_svc_query_memory.cpp +++ b/libraries/libmesosphere/source/svc/kern_svc_query_memory.cpp @@ -35,7 +35,39 @@ namespace ams::kern::svc { return ResultSuccess(); } - Result QueryMemory(ams::svc::MemoryInfo *out_memory_info, ams::svc::PageInfo *out_page_info, uintptr_t address) { + template + Result QueryProcessMemory(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, ams::svc::Handle process_handle, uint64_t address) { + /* Get an ams::svc::MemoryInfo for the region. */ + ams::svc::MemoryInfo info = {}; + R_TRY(QueryProcessMemory(std::addressof(info), out_page_info, process_handle, address)); + + /* Invoke the implementation. */ + if constexpr (std::same_as) { + R_TRY(out_memory_info.CopyFrom(std::addressof(info))); + } else { + /* Convert the info. */ + T converted_info = {}; + static_assert(std::same_as); + static_assert(std::same_as); + + converted_info.addr = info.addr; + converted_info.size = info.size; + converted_info.state = info.state; + converted_info.attr = info.attr; + converted_info.perm = info.perm; + converted_info.ipc_refcount = info.ipc_refcount; + converted_info.device_refcount = info.device_refcount; + + /* Invoke. */ + R_TRY(out_memory_info.CopyFrom(std::addressof(converted_info))); + } + + return ResultSuccess(); + } + + + template + Result QueryMemory(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, uintptr_t address) { /* Query memory is just QueryProcessMemory on the current process. */ return QueryProcessMemory(out_memory_info, out_page_info, ams::svc::PseudoHandle::CurrentProcess, address); } @@ -45,37 +77,21 @@ namespace ams::kern::svc { /* ============================= 64 ABI ============================= */ Result QueryMemory64(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, ams::svc::Address address) { - /* Get an ams::svc::MemoryInfo for the region. */ - ams::svc::MemoryInfo info = {}; - R_TRY(QueryMemory(std::addressof(info), out_page_info, address)); - - /* Try to copy to userspace. In the 64-bit case, ams::svc::lp64::MemoryInfo is the same as ams::svc::MemoryInfo. */ - static_assert(sizeof(ams::svc::MemoryInfo) == sizeof(ams::svc::lp64::MemoryInfo)); - R_TRY(out_memory_info.CopyFrom(std::addressof(info))); - - return ResultSuccess(); + return QueryMemory(out_memory_info, out_page_info, address); } Result QueryProcessMemory64(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, ams::svc::Handle process_handle, uint64_t address) { - /* Get an ams::svc::MemoryInfo for the region. */ - ams::svc::MemoryInfo info = {}; - R_TRY(QueryProcessMemory(std::addressof(info), out_page_info, process_handle, address)); - - /* Try to copy to userspace. In the 64-bit case, ams::svc::lp64::MemoryInfo is the same as ams::svc::MemoryInfo. */ - static_assert(sizeof(ams::svc::MemoryInfo) == sizeof(ams::svc::lp64::MemoryInfo)); - R_TRY(out_memory_info.CopyFrom(std::addressof(info))); - - return ResultSuccess(); + return QueryProcessMemory(out_memory_info, out_page_info, process_handle, address); } /* ============================= 64From32 ABI ============================= */ Result QueryMemory64From32(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, ams::svc::Address address) { - MESOSPHERE_PANIC("Stubbed SvcQueryMemory64From32 was called."); + return QueryMemory(out_memory_info, out_page_info, address); } Result QueryProcessMemory64From32(KUserPointer out_memory_info, ams::svc::PageInfo *out_page_info, ams::svc::Handle process_handle, uint64_t address) { - MESOSPHERE_PANIC("Stubbed SvcQueryProcessMemory64From32 was called."); + return QueryProcessMemory(out_memory_info, out_page_info, process_handle, address); } }