aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs
blob: 2b7591406ff5dafcf38105d89928916a803a681a (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Ryujinx.HLE.HOS.Kernel.Process;
using ChocolArm64.Memory;

namespace Ryujinx.HLE.HOS.Kernel.Common
{
    static class KernelTransfer
    {
        public static bool UserToKernelInt32(Horizon system, ulong address, out int value)
        {
            KProcess currentProcess = system.Scheduler.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsMapped((long)address) &&
                currentProcess.CpuMemory.IsMapped((long)address + 3))
            {
                value = currentProcess.CpuMemory.ReadInt32((long)address);

                return true;
            }

            value = 0;

            return false;
        }

        public static bool UserToKernelInt32Array(Horizon system, ulong address, int[] values)
        {
            KProcess currentProcess = system.Scheduler.GetCurrentProcess();

            for (int index = 0; index < values.Length; index++, address += 4)
            {
                if (currentProcess.CpuMemory.IsMapped((long)address) &&
                    currentProcess.CpuMemory.IsMapped((long)address + 3))
                {
                    values[index]= currentProcess.CpuMemory.ReadInt32((long)address);
                }
                else
                {
                    return false;
                }
            }

            return true;
        }

        public static bool UserToKernelString(Horizon system, ulong address, int size, out string value)
        {
            KProcess currentProcess = system.Scheduler.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsMapped((long)address) &&
                currentProcess.CpuMemory.IsMapped((long)address + size - 1))
            {
                value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, (long)address, size);

                return true;
            }

            value = null;

            return false;
        }

        public static bool KernelToUserInt32(Horizon system, ulong address, int value)
        {
            KProcess currentProcess = system.Scheduler.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsMapped((long)address) &&
                currentProcess.CpuMemory.IsMapped((long)address + 3))
            {
                currentProcess.CpuMemory.WriteInt32ToSharedAddr((long)address, value);

                return true;
            }

            return false;
        }

        public static bool KernelToUserInt64(Horizon system, ulong address, long value)
        {
            KProcess currentProcess = system.Scheduler.GetCurrentProcess();

            if (currentProcess.CpuMemory.IsMapped((long)address) &&
                currentProcess.CpuMemory.IsMapped((long)address + 7))
            {
                currentProcess.CpuMemory.WriteInt64((long)address, value);

                return true;
            }

            return false;
        }
    }
}