aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs
blob: cbc276c50b3682ac9942cb482d131ca7c3060d00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Process;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.HLE.HOS.Kernel.Common
{
    static class KernelTransfer
    {
        public static bool UserToKernel<T>(out T value, ulong address) where T : unmanaged
        {
            KProcess currentProcess = KernelStatic.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)Unsafe.SizeOf<T>()))
            {
                value = currentProcess.CpuMemory.Read<T>(address);

                return true;
            }

            value = default;

            return false;
        }

        public static bool UserToKernelArray<T>(ulong address, Span<T> values) where T : unmanaged
        {
            KProcess currentProcess = KernelStatic.GetCurrentProcess();

            Span<byte> data = MemoryMarshal.Cast<T, byte>(values);

            if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)data.Length))
            {
                currentProcess.CpuMemory.Read(address, data);

                return true;
            }

            return false;
        }

        public static bool UserToKernelString(out string value, ulong address, uint size)
        {
            KProcess currentProcess = KernelStatic.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsRangeMapped(address, size))
            {
                value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, address, size);

                return true;
            }

            value = null;

            return false;
        }

        public static bool KernelToUser<T>(ulong address, T value) where T: unmanaged
        {
            KProcess currentProcess = KernelStatic.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsRangeMapped(address, (ulong)Unsafe.SizeOf<T>()))
            {
                currentProcess.CpuMemory.Write(address, value);

                return true;
            }

            return false;
        }
    }
}