Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins with heaps of 5 MiB or less to use PRIVATE memory instead of SHARED #2079

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions k11_extension/include/svc/MapProcessMemoryPrivate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#pragma once

#include "utils.h"
#include "kernel.h"
#include "svc.h"

Result MapProcessMemoryPrivate(Handle dstProcessHandle, u32 vaDst, Handle srcProcessHandle, u32 vaSrc, u32 size);
Result MapProcessMemoryPrivateWrapper(Handle dstProcessHandle, u32 vaDst, Handle srcProcessHandle, u32 vaSrc, u32 size);
2 changes: 2 additions & 0 deletions k11_extension/source/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "svc/CustomBackdoor.h"
#include "svc/MapProcessMemoryEx.h"
#include "svc/UnmapProcessMemoryEx.h"
#include "svc/MapProcessMemoryPrivate.h"
#include "svc/ControlService.h"
#include "svc/ControlProcess.h"
#include "svc/ExitProcess.h"
Expand Down Expand Up @@ -98,6 +99,7 @@ void buildAlteredSvcTable(void)
alteredSvcTable[0xA1] = UnmapProcessMemoryEx;
alteredSvcTable[0xA2] = ControlMemoryEx;
alteredSvcTable[0xA3] = ControlMemoryUnsafeWrapper;
alteredSvcTable[0xA4] = MapProcessMemoryPrivateWrapper;

alteredSvcTable[0xB0] = ControlService;
alteredSvcTable[0xB1] = CopyHandleWrapper;
Expand Down
86 changes: 86 additions & 0 deletions k11_extension/source/svc/MapProcessMemoryPrivate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#include "svc/MapProcessMemoryPrivate.h"

Result MapProcessMemoryPrivate(Handle dstProcessHandle, u32 vaDst, Handle srcProcessHandle, u32 vaSrc, u32 size)
{
Result res = 0;
u32 sizeInPage = size >> 12;
KLinkedList list;
KProcess *srcProcess;
KProcess *dstProcess;
KProcessHandleTable *handleTable = handleTableOfProcess(currentCoreContext->objectContext.currentProcess);

if (dstProcessHandle == CUR_PROCESS_HANDLE)
{
dstProcess = currentCoreContext->objectContext.currentProcess;
KAutoObject__AddReference((KAutoObject *)dstProcess);
}
else
dstProcess = KProcessHandleTable__ToKProcess(handleTable, dstProcessHandle);

if (dstProcess == NULL)
return 0xD8E007F7;

if (srcProcessHandle == CUR_PROCESS_HANDLE)
{
srcProcess = currentCoreContext->objectContext.currentProcess;
KAutoObject__AddReference((KAutoObject *)srcProcess);
}
else
srcProcess = KProcessHandleTable__ToKProcess(handleTable, srcProcessHandle);

if (srcProcess == NULL)
{
res = 0xD8E007F7;
goto exit1;
}

KLinkedList__Initialize(&list);

res = KProcessHwInfo__GetListOfKBlockInfoForVA(hwInfoOfProcess(srcProcess), &list, vaSrc, sizeInPage);

if (res >= 0)
{
// Check if the destination address is free and large enough
res = KProcessHwInfo__CheckVaState(hwInfoOfProcess(dstProcess), vaDst, size, 0, 0);
if (res == 0)
res = KProcessHwInfo__MapListOfKBlockInfo(hwInfoOfProcess(dstProcess), vaDst, &list, 0xBB05, MEMPERM_RW | 0x18, 0);
}

KLinkedList_KBlockInfo__Clear(&list);

((KAutoObject *)srcProcess)->vtable->DecrementReferenceCount((KAutoObject *)srcProcess);

exit1:
((KAutoObject *)dstProcess)->vtable->DecrementReferenceCount((KAutoObject *)dstProcess);

invalidateEntireInstructionCache();
flushEntireDataCache();

return res;
}
9 changes: 9 additions & 0 deletions k11_extension/source/svc/wrappers.s
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ MapProcessMemoryExWrapper:
str r4, [sp, #-4]!
bl MapProcessMemoryEx
add sp, #4
pop {pc}

.global MapProcessMemoryPrivateWrapper
.type MapProcessMemoryPrivateWrapper, %function
MapProcessMemoryPrivateWrapper:
push {lr}
str r4, [sp, #-4]!
bl MapProcessMemoryPrivate
add sp, #4
pop {pc}
10 changes: 10 additions & 0 deletions sysmodules/rosalina/include/csvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ void svcInvalidateEntireInstructionCache(void);
*/
Result svcMapProcessMemoryEx(Handle dstProcessHandle, u32 destAddress, Handle srcProcessHandle, u32 srcAddress, u32 size);

/**
* @brief Maps a block of process memory, but sets MemState to PRIVATE instead of SHARED.
* @param dstProcessHandle Handle of the process to map the memory in (destination)
* @param destAddress Start address of the memory block in the destination process
* @param srcProcessHandle Handle of the process to map the memory from (source)
* @param srcAddress Start address of the memory block in the source process
* @param size Size of the block of the memory to map (truncated to a multiple of 0x1000 bytes)
*/
Result svcMapProcessMemoryPrivate(Handle dstProcessHandle, u32 destAddress, Handle srcProcessHandle, u32 srcAddress, u32 size);

/**
* @brief Unmaps a block of process memory.
* @param process Handle of the process to unmap the memory from
Expand Down
8 changes: 8 additions & 0 deletions sysmodules/rosalina/source/csvc.s
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ SVC_BEGIN svcFreeMemory
bx lr
SVC_END

SVC_BEGIN svcMapProcessMemoryPrivate
str r4, [sp, #-4]!
ldr r4, [sp, #4]
svc 0xA4
ldr r4, [sp], #4
bx lr
SVC_END

SVC_BEGIN svcControlService
svc 0xB0
bx lr
Expand Down
16 changes: 14 additions & 2 deletions sysmodules/rosalina/source/plugin/memoryblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,16 @@ Result MemoryBlock__MountInProcess(void)
}

// Heap (to be used by the plugin)
if (R_FAILED((res = svcMapProcessMemoryEx(target, header->heapVA, CUR_PROCESS_HANDLE, (u32)memblock->memblock + header->exeSize, header->heapSize))))
if (header->heapSize > 0x500000)
{
res = svcMapProcessMemoryEx(target, header->heapVA, CUR_PROCESS_HANDLE, (u32)memblock->memblock + header->exeSize, header->heapSize);
}
else
{
res = svcMapProcessMemoryPrivate(target, header->heapVA, CUR_PROCESS_HANDLE, (u32)memblock->memblock + header->exeSize, header->heapSize);
}

if (R_FAILED(res))
{
error->message = "Couldn't map heap memory block";
error->code = res;
Expand All @@ -230,10 +239,13 @@ Result MemoryBlock__UnmountFromProcess(void)
{
Handle target = PluginLoaderCtx.target;
PluginHeader *header = &PluginLoaderCtx.header;
MemoryBlock *memblock = &PluginLoaderCtx.memblock;

Result res = 0;

res = svcUnmapProcessMemoryEx(target, 0x07000000, header->exeSize);
res = svcMapProcessMemoryEx(target, header->heapVA, CUR_PROCESS_HANDLE, (u32) memblock->memblock + header->exeSize, header->heapSize);

res |= svcUnmapProcessMemoryEx(target, 0x07000000, header->exeSize);
res |= svcUnmapProcessMemoryEx(target, header->heapVA, header->heapSize);

return res;
Expand Down