2020-06-16 11:28:02 -07:00
|
|
|
using ARMeilleure.Translation.PTC;
|
2021-08-12 14:56:24 -07:00
|
|
|
using LibHac.Loader;
|
|
|
|
using LibHac.Ncm;
|
|
|
|
using LibHac.Util;
|
2018-11-28 14:18:09 -08:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-03 20:41:29 -07:00
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-12-17 21:33:36 -08:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2018-11-28 14:18:09 -08:00
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
2021-03-27 07:12:05 -07:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2021-08-12 14:56:24 -07:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using Npdm = LibHac.Loader.Npdm;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS
|
|
|
|
{
|
2021-08-12 14:56:24 -07:00
|
|
|
struct ProgramInfo
|
|
|
|
{
|
|
|
|
public string Name;
|
|
|
|
public ulong ProgramId;
|
2022-05-03 14:28:32 -07:00
|
|
|
public bool AllowCodeMemoryForJit;
|
2021-08-12 14:56:24 -07:00
|
|
|
|
2022-05-03 14:28:32 -07:00
|
|
|
public ProgramInfo(in Npdm npdm, bool allowCodeMemoryForJit)
|
2021-08-12 14:56:24 -07:00
|
|
|
{
|
|
|
|
Name = StringUtils.Utf8ZToString(npdm.Meta.Value.ProgramName);
|
|
|
|
ProgramId = npdm.Aci.Value.ProgramId.Value;
|
2022-05-03 14:28:32 -07:00
|
|
|
AllowCodeMemoryForJit = allowCodeMemoryForJit;
|
2021-08-12 14:56:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:28:02 -07:00
|
|
|
static class ProgramLoader
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
private const bool AslrEnabled = true;
|
|
|
|
|
|
|
|
private const int ArgsHeaderSize = 8;
|
|
|
|
private const int ArgsDataSize = 0x9000;
|
|
|
|
private const int ArgsTotalSize = ArgsHeaderSize + ArgsDataSize;
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
public static bool LoadKip(KernelContext context, KipExecutable kip)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-12-12 23:30:27 -08:00
|
|
|
uint endOffset = kip.DataOffset + (uint)kip.Data.Length;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (kip.BssSize != 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
endOffset = kip.BssOffset + kip.BssSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
uint codeSize = BitUtils.AlignUp(kip.TextOffset + endOffset, KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
int codePagesCount = (int)(codeSize / KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
ulong codeBaseAddress = kip.Is64BitAddressSpace ? 0x8000000UL : 0x200000UL;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
ProcessCreationFlags flags = 0;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
if (AslrEnabled)
|
|
|
|
{
|
2019-07-01 19:39:22 -07:00
|
|
|
// TODO: Randomization.
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
flags |= ProcessCreationFlags.EnableAslr;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
if (kip.Is64BitAddressSpace)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-12-01 15:23:43 -08:00
|
|
|
flags |= ProcessCreationFlags.AddressSpace64Bit;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
if (kip.Is64Bit)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-12-01 15:23:43 -08:00
|
|
|
flags |= ProcessCreationFlags.Is64Bit;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
|
2020-07-03 16:58:01 -07:00
|
|
|
kip.Name,
|
|
|
|
kip.Version,
|
|
|
|
kip.ProgramId,
|
2018-12-06 03:16:24 -08:00
|
|
|
codeAddress,
|
|
|
|
codePagesCount,
|
2020-12-01 15:23:43 -08:00
|
|
|
flags,
|
2018-11-28 14:18:09 -08:00
|
|
|
0,
|
|
|
|
0);
|
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
MemoryRegion memoryRegion = kip.UsesSecureMemory
|
2018-11-28 14:18:09 -08:00
|
|
|
? MemoryRegion.Service
|
|
|
|
: MemoryRegion.Application;
|
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
KMemoryRegionManager region = context.MemoryManager.MemoryRegions[(int)memoryRegion];
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KernelResult result = region.AllocatePages((ulong)codePagesCount, false, out KPageList pageList);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KProcess process = new KProcess(context);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2022-05-31 12:29:35 -07:00
|
|
|
var processContextFactory = new ArmProcessContextFactory(context.Device.System.CpuEngine, context.Device.Gpu);
|
2020-12-01 15:23:43 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
result = process.InitializeKip(
|
|
|
|
creationInfo,
|
|
|
|
kip.Capabilities,
|
|
|
|
pageList,
|
2020-05-03 20:41:29 -07:00
|
|
|
context.ResourceLimit,
|
2020-12-01 15:23:43 -08:00
|
|
|
memoryRegion,
|
|
|
|
processContextFactory);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
result = LoadIntoMemory(process, kip, codeBaseAddress);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
process.DefaultCpuCore = kip.IdealCoreId;
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2020-07-03 16:58:01 -07:00
|
|
|
result = process.Start(kip.Priority, (ulong)kip.StackSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
context.Processes.TryAdd(process.Pid, process);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:28:32 -07:00
|
|
|
public static bool LoadNsos(
|
|
|
|
KernelContext context,
|
|
|
|
out ProcessTamperInfo tamperInfo,
|
|
|
|
MetaLoader metaData,
|
|
|
|
ProgramInfo programInfo,
|
|
|
|
byte[] arguments = null,
|
|
|
|
params IExecutable[] executables)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2021-08-12 14:56:24 -07:00
|
|
|
LibHac.Result rc = metaData.GetNpdm(out var npdm);
|
|
|
|
|
|
|
|
if (rc.IsFailure())
|
|
|
|
{
|
|
|
|
tamperInfo = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref readonly var meta = ref npdm.Meta.Value;
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong argsStart = 0;
|
2020-12-12 23:30:27 -08:00
|
|
|
uint argsSize = 0;
|
2021-08-12 14:56:24 -07:00
|
|
|
ulong codeStart = (meta.Flags & 1) != 0 ? 0x8000000UL : 0x200000UL;
|
2020-12-12 23:30:27 -08:00
|
|
|
uint codeSize = 0;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
var buildIds = executables.Select(e => (e switch
|
|
|
|
{
|
2022-02-26 15:52:25 -08:00
|
|
|
NsoExecutable nso => BitConverter.ToString(nso.BuildId.ItemsRo.ToArray()),
|
2021-03-27 07:12:05 -07:00
|
|
|
NroExecutable nro => BitConverter.ToString(nro.Header.BuildId),
|
|
|
|
_ => ""
|
|
|
|
}).Replace("-", "").ToUpper());
|
|
|
|
|
2020-05-14 23:16:46 -07:00
|
|
|
ulong[] nsoBase = new ulong[executables.Length];
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-14 23:16:46 -07:00
|
|
|
for (int index = 0; index < executables.Length; index++)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-12-12 23:30:27 -08:00
|
|
|
IExecutable nso = executables[index];
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
uint textEnd = nso.TextOffset + (uint)nso.Text.Length;
|
|
|
|
uint roEnd = nso.RoOffset + (uint)nso.Ro.Length;
|
|
|
|
uint dataEnd = nso.DataOffset + (uint)nso.Data.Length + nso.BssSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
uint nsoSize = textEnd;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
if (nsoSize < roEnd)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
nsoSize = roEnd;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
if (nsoSize < dataEnd)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
nsoSize = dataEnd;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
nsoSize = BitUtils.AlignUp(nsoSize, KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
nsoBase[index] = codeStart + (ulong)codeSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
codeSize += nsoSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (arguments != null && argsSize == 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
argsStart = (ulong)codeSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
argsSize = (uint)BitUtils.AlignDown(arguments.Length * 2 + ArgsTotalSize - 1, KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
codeSize += argsSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:28:02 -07:00
|
|
|
PtcProfiler.StaticCodeStart = codeStart;
|
2020-12-17 11:32:09 -08:00
|
|
|
PtcProfiler.StaticCodeSize = (ulong)codeSize;
|
2020-06-16 11:28:02 -07:00
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
int codePagesCount = (int)(codeSize / KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-08-12 14:56:24 -07:00
|
|
|
int personalMmHeapPagesCount = (int)(meta.SystemResourceSize / KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
|
2021-08-12 14:56:24 -07:00
|
|
|
programInfo.Name,
|
|
|
|
(int)meta.Version,
|
|
|
|
programInfo.ProgramId,
|
2018-12-06 03:16:24 -08:00
|
|
|
codeStart,
|
|
|
|
codePagesCount,
|
2021-08-12 14:56:24 -07:00
|
|
|
(ProcessCreationFlags)meta.Flags | ProcessCreationFlags.IsApplication,
|
2018-11-28 14:18:09 -08:00
|
|
|
0,
|
2018-12-06 03:16:24 -08:00
|
|
|
personalMmHeapPagesCount);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-08-12 14:56:24 -07:00
|
|
|
context.Device.System.LibHacHorizonManager.InitializeApplicationClient(new ProgramId(programInfo.ProgramId), in npdm);
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
KernelResult result;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KResourceLimit resourceLimit = new KResourceLimit(context);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
long applicationRgSize = (long)context.MemoryManager.MemoryRegions[(int)MemoryRegion.Application].Size;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Thread, 608);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Event, 700);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128);
|
|
|
|
result |= resourceLimit.SetLimitValue(LimitableResource.Session, 894);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization failed setting resource limit values.");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
tamperInfo = null;
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:28:32 -07:00
|
|
|
KProcess process = new KProcess(context, programInfo.AllowCodeMemoryForJit);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-08-12 14:56:24 -07:00
|
|
|
MemoryRegion memoryRegion = (MemoryRegion)((npdm.Acid.Value.Flags >> 2) & 0xf);
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
if (memoryRegion > MemoryRegion.NvServices)
|
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization failed due to invalid ACID flags.");
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
tamperInfo = null;
|
|
|
|
|
2019-01-18 14:26:39 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:29:35 -07:00
|
|
|
var processContextFactory = new ArmProcessContextFactory(context.Device.System.CpuEngine, context.Device.Gpu);
|
2020-12-01 15:23:43 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
result = process.Initialize(
|
|
|
|
creationInfo,
|
2021-08-12 14:56:24 -07:00
|
|
|
MemoryMarshal.Cast<byte, int>(npdm.KernelCapabilityData).ToArray(),
|
2018-12-06 03:16:24 -08:00
|
|
|
resourceLimit,
|
2020-12-01 15:23:43 -08:00
|
|
|
memoryRegion,
|
|
|
|
processContextFactory);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
tamperInfo = null;
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:16:46 -07:00
|
|
|
for (int index = 0; index < executables.Length; index++)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Info?.Print(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}...");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-14 23:16:46 -07:00
|
|
|
result = LoadIntoMemory(process, executables[index], nsoBase[index]);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
tamperInfo = null;
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:56:24 -07:00
|
|
|
process.DefaultCpuCore = meta.DefaultCpuId;
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2021-08-12 14:56:24 -07:00
|
|
|
result = process.Start(meta.MainThreadPriority, meta.MainThreadStackSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-08-03 16:32:53 -07:00
|
|
|
Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\".");
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
tamperInfo = null;
|
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
context.Processes.TryAdd(process.Pid, process);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2021-03-27 07:12:05 -07:00
|
|
|
// Keep the build ids because the tamper machine uses them to know which process to associate a
|
|
|
|
// tamper to and also keep the starting address of each executable inside a process because some
|
|
|
|
// memory modifications are relative to this address.
|
2021-08-04 13:05:17 -07:00
|
|
|
tamperInfo = new ProcessTamperInfo(process, buildIds, nsoBase, process.MemoryManager.HeapRegionStart,
|
|
|
|
process.MemoryManager.AliasRegionStart, process.MemoryManager.CodeRegionStart);
|
2021-03-27 07:12:05 -07:00
|
|
|
|
2018-11-28 14:18:09 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private static KernelResult LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong textStart = baseAddress + (ulong)image.TextOffset;
|
|
|
|
ulong roStart = baseAddress + (ulong)image.RoOffset;
|
|
|
|
ulong dataStart = baseAddress + (ulong)image.DataOffset;
|
|
|
|
ulong bssStart = baseAddress + (ulong)image.BssOffset;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong end = dataStart + (ulong)image.Data.Length;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (image.BssSize != 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
end = bssStart + (ulong)image.BssSize;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:54:50 -07:00
|
|
|
process.CpuMemory.Write(textStart, image.Text);
|
|
|
|
process.CpuMemory.Write(roStart, image.Ro);
|
|
|
|
process.CpuMemory.Write(dataStart, image.Data);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
process.CpuMemory.Fill(bssStart, image.BssSize, 0);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
KernelResult SetProcessMemoryPermission(ulong address, ulong size, KMemoryPermission permission)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
if (size == 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
POWER - Performance Optimizations With Extensive Ramifications (#2286)
* Refactoring of KMemoryManager class
* Replace some trivial uses of DRAM address with VA
* Get rid of GetDramAddressFromVa
* Abstracting more operations on derived page table class
* Run auto-format on KPageTableBase
* Managed to make TryConvertVaToPa private, few uses remains now
* Implement guest physical pages ref counting, remove manual freeing
* Make DoMmuOperation private and call new abstract methods only from the base class
* Pass pages count rather than size on Map/UnmapMemory
* Change memory managers to take host pointers
* Fix a guest memory leak and simplify KPageTable
* Expose new methods for host range query and mapping
* Some refactoring of MapPagesFromClientProcess to allow proper page ref counting and mapping without KPageLists
* Remove more uses of AddVaRangeToPageList, now only one remains (shared memory page checking)
* Add a SharedMemoryStorage class, will be useful for host mapping
* Sayonara AddVaRangeToPageList, you served us well
* Start to implement host memory mapping (WIP)
* Support memory tracking through host exception handling
* Fix some access violations from HLE service guest memory access and CPU
* Fix memory tracking
* Fix mapping list bugs, including a race and a error adding mapping ranges
* Simple page table for memory tracking
* Simple "volatile" region handle mode
* Update UBOs directly (experimental, rough)
* Fix the overlap check
* Only set non-modified buffers as volatile
* Fix some memory tracking issues
* Fix possible race in MapBufferFromClientProcess (block list updates were not locked)
* Write uniform update to memory immediately, only defer the buffer set.
* Fix some memory tracking issues
* Pass correct pages count on shared memory unmap
* Armeilleure Signal Handler v1 + Unix changes
Unix currently behaves like windows, rather than remapping physical
* Actually check if the host platform is unix
* Fix decommit on linux.
* Implement windows 10 placeholder shared memory, fix a buffer issue.
* Make PTC version something that will never match with master
* Remove testing variable for block count
* Add reference count for memory manager, fix dispose
Can still deadlock with OpenAL
* Add address validation, use page table for mapped check, add docs
Might clean up the page table traversing routines.
* Implement batched mapping/tracking.
* Move documentation, fix tests.
* Cleanup uniform buffer update stuff.
* Remove unnecessary assignment.
* Add unsafe host mapped memory switch
On by default. Would be good to turn this off for untrusted code (homebrew, exefs mods) and give the user the option to turn it on manually, though that requires some UI work.
* Remove C# exception handlers
They have issues due to current .NET limitations, so the meilleure one fully replaces them for now.
* Fix MapPhysicalMemory on the software MemoryManager.
* Null check for GetHostAddress, docs
* Add configuration for setting memory manager mode (not in UI yet)
* Add config to UI
* Fix type mismatch on Unix signal handler code emit
* Fix 6GB DRAM mode.
The size can be greater than `uint.MaxValue` when the DRAM is >4GB.
* Address some feedback.
* More detailed error if backing memory cannot be mapped.
* SetLastError on all OS functions for consistency
* Force pages dirty with UBO update instead of setting them directly.
Seems to be much faster across a few games. Need retesting.
* Rebase, configuration rework, fix mem tracking regression
* Fix race in FreePages
* Set memory managers null after decrementing ref count
* Remove readonly keyword, as this is now modified.
* Use a local variable for the signal handler rather than a register.
* Fix bug with buffer resize, and index/uniform buffer binding.
Should fix flickering in games.
* Add InvalidAccessHandler to MemoryTracking
Doesn't do anything yet
* Call invalid access handler on unmapped read/write.
Same rules as the regular memory manager.
* Make unsafe mapped memory its own MemoryManagerType
* Move FlushUboDirty into UpdateState.
* Buffer dirty cache, rather than ubo cache
Much cleaner, may be reusable for Inline2Memory updates.
* This doesn't return anything anymore.
* Add sigaction remove methods, correct a few function signatures.
* Return empty list of physical regions for size 0.
* Also on AddressSpaceManager
Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2021-05-24 13:52:44 -07:00
|
|
|
size = BitUtils.AlignUp(size, KPageTableBase.PageSize);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
return process.MemoryManager.SetProcessMemoryPermission(address, size, permission);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
KernelResult result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, KMemoryPermission.ReadAndExecute);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return result;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, KMemoryPermission.Read);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return result;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
return SetProcessMemoryPermission(dataStart, end - dataStart, KMemoryPermission.ReadAndWrite);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 15:41:02 -07:00
|
|
|
}
|