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
|
using ChocolArm64.Memory;
using Ryujinx.Common;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.HLE.Loaders.Npdm;
namespace Ryujinx.HLE.HOS
{
class ProgramLoader
{
private const bool AslrEnabled = true;
private const int ArgsHeaderSize = 8;
private const int ArgsDataSize = 0x9000;
private const int ArgsTotalSize = ArgsHeaderSize + ArgsDataSize;
public static bool LoadKernelInitalProcess(Horizon system, KernelInitialProcess kip)
{
int endOffset = kip.DataOffset + kip.Data.Length;
if (kip.BssSize != 0)
{
endOffset = kip.BssOffset + kip.BssSize;
}
int codeSize = BitUtils.AlignUp(kip.TextOffset + endOffset, KMemoryManager.PageSize);
int codePagesCount = codeSize / KMemoryManager.PageSize;
ulong codeBaseAddress = kip.Addr39Bits ? 0x8000000UL : 0x200000UL;
ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset;
int mmuFlags = 0;
if (AslrEnabled)
{
//TODO: Randomization.
mmuFlags |= 0x20;
}
if (kip.Addr39Bits)
{
mmuFlags |= (int)AddressSpaceType.Addr39Bits << 1;
}
if (kip.Is64Bits)
{
mmuFlags |= 1;
}
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
kip.Name,
kip.ProcessCategory,
kip.TitleId,
codeAddress,
codePagesCount,
mmuFlags,
0,
0);
MemoryRegion memoryRegion = kip.IsService
? MemoryRegion.Service
: MemoryRegion.Application;
KMemoryRegionManager region = system.MemoryRegions[(int)memoryRegion];
KernelResult result = region.AllocatePages((ulong)codePagesCount, false, out KPageList pageList);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
KProcess process = new KProcess(system);
result = process.InitializeKip(
creationInfo,
kip.Capabilities,
pageList,
system.ResourceLimit,
memoryRegion);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
result = LoadIntoMemory(process, kip, codeBaseAddress);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
process.DefaultCpuCore = kip.DefaultProcessorId;
result = process.Start(kip.MainThreadPriority, (ulong)kip.MainThreadStackSize);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
return false;
}
system.Processes.Add(process.Pid, process);
return true;
}
public static bool LoadStaticObjects(
Horizon system,
Npdm metaData,
IExecutable[] staticObjects,
byte[] arguments = null)
{
if (!metaData.Is64Bits)
{
Logger.PrintWarning(LogClass.Loader, "32-bits application detected!");
}
ulong argsStart = 0;
int argsSize = 0;
ulong codeStart = metaData.Is64Bits ? 0x8000000UL : 0x200000UL;
int codeSize = 0;
ulong[] nsoBase = new ulong[staticObjects.Length];
for (int index = 0; index < staticObjects.Length; index++)
{
IExecutable staticObject = staticObjects[index];
int textEnd = staticObject.TextOffset + staticObject.Text.Length;
int roEnd = staticObject.RoOffset + staticObject.Ro.Length;
int dataEnd = staticObject.DataOffset + staticObject.Data.Length + staticObject.BssSize;
int nsoSize = textEnd;
if ((uint)nsoSize < (uint)roEnd)
{
nsoSize = roEnd;
}
if ((uint)nsoSize < (uint)dataEnd)
{
nsoSize = dataEnd;
}
nsoSize = BitUtils.AlignUp(nsoSize, KMemoryManager.PageSize);
nsoBase[index] = codeStart + (ulong)codeSize;
codeSize += nsoSize;
if (arguments != null && argsSize == 0)
{
argsStart = (ulong)codeSize;
argsSize = BitUtils.AlignDown(arguments.Length * 2 + ArgsTotalSize - 1, KMemoryManager.PageSize);
codeSize += argsSize;
}
}
int codePagesCount = codeSize / KMemoryManager.PageSize;
int personalMmHeapPagesCount = metaData.PersonalMmHeapSize / KMemoryManager.PageSize;
ProcessCreationInfo creationInfo = new ProcessCreationInfo(
metaData.TitleName,
metaData.ProcessCategory,
metaData.Aci0.TitleId,
codeStart,
codePagesCount,
metaData.MmuFlags,
0,
personalMmHeapPagesCount);
KernelResult result;
KResourceLimit resourceLimit = new KResourceLimit(system);
long applicationRgSize = (long)system.MemoryRegions[(int)MemoryRegion.Application].Size;
result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize);
result |= resourceLimit.SetLimitValue(LimitableResource.Thread, 608);
result |= resourceLimit.SetLimitValue(LimitableResource.Event, 700);
result |= resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128);
result |= resourceLimit.SetLimitValue(LimitableResource.Session, 894);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization failed setting resource limit values.");
return false;
}
KProcess process = new KProcess(system);
MemoryRegion memoryRegion = (MemoryRegion)((metaData.Acid.Flags >> 2) & 0xf);
if (memoryRegion > MemoryRegion.NvServices)
{
Logger.PrintError(LogClass.Loader, $"Process initialization failed due to invalid ACID flags.");
return false;
}
result = process.Initialize(
creationInfo,
metaData.Aci0.KernelAccessControl.Capabilities,
resourceLimit,
memoryRegion);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
for (int index = 0; index < staticObjects.Length; index++)
{
Logger.PrintInfo(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}...");
result = LoadIntoMemory(process, staticObjects[index], nsoBase[index]);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
}
process.DefaultCpuCore = metaData.DefaultCpuId;
result = process.Start(metaData.MainThreadPriority, (ulong)metaData.MainThreadStackSize);
if (result != KernelResult.Success)
{
Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
return false;
}
system.Processes.Add(process.Pid, process);
return true;
}
private static KernelResult LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress)
{
ulong textStart = baseAddress + (ulong)image.TextOffset;
ulong roStart = baseAddress + (ulong)image.RoOffset;
ulong dataStart = baseAddress + (ulong)image.DataOffset;
ulong bssStart = baseAddress + (ulong)image.BssOffset;
ulong end = dataStart + (ulong)image.Data.Length;
if (image.BssSize != 0)
{
end = bssStart + (ulong)image.BssSize;
}
process.CpuMemory.WriteBytes((long)textStart, image.Text);
process.CpuMemory.WriteBytes((long)roStart, image.Ro);
process.CpuMemory.WriteBytes((long)dataStart, image.Data);
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize);
KernelResult SetProcessMemoryPermission(ulong address, ulong size, MemoryPermission permission)
{
if (size == 0)
{
return KernelResult.Success;
}
size = BitUtils.AlignUp(size, KMemoryManager.PageSize);
return process.MemoryManager.SetProcessMemoryPermission(address, size, permission);
}
KernelResult result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, MemoryPermission.ReadAndExecute);
if (result != KernelResult.Success)
{
return result;
}
result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, MemoryPermission.Read);
if (result != KernelResult.Success)
{
return result;
}
return SetProcessMemoryPermission(dataStart, end - dataStart, MemoryPermission.ReadAndWrite);
}
}
}
|