aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Horizon/Sdk/Sf/Hipc/Api.cs
blob: deac524c722d607ef0364c7bae693bdd9ce0e7df (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
using Ryujinx.Horizon.Common;
using System;

namespace Ryujinx.Horizon.Sdk.Sf.Hipc
{
    static class Api
    {
        public const int TlsMessageBufferSize = 0x100;

        public static Result Receive(out ReceiveResult recvResult, int sessionHandle, Span<byte> messageBuffer)
        {
            Result result = ReceiveImpl(sessionHandle, messageBuffer);

            if (result == KernelResult.PortRemoteClosed)
            {
                recvResult = ReceiveResult.Closed;

                return Result.Success;
            }
            else if (result == KernelResult.ReceiveListBroken)
            {
                recvResult = ReceiveResult.NeedsRetry;

                return Result.Success;
            }

            recvResult = ReceiveResult.Success;

            return result;
        }

        private static Result ReceiveImpl(int sessionHandle, Span<byte> messageBuffer)
        {
            Span<int> handles = stackalloc int[1];

            handles[0] = sessionHandle;

            var tlsSpan = HorizonStatic.AddressSpace.GetSpan(HorizonStatic.ThreadContext.TlsAddress, TlsMessageBufferSize);

            if (messageBuffer == tlsSpan)
            {
                return HorizonStatic.Syscall.ReplyAndReceive(out _, handles, 0, -1L);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public static Result Reply(int sessionHandle, ReadOnlySpan<byte> messageBuffer)
        {
            Result result = ReplyImpl(sessionHandle, messageBuffer);

            result.AbortUnless(KernelResult.TimedOut, KernelResult.PortRemoteClosed);

            return Result.Success;
        }

        private static Result ReplyImpl(int sessionHandle, ReadOnlySpan<byte> messageBuffer)
        {
            Span<int> handles = stackalloc int[1];

            handles[0] = sessionHandle;

            var tlsSpan = HorizonStatic.AddressSpace.GetSpan(HorizonStatic.ThreadContext.TlsAddress, TlsMessageBufferSize);

            if (messageBuffer == tlsSpan)
            {
                return HorizonStatic.Syscall.ReplyAndReceive(out _, handles, sessionHandle, 0);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public static Result CreateSession(out int serverHandle, out int clientHandle)
        {
            Result result = HorizonStatic.Syscall.CreateSession(out serverHandle, out clientHandle, false, null);

            if (result == KernelResult.OutOfResource)
            {
                return HipcResult.OutOfSessions;
            }

            return result;
        }
    }
}