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