2020-05-03 23:54:50 +01:00
|
|
|
using Ryujinx.Cpu;
|
2018-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2020-07-17 05:22:13 +01:00
|
|
|
using System;
|
2022-01-29 21:18:03 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
|
|
|
{
|
|
|
|
static class KernelTransfer
|
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
public static bool UserToKernel<T>(out T value, ulong address) where T : unmanaged
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-12-18 05:33:36 +00:00
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)Unsafe.SizeOf<T>()))
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
value = currentProcess.CpuMemory.Read<T>(address);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
value = default;
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
public static bool UserToKernelArray<T>(ulong address, Span<T> values) where T : unmanaged
|
2019-01-18 22:26:39 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2019-01-18 22:26:39 +00:00
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
Span<byte> data = MemoryMarshal.Cast<T, byte>(values);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)data.Length))
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
currentProcess.CpuMemory.Read(address, data);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
public static bool UserToKernelString(out string value, ulong address, uint size)
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-12-18 05:33:36 +00:00
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
if (currentProcess.CpuMemory.IsRangeMapped(address, size))
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2022-01-29 21:18:03 +00:00
|
|
|
value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, address, size);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
value = null;
|
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
public static bool KernelToUser<T>(ulong address, T value) where T: unmanaged
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-12-18 05:33:36 +00:00
|
|
|
|
2022-01-29 21:18:03 +00:00
|
|
|
if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)Unsafe.SizeOf<T>()))
|
2018-12-18 05:33:36 +00:00
|
|
|
{
|
2020-05-03 23:54:50 +01:00
|
|
|
currentProcess.CpuMemory.Write(address, value);
|
2018-12-18 05:33:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|