aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Nv/NvMap/NvMapIoctl.cs
blob: b1ae83078285a6839e2f66bf9ddb7e299747092d (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
using ChocolArm64.Memory;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Memory;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.Utilities;
using System.Collections.Concurrent;

namespace Ryujinx.HLE.HOS.Services.Nv.NvMap
{
    class NvMapIoctl
    {
        private const int FlagNotFreedYet = 1;

        private static ConcurrentDictionary<KProcess, IdDictionary> _maps;

        static NvMapIoctl()
        {
            _maps = new ConcurrentDictionary<KProcess, IdDictionary>();
        }

        public static int ProcessIoctl(ServiceCtx context, int cmd)
        {
            switch (cmd & 0xffff)
            {
                case 0x0101: return Create(context);
                case 0x0103: return FromId(context);
                case 0x0104: return Alloc (context);
                case 0x0105: return Free  (context);
                case 0x0109: return Param (context);
                case 0x010e: return GetId (context);
            }

            Logger.PrintWarning(LogClass.ServiceNv, $"Unsupported Ioctl command 0x{cmd:x8}!");

            return NvResult.NotSupported;
        }

        private static int Create(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapCreate args = MemoryHelper.Read<NvMapCreate>(context.Memory, inputPosition);

            if (args.Size == 0)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid size 0x{args.Size:x8}!");

                return NvResult.InvalidInput;
            }

            int size = IntUtils.AlignUp(args.Size, NvGpuVmm.PageSize);

            args.Handle = AddNvMap(context, new NvMapHandle(size));

            Logger.PrintInfo(LogClass.ServiceNv, $"Created map {args.Handle} with size 0x{size:x8}!");

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return NvResult.Success;
        }

        private static int FromId(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapFromId args = MemoryHelper.Read<NvMapFromId>(context.Memory, inputPosition);

            NvMapHandle map = GetNvMap(context, args.Id);

            if (map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");

                return NvResult.InvalidInput;
            }

            map.IncrementRefCount();

            args.Handle = args.Id;

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return NvResult.Success;
        }

        private static int Alloc(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapAlloc args = MemoryHelper.Read<NvMapAlloc>(context.Memory, inputPosition);

            NvMapHandle map = GetNvMap(context, args.Handle);

            if (map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");

                return NvResult.InvalidInput;
            }

            if ((args.Align & (args.Align - 1)) != 0)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid alignment 0x{args.Align:x8}!");

                return NvResult.InvalidInput;
            }

            if ((uint)args.Align < NvGpuVmm.PageSize)
            {
                args.Align = NvGpuVmm.PageSize;
            }

            int result = NvResult.Success;

            if (!map.Allocated)
            {
                map.Allocated = true;

                map.Align =       args.Align;
                map.Kind  = (byte)args.Kind;

                int size = IntUtils.AlignUp(map.Size, NvGpuVmm.PageSize);

                long address = args.Address;

                if (address == 0)
                {
                    //When the address is zero, we need to allocate
                    //our own backing memory for the NvMap.
                    //TODO: Is this allocation inside the transfer memory?
                    result = NvResult.OutOfMemory;
                }

                if (result == NvResult.Success)
                {
                    map.Size    = size;
                    map.Address = address;
                }
            }

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return result;
        }

        private static int Free(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapFree args = MemoryHelper.Read<NvMapFree>(context.Memory, inputPosition);

            NvMapHandle map = GetNvMap(context, args.Handle);

            if (map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");

                return NvResult.InvalidInput;
            }

            if (map.DecrementRefCount() <= 0)
            {
                DeleteNvMap(context, args.Handle);

                Logger.PrintInfo(LogClass.ServiceNv, $"Deleted map {args.Handle}!");

                args.Address = map.Address;
                args.Flags   = 0;
            }
            else
            {
                args.Address = 0;
                args.Flags   = FlagNotFreedYet;
            }

            args.Size = map.Size;

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return NvResult.Success;
        }

        private static int Param(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapParam args = MemoryHelper.Read<NvMapParam>(context.Memory, inputPosition);

            NvMapHandle map = GetNvMap(context, args.Handle);

            if (map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");

                return NvResult.InvalidInput;
            }

            switch ((NvMapHandleParam)args.Param)
            {
                case NvMapHandleParam.Size:  args.Result = map.Size;   break;
                case NvMapHandleParam.Align: args.Result = map.Align;  break;
                case NvMapHandleParam.Heap:  args.Result = 0x40000000; break;
                case NvMapHandleParam.Kind:  args.Result = map.Kind;   break;
                case NvMapHandleParam.Compr: args.Result = 0;          break;

                //Note: Base is not supported and returns an error.
                //Any other value also returns an error.
                default: return NvResult.InvalidInput;
            }

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return NvResult.Success;
        }

        private static int GetId(ServiceCtx context)
        {
            long inputPosition  = context.Request.GetBufferType0x21().Position;
            long outputPosition = context.Request.GetBufferType0x22().Position;

            NvMapGetId args = MemoryHelper.Read<NvMapGetId>(context.Memory, inputPosition);

            NvMapHandle map = GetNvMap(context, args.Handle);

            if (map == null)
            {
                Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");

                return NvResult.InvalidInput;
            }

            args.Id = args.Handle;

            MemoryHelper.Write(context.Memory, outputPosition, args);

            return NvResult.Success;
        }

        private static int AddNvMap(ServiceCtx context, NvMapHandle map)
        {
            IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>
            {
                IdDictionary newDict = new IdDictionary();

                newDict.Add(0, new NvMapHandle());

                return newDict;
            });

            return dict.Add(map);
        }

        private static bool DeleteNvMap(ServiceCtx context, int handle)
        {
            if (_maps.TryGetValue(context.Process, out IdDictionary dict))
            {
                return dict.Delete(handle) != null;
            }

            return false;
        }

        public static void InitializeNvMap(ServiceCtx context)
        {
            IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>new IdDictionary());

            dict.Add(0, new NvMapHandle());
        }

        public static NvMapHandle GetNvMapWithFb(ServiceCtx context, int handle)
        {
            if (_maps.TryGetValue(context.Process, out IdDictionary dict))
            {
                return dict.GetData<NvMapHandle>(handle);
            }

            return null;
        }

        public static NvMapHandle GetNvMap(ServiceCtx context, int handle)
        {
            if (handle != 0 && _maps.TryGetValue(context.Process, out IdDictionary dict))
            {
                return dict.GetData<NvMapHandle>(handle);
            }

            return null;
        }

        public static void UnloadProcess(KProcess process)
        {
            _maps.TryRemove(process, out _);
        }
    }
}