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