aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Nv/NvGpuAS/NvGpuASIoctl.cs
blob: fed41042293bd297e3764a383123aa4087d5bc05 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using ChocolArm64.Memory;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Memory;
using Ryujinx.HLE.HOS.Services.Nv.NvMap;
using System;
using System.Collections.Concurrent;

namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS
{
    class NvGpuASIoctl
    {
        private const int FlagFixedOffset = 1;

        private const int FlagRemapSubRange = 0x100;

        private static ConcurrentDictionary<Process, NvGpuASCtx> ASCtxs;

        static NvGpuASIoctl()
        {
            ASCtxs = new ConcurrentDictionary<Process, NvGpuASCtx>();
        }

        public static int ProcessIoctl(ServiceCtx Context, int Cmd)
        {
            switch (Cmd & 0xffff)
            {
                case 0x4101: return BindChannel (Context);
                case 0x4102: return AllocSpace  (Context);
                case 0x4103: return FreeSpace   (Context);
                case 0x4105: return UnmapBuffer (Context);
                case 0x4106: return MapBufferEx (Context);
                case 0x4108: return GetVaRegions(Context);
                case 0x4109: return InitializeEx(Context);
                case 0x4114: return Remap       (Context, Cmd);
            }

            throw new NotImplementedException(Cmd.ToString("x8"));
        }

        private static int BindChannel(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return NvResult.Success;
        }

        private static int AllocSpace(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            NvGpuASAllocSpace Args = MemoryHelper.Read<NvGpuASAllocSpace>(Context.Memory, InputPosition);

            NvGpuASCtx ASCtx = GetASCtx(Context);

            ulong Size = (ulong)Args.Pages *
                         (ulong)Args.PageSize;

            int Result = NvResult.Success;

            lock (ASCtx)
            {
                //Note: When the fixed offset flag is not set,
                //the Offset field holds the alignment size instead.
                if ((Args.Flags & FlagFixedOffset) != 0)
                {
                    Args.Offset = ASCtx.Vmm.ReserveFixed(Args.Offset, (long)Size);
                }
                else
                {
                    Args.Offset = ASCtx.Vmm.Reserve((long)Size, Args.Offset);
                }

                if (Args.Offset < 0)
                {
                    Args.Offset = 0;

                    Logger.PrintWarning(LogClass.ServiceNv, $"Failed to allocate size {Size:x16}!");

                    Result = NvResult.OutOfMemory;
                }
                else
                {
                    ASCtx.AddReservation(Args.Offset, (long)Size);
                }
            }

            MemoryHelper.Write(Context.Memory, OutputPosition, Args);

            return Result;
        }

        private static int FreeSpace(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            NvGpuASAllocSpace Args = MemoryHelper.Read<NvGpuASAllocSpace>(Context.Memory, InputPosition);

            NvGpuASCtx ASCtx = GetASCtx(Context);

            int Result = NvResult.Success;

            lock (ASCtx)
            {
                ulong Size = (ulong)Args.Pages *
                             (ulong)Args.PageSize;

                if (ASCtx.RemoveReservation(Args.Offset))
                {
                    ASCtx.Vmm.Free(Args.Offset, (long)Size);
                }
                else
                {
                    Logger.PrintWarning(LogClass.ServiceNv,
                        $"Failed to free offset 0x{Args.Offset:x16} size 0x{Size:x16}!");

                    Result = NvResult.InvalidInput;
                }
            }

            return Result;
        }

        private static int UnmapBuffer(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            NvGpuASUnmapBuffer Args = MemoryHelper.Read<NvGpuASUnmapBuffer>(Context.Memory, InputPosition);

            NvGpuASCtx ASCtx = GetASCtx(Context);

            lock (ASCtx)
            {
                if (ASCtx.RemoveMap(Args.Offset, out long Size))
                {
                    if (Size != 0)
                    {
                        ASCtx.Vmm.Free(Args.Offset, Size);
                    }
                }
                else
                {
                    Logger.PrintWarning(LogClass.ServiceNv, $"Invalid buffer offset {Args.Offset:x16}!");
                }
            }

            return NvResult.Success;
        }

        private static int MapBufferEx(ServiceCtx Context)
        {
            const string MapErrorMsg = "Failed to map fixed buffer with offset 0x{0:x16} and size 0x{1:x16}!";

            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            NvGpuASMapBufferEx Args = MemoryHelper.Read<NvGpuASMapBufferEx>(Context.Memory, InputPosition);

            NvGpuASCtx ASCtx = GetASCtx(Context);

            NvMapHandle Map = NvMapIoctl.GetNvMapWithFb(Context, Args.NvMapHandle);

            if (Map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{Args.NvMapHandle:x8}!");

                return NvResult.InvalidInput;
            }

            long PA;

            if ((Args.Flags & FlagRemapSubRange) != 0)
            {
                lock (ASCtx)
                {
                    if (ASCtx.TryGetMapPhysicalAddress(Args.Offset, out PA))
                    {
                        long VA = Args.Offset + Args.BufferOffset;

                        PA += Args.BufferOffset;

                        if (ASCtx.Vmm.Map(PA, VA, Args.MappingSize) < 0)
                        {
                            string Msg = string.Format(MapErrorMsg, VA, Args.MappingSize);

                            Logger.PrintWarning(LogClass.ServiceNv, Msg);

                            return NvResult.InvalidInput;
                        }

                        return NvResult.Success;
                    }
                    else
                    {
                        Logger.PrintWarning(LogClass.ServiceNv, $"Address 0x{Args.Offset:x16} not mapped!");

                        return NvResult.InvalidInput;
                    }
                }
            }

            PA = Map.Address + Args.BufferOffset;

            long Size = Args.MappingSize;

            if (Size == 0)
            {
                Size = (uint)Map.Size;
            }

            int Result = NvResult.Success;

            lock (ASCtx)
            {
                //Note: When the fixed offset flag is not set,
                //the Offset field holds the alignment size instead.
                bool VaAllocated = (Args.Flags & FlagFixedOffset) == 0;

                if (!VaAllocated)
                {
                    if (ASCtx.ValidateFixedBuffer(Args.Offset, Size))
                    {
                        Args.Offset = ASCtx.Vmm.Map(PA, Args.Offset, Size);
                    }
                    else
                    {
                        string Msg = string.Format(MapErrorMsg, Args.Offset, Size);

                        Logger.PrintWarning(LogClass.ServiceNv, Msg);

                        Result = NvResult.InvalidInput;
                    }
                }
                else
                {
                    Args.Offset = ASCtx.Vmm.Map(PA, Size);
                }

                if (Args.Offset < 0)
                {
                    Args.Offset = 0;

                    Logger.PrintWarning(LogClass.ServiceNv, $"Failed to map size 0x{Size:x16}!");

                    Result = NvResult.InvalidInput;
                }
                else
                {
                    ASCtx.AddMap(Args.Offset, Size, PA, VaAllocated);
                }
            }

            MemoryHelper.Write(Context.Memory, OutputPosition, Args);

            return Result;
        }

        private static int GetVaRegions(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return NvResult.Success;
        }

        private static int InitializeEx(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21().Position;
            long OutputPosition = Context.Request.GetBufferType0x22().Position;

            Logger.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return NvResult.Success;
        }

        private static int Remap(ServiceCtx Context, int Cmd)
        {
            int Count = ((Cmd >> 16) & 0xff) / 0x14;

            long InputPosition  = Context.Request.GetBufferType0x21().Position;

            for (int Index = 0; Index < Count; Index++, InputPosition += 0x14)
            {
                NvGpuASRemap Args = MemoryHelper.Read<NvGpuASRemap>(Context.Memory, InputPosition);

                NvGpuVmm Vmm = GetASCtx(Context).Vmm;

                NvMapHandle Map = NvMapIoctl.GetNvMapWithFb(Context, Args.NvMapHandle);

                if (Map == null)
                {
                    Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{Args.NvMapHandle:x8}!");

                    return NvResult.InvalidInput;
                }

                long Result = Vmm.Map(Map.Address, (long)(uint)Args.Offset << 16,
                                                   (long)(uint)Args.Pages  << 16);

                if (Result < 0)
                {
                    Logger.PrintWarning(LogClass.ServiceNv,
                        $"Page 0x{Args.Offset:x16} size 0x{Args.Pages:x16} not allocated!");

                    return NvResult.InvalidInput;
                }
            }

            return NvResult.Success;
        }

        public static NvGpuASCtx GetASCtx(ServiceCtx Context)
        {
            return ASCtxs.GetOrAdd(Context.Process, (Key) => new NvGpuASCtx(Context));
        }

        public static void UnloadProcess(Process Process)
        {
            ASCtxs.TryRemove(Process, out _);
        }
    }
}