mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-12-23 04:41:12 +00:00
kern: implement SvcMapProcessMemory, SvcUnmapProcessMemory
This commit is contained in:
parent
3cf793f87e
commit
fb6e85b291
4 changed files with 112 additions and 19 deletions
|
@ -88,6 +88,10 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.MapPageGroup(addr, pg, state, perm);
|
||||
}
|
||||
|
||||
Result UnmapPageGroup(KProcessAddress address, const KPageGroup &pg, KMemoryState state) {
|
||||
return this->page_table.UnmapPageGroup(address, pg, state);
|
||||
}
|
||||
|
||||
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KMemoryState state, KMemoryPermission perm) {
|
||||
return this->page_table.MapPages(out_addr, num_pages, alignment, phys_addr, state, perm);
|
||||
}
|
||||
|
|
|
@ -126,20 +126,7 @@ namespace ams::kern {
|
|||
NOINLINE bool Remove(ams::svc::Handle handle);
|
||||
|
||||
template<typename T = KAutoObject>
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
||||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::is_base_of<T, KProcess>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::is_base_of<T, KThread>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObjectWithoutPseudoHandle(ams::svc::Handle handle) const {
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
|
@ -155,6 +142,24 @@ namespace ams::kern {
|
|||
}
|
||||
}
|
||||
|
||||
template<typename T = KAutoObject>
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
||||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::derived_from<KProcess, T>) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::derived_from<KThread, T>) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
return this->template GetObjectWithoutPseudoHandle<T>(handle);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectForIpcWithoutPseudoHandle(ams::svc::Handle handle) const {
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
|
|
|
@ -90,7 +90,7 @@ namespace ams::kern {
|
|||
R_SUCCEED_IF(test_tag != (handle | ams::svc::HandleWaitMask));
|
||||
|
||||
/* Get the lock owner thread. */
|
||||
owner_thread = GetCurrentProcess().GetHandleTable().GetObject<KThread>(handle);
|
||||
owner_thread = GetCurrentProcess().GetHandleTable().GetObjectWithoutPseudoHandle<KThread>(handle);
|
||||
R_UNLESS(owner_thread.IsNotNull(), svc::ResultInvalidHandle());
|
||||
|
||||
/* Update the lock. */
|
||||
|
|
|
@ -57,6 +57,90 @@ namespace ams::kern::svc {
|
|||
return page_table.SetProcessMemoryPermission(address, size, perm);
|
||||
}
|
||||
|
||||
Result MapProcessMemory(uintptr_t dst_address, ams::svc::Handle process_handle, uint64_t src_address, size_t size) {
|
||||
/* Validate the address/size. */
|
||||
R_UNLESS(util::IsAligned(dst_address, PageSize), svc::ResultInvalidAddress());
|
||||
R_UNLESS(util::IsAligned(src_address, PageSize), svc::ResultInvalidAddress());
|
||||
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
||||
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
||||
R_UNLESS((dst_address < dst_address + size), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS((src_address < src_address + size), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS(src_address == static_cast<uintptr_t>(src_address), svc::ResultInvalidCurrentMemory());
|
||||
|
||||
/* Get the processes. */
|
||||
KProcess *dst_process = GetCurrentProcessPointer();
|
||||
KScopedAutoObject src_process = dst_process->GetHandleTable().GetObjectWithoutPseudoHandle<KProcess>(process_handle);
|
||||
R_UNLESS(src_process.IsNotNull(), svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the page tables. */
|
||||
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), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS(dst_pt.CanContain(dst_address, size, KMemoryState_SharedCode), svc::ResultInvalidMemoryRegion());
|
||||
|
||||
/* Create a new page group. */
|
||||
KPageGroup pg(dst_pt.GetBlockInfoManager());
|
||||
|
||||
/* Make the page group. */
|
||||
R_TRY(src_pt.MakeAndOpenPageGroup(std::addressof(pg),
|
||||
src_address, size / PageSize,
|
||||
KMemoryState_FlagCanMapProcess, KMemoryState_FlagCanMapProcess,
|
||||
KMemoryPermission_None, KMemoryPermission_None,
|
||||
KMemoryAttribute_All, KMemoryAttribute_None));
|
||||
|
||||
/* Close the page group when we're done. */
|
||||
ON_SCOPE_EXIT { pg.Close(); };
|
||||
|
||||
/* Map the group. */
|
||||
R_TRY(dst_pt.MapPageGroup(dst_address, pg, KMemoryState_SharedCode, KMemoryPermission_UserReadWrite));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result UnmapProcessMemory(uintptr_t dst_address, ams::svc::Handle process_handle, uint64_t src_address, size_t size) {
|
||||
/* Validate the address/size. */
|
||||
R_UNLESS(util::IsAligned(dst_address, PageSize), svc::ResultInvalidAddress());
|
||||
R_UNLESS(util::IsAligned(src_address, PageSize), svc::ResultInvalidAddress());
|
||||
R_UNLESS(util::IsAligned(size, PageSize), svc::ResultInvalidSize());
|
||||
R_UNLESS(size > 0, svc::ResultInvalidSize());
|
||||
R_UNLESS((dst_address < dst_address + size), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS((src_address < src_address + size), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS(src_address == static_cast<uintptr_t>(src_address), svc::ResultInvalidCurrentMemory());
|
||||
|
||||
/* Get the processes. */
|
||||
KProcess *dst_process = GetCurrentProcessPointer();
|
||||
KScopedAutoObject src_process = dst_process->GetHandleTable().GetObjectWithoutPseudoHandle<KProcess>(process_handle);
|
||||
R_UNLESS(src_process.IsNotNull(), svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the page tables. */
|
||||
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), svc::ResultInvalidCurrentMemory());
|
||||
R_UNLESS(dst_pt.CanContain(dst_address, size, KMemoryState_SharedCode), svc::ResultInvalidMemoryRegion());
|
||||
|
||||
/* Create a new page group. */
|
||||
KPageGroup pg(dst_pt.GetBlockInfoManager());
|
||||
|
||||
/* Make the page group. */
|
||||
R_TRY(src_pt.MakeAndOpenPageGroup(std::addressof(pg),
|
||||
src_address, size / PageSize,
|
||||
KMemoryState_FlagCanMapProcess, KMemoryState_FlagCanMapProcess,
|
||||
KMemoryPermission_None, KMemoryPermission_None,
|
||||
KMemoryAttribute_All, KMemoryAttribute_None));
|
||||
|
||||
/* Close the page group when we're done. */
|
||||
ON_SCOPE_EXIT { pg.Close(); };
|
||||
|
||||
/* Unmap the group. */
|
||||
R_TRY(dst_pt.UnmapPageGroup(dst_address, pg, KMemoryState_SharedCode));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* ============================= 64 ABI ============================= */
|
||||
|
@ -66,11 +150,11 @@ namespace ams::kern::svc {
|
|||
}
|
||||
|
||||
Result MapProcessMemory64(ams::svc::Address dst_address, ams::svc::Handle process_handle, uint64_t src_address, ams::svc::Size size) {
|
||||
MESOSPHERE_PANIC("Stubbed SvcMapProcessMemory64 was called.");
|
||||
return MapProcessMemory(dst_address, process_handle, src_address, size);
|
||||
}
|
||||
|
||||
Result UnmapProcessMemory64(ams::svc::Address dst_address, ams::svc::Handle process_handle, uint64_t src_address, ams::svc::Size size) {
|
||||
MESOSPHERE_PANIC("Stubbed SvcUnmapProcessMemory64 was called.");
|
||||
return UnmapProcessMemory(dst_address, process_handle, src_address, size);
|
||||
}
|
||||
|
||||
Result MapProcessCodeMemory64(ams::svc::Handle process_handle, uint64_t dst_address, uint64_t src_address, uint64_t size) {
|
||||
|
@ -88,11 +172,11 @@ namespace ams::kern::svc {
|
|||
}
|
||||
|
||||
Result MapProcessMemory64From32(ams::svc::Address dst_address, ams::svc::Handle process_handle, uint64_t src_address, ams::svc::Size size) {
|
||||
MESOSPHERE_PANIC("Stubbed SvcMapProcessMemory64From32 was called.");
|
||||
return MapProcessMemory(dst_address, process_handle, src_address, size);
|
||||
}
|
||||
|
||||
Result UnmapProcessMemory64From32(ams::svc::Address dst_address, ams::svc::Handle process_handle, uint64_t src_address, ams::svc::Size size) {
|
||||
MESOSPHERE_PANIC("Stubbed SvcUnmapProcessMemory64From32 was called.");
|
||||
return UnmapProcessMemory(dst_address, process_handle, src_address, size);
|
||||
}
|
||||
|
||||
Result MapProcessCodeMemory64From32(ams::svc::Handle process_handle, uint64_t dst_address, uint64_t src_address, uint64_t size) {
|
||||
|
|
Loading…
Reference in a new issue