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