aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Sf/Hipc/ServerManager.cs
blob: e8957b758edabd97aaafe6f8e88f9dc6290b5a0a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using Ryujinx.Horizon.Sdk.OsTypes;
using Ryujinx.Horizon.Sdk.Sf.Cmif;
using Ryujinx.Horizon.Sdk.Sm;
using System;
using System.Collections.Generic;
using System.Numerics;

namespace Ryujinx.Horizon.Sdk.Sf.Hipc
{
    class ServerManager : ServerManagerBase, IDisposable
    {
        private readonly SmApi _sm;
        private readonly int _pointerBufferSize;
        private readonly bool _canDeferInvokeRequest;
        private readonly int _maxSessions;

        private readonly ulong _pointerBuffersBaseAddress;
        private readonly ulong _savedMessagesBaseAddress;

        private readonly object _resourceLock;
        private readonly ulong[] _sessionAllocationBitmap;
        private readonly HashSet<ServerSession> _sessions;
        private readonly HashSet<Server> _servers;

        public ServerManager(HeapAllocator allocator, SmApi sm, int maxPorts, ManagerOptions options, int maxSessions) : base(sm, options)
        {
            _sm = sm;
            _pointerBufferSize = options.PointerBufferSize;
            _canDeferInvokeRequest = options.CanDeferInvokeRequest;
            _maxSessions = maxSessions;

            if (allocator != null)
            {
                if (options.PointerBufferSize != 0)
                {
                    _pointerBuffersBaseAddress = allocator.Allocate((ulong)maxSessions * (ulong)options.PointerBufferSize);
                }

                if (options.CanDeferInvokeRequest)
                {
                    _savedMessagesBaseAddress = allocator.Allocate((ulong)maxSessions * Api.TlsMessageBufferSize);
                }
            }

            _resourceLock = new object();
            _sessionAllocationBitmap = new ulong[(maxSessions + 63) / 64];
            _sessions = new HashSet<ServerSession>();
            _servers = new HashSet<Server>();
        }

        private static PointerAndSize GetObjectBySessionIndex(ServerSession session, ulong baseAddress, ulong size)
        {
            return new PointerAndSize(baseAddress + (ulong)session.SessionIndex * size, size);
        }

        protected override ServerSession AllocateSession(int sessionHandle, ServiceObjectHolder obj)
        {
            int sessionIndex = -1;

            lock (_resourceLock)
            {
                if (_sessions.Count >= _maxSessions)
                {
                    return null;
                }

                for (int i = 0; i < _sessionAllocationBitmap.Length; i++)
                {
                    ref ulong mask = ref _sessionAllocationBitmap[i];

                    if (mask != ulong.MaxValue)
                    {
                        int bit = BitOperations.TrailingZeroCount(~mask);
                        sessionIndex = i * 64 + bit;
                        mask |= 1UL << bit;

                        break;
                    }
                }

                if (sessionIndex == -1)
                {
                    return null;
                }

                ServerSession session = new(sessionIndex, sessionHandle, obj);

                _sessions.Add(session);

                return session;
            }
        }

        protected override void FreeSession(ServerSession session)
        {
            if (session.ServiceObjectHolder.ServiceObject is IDisposable disposableObj)
            {
                disposableObj.Dispose();
            }

            lock (_resourceLock)
            {
                _sessionAllocationBitmap[session.SessionIndex / 64] &= ~(1UL << (session.SessionIndex & 63));
                _sessions.Remove(session);
            }
        }

        protected override Server AllocateServer(
            int portIndex,
            int portHandle,
            ServiceName name,
            bool managed,
            ServiceObjectHolder staticHoder)
        {
            lock (_resourceLock)
            {
                Server server = new(portIndex, portHandle, name, managed, staticHoder);

                _servers.Add(server);

                return server;
            }
        }

        protected override void DestroyServer(Server server)
        {
            lock (_resourceLock)
            {
                server.UnlinkFromMultiWaitHolder();
                Os.FinalizeMultiWaitHolder(server);

                if (server.Managed)
                {
                    // We should AbortOnFailure, but sometimes SM is already gone when this is called,
                    // so let's just ignore potential errors.
                    _sm.UnregisterService(server.Name);

                    HorizonStatic.Syscall.CloseHandle(server.PortHandle);
                }

                _servers.Remove(server);
            }
        }

        protected override PointerAndSize GetSessionPointerBuffer(ServerSession session)
        {
            if (_pointerBufferSize > 0)
            {
                return GetObjectBySessionIndex(session, _pointerBuffersBaseAddress, (ulong)_pointerBufferSize);
            }

            return PointerAndSize.Empty;
        }

        protected override PointerAndSize GetSessionSavedMessageBuffer(ServerSession session)
        {
            if (_canDeferInvokeRequest)
            {
                return GetObjectBySessionIndex(session, _savedMessagesBaseAddress, Api.TlsMessageBufferSize);
            }

            return PointerAndSize.Empty;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                lock (_resourceLock)
                {
                    ServerSession[] sessionsToClose = new ServerSession[_sessions.Count];

                    _sessions.CopyTo(sessionsToClose);

                    foreach (ServerSession session in sessionsToClose)
                    {
                        CloseSessionImpl(session);
                    }

                    Server[] serversToClose = new Server[_servers.Count];

                    _servers.CopyTo(serversToClose);

                    foreach (Server server in serversToClose)
                    {
                        DestroyServer(server);
                    }
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }
}