kern: implement SvcMapProcessMemory, SvcUnmapProcessMemory

This commit is contained in:
Michael Scire
2020-07-21 05:22:58 -07:00
committed by SciresM
parent 3cf793f87e
commit fb6e85b291
4 changed files with 112 additions and 19 deletions

View File

@@ -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) {