aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThreadSync.cs
blob: ecda9e2d00a82677455506430cbae18a0e2dc403 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.HOS.Kernel.Threading;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
{
    partial class SvcHandler
    {
        public KernelResult WaitSynchronization64(ulong handlesPtr, int handlesCount, long timeout, out int handleIndex)
        {
            return WaitSynchronization(handlesPtr, handlesCount, timeout, out handleIndex);
        }

        private KernelResult WaitSynchronization(ulong handlesPtr, int handlesCount, long timeout, out int handleIndex)
        {
            handleIndex = 0;

            if ((uint)handlesCount > 0x40)
            {
                return KernelResult.MaximumExceeded;
            }

            List<KSynchronizationObject> syncObjs = new List<KSynchronizationObject>();

            for (int index = 0; index < handlesCount; index++)
            {
                int handle = _memory.ReadInt32((long)handlesPtr + index * 4);

                KSynchronizationObject syncObj = _process.HandleTable.GetObject<KSynchronizationObject>(handle);

                if (syncObj == null)
                {
                    break;
                }

                syncObjs.Add(syncObj);
            }

            return _system.Synchronization.WaitFor(syncObjs.ToArray(), timeout, out handleIndex);
        }

        public KernelResult CancelSynchronization64(int handle)
        {
            return CancelSynchronization(handle);
        }

        private KernelResult CancelSynchronization(int handle)
        {
            KThread thread = _process.HandleTable.GetKThread(handle);

            if (thread == null)
            {
                return KernelResult.InvalidHandle;
            }

            thread.CancelSynchronization();

            return KernelResult.Success;
        }

        public KernelResult ArbitrateLock64(int ownerHandle, ulong mutexAddress, int requesterHandle)
        {
            return ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
        }

        private KernelResult ArbitrateLock(int ownerHandle, ulong mutexAddress, int requesterHandle)
        {
            if (IsPointingInsideKernel(mutexAddress))
            {
                return KernelResult.InvalidMemState;
            }

            if (IsAddressNotWordAligned(mutexAddress))
            {
                return KernelResult.InvalidAddress;
            }

            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            return currentProcess.AddressArbiter.ArbitrateLock(ownerHandle, mutexAddress, requesterHandle);
        }

        public KernelResult ArbitrateUnlock64(ulong mutexAddress)
        {
            return ArbitrateUnlock(mutexAddress);
        }

        private KernelResult ArbitrateUnlock(ulong mutexAddress)
        {
            if (IsPointingInsideKernel(mutexAddress))
            {
                return KernelResult.InvalidMemState;
            }

            if (IsAddressNotWordAligned(mutexAddress))
            {
                return KernelResult.InvalidAddress;
            }

            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            return currentProcess.AddressArbiter.ArbitrateUnlock(mutexAddress);
        }

        public KernelResult WaitProcessWideKeyAtomic64(
            ulong mutexAddress,
            ulong condVarAddress,
            int   handle,
            long  timeout)
        {
            return WaitProcessWideKeyAtomic(mutexAddress, condVarAddress, handle, timeout);
        }

        private KernelResult WaitProcessWideKeyAtomic(
            ulong mutexAddress,
            ulong condVarAddress,
            int   handle,
            long  timeout)
        {
            if (IsPointingInsideKernel(mutexAddress))
            {
                return KernelResult.InvalidMemState;
            }

            if (IsAddressNotWordAligned(mutexAddress))
            {
                return KernelResult.InvalidAddress;
            }

            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            return currentProcess.AddressArbiter.WaitProcessWideKeyAtomic(
                mutexAddress,
                condVarAddress,
                handle,
                timeout);
        }

        public KernelResult SignalProcessWideKey64(ulong address, int count)
        {
            return SignalProcessWideKey(address, count);
        }

        private KernelResult SignalProcessWideKey(ulong address, int count)
        {
            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            currentProcess.AddressArbiter.SignalProcessWideKey(address, count);

            return KernelResult.Success;
        }

        public KernelResult WaitForAddress64(ulong address, ArbitrationType type, int value, long timeout)
        {
            return WaitForAddress(address, type, value, timeout);
        }

        private KernelResult WaitForAddress(ulong address, ArbitrationType type, int value, long timeout)
        {
            if (IsPointingInsideKernel(address))
            {
                return KernelResult.InvalidMemState;
            }

            if (IsAddressNotWordAligned(address))
            {
                return KernelResult.InvalidAddress;
            }

            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            KernelResult result;

            switch (type)
            {
                case ArbitrationType.WaitIfLessThan:
                    result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, false, timeout);
                    break;

                case ArbitrationType.DecrementAndWaitIfLessThan:
                    result = currentProcess.AddressArbiter.WaitForAddressIfLessThan(address, value, true, timeout);
                    break;

                case ArbitrationType.WaitIfEqual:
                    result = currentProcess.AddressArbiter.WaitForAddressIfEqual(address, value, timeout);
                    break;

                default:
                    result = KernelResult.InvalidEnumValue;
                    break;
            }

            return result;
        }

        public KernelResult SignalToAddress64(ulong address, SignalType type, int value, int count)
        {
            return SignalToAddress(address, type, value, count);
        }

        private KernelResult SignalToAddress(ulong address, SignalType type, int value, int count)
        {
            if (IsPointingInsideKernel(address))
            {
                return KernelResult.InvalidMemState;
            }

            if (IsAddressNotWordAligned(address))
            {
                return KernelResult.InvalidAddress;
            }

            KProcess currentProcess = _system.Scheduler.GetCurrentProcess();

            KernelResult result;

            switch (type)
            {
                case SignalType.Signal:
                    result = currentProcess.AddressArbiter.Signal(address, count);
                    break;

                case SignalType.SignalAndIncrementIfEqual:
                    result = currentProcess.AddressArbiter.SignalAndIncrementIfEqual(address, value, count);
                    break;

                case SignalType.SignalAndModifyIfEqual:
                    result = currentProcess.AddressArbiter.SignalAndModifyIfEqual(address, value, count);
                    break;

                default:
                    result = KernelResult.InvalidEnumValue;
                    break;
            }

            return result;
        }

        private bool IsPointingInsideKernel(ulong address)
        {
            return (address + 0x1000000000) < 0xffffff000;
        }

        private bool IsAddressNotWordAligned(ulong address)
        {
            return (address & 3) != 0;
        }
    }
}