ProcessLoaderHelper.cs
1 using LibHac.Account; 2 using LibHac.Common; 3 using LibHac.Fs; 4 using LibHac.Fs.Fsa; 5 using LibHac.Fs.Shim; 6 using LibHac.Loader; 7 using LibHac.Ncm; 8 using LibHac.Ns; 9 using LibHac.Tools.Fs; 10 using LibHac.Tools.FsSystem; 11 using LibHac.Tools.FsSystem.NcaUtils; 12 using Ryujinx.Common; 13 using Ryujinx.Common.Logging; 14 using Ryujinx.HLE.HOS; 15 using Ryujinx.HLE.HOS.Kernel; 16 using Ryujinx.HLE.HOS.Kernel.Common; 17 using Ryujinx.HLE.HOS.Kernel.Memory; 18 using Ryujinx.HLE.HOS.Kernel.Process; 19 using Ryujinx.HLE.Loaders.Executables; 20 using Ryujinx.HLE.Loaders.Processes.Extensions; 21 using Ryujinx.Horizon.Common; 22 using Ryujinx.Horizon.Sdk.Arp; 23 using System; 24 using System.Linq; 25 using System.Runtime.InteropServices; 26 using ApplicationId = LibHac.Ncm.ApplicationId; 27 28 namespace Ryujinx.HLE.Loaders.Processes 29 { 30 static class ProcessLoaderHelper 31 { 32 // NOTE: If you want to change this value make sure to increment the InternalVersion of Ptc and PtcProfiler. 33 // You also need to add a new migration path and adjust the existing ones. 34 // TODO: Remove this workaround when ASLR is implemented. 35 private const ulong CodeStartOffset = 0x500000UL; 36 37 public static LibHac.Result RegisterProgramMapInfo(Switch device, IFileSystem partitionFileSystem) 38 { 39 ulong applicationId = 0; 40 int programCount = 0; 41 42 Span<bool> hasIndex = stackalloc bool[0x10]; 43 44 foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca")) 45 { 46 Nca nca = partitionFileSystem.GetNca(device.FileSystem.KeySet, fileEntry.FullPath); 47 48 if (!nca.IsProgram()) 49 { 50 continue; 51 } 52 53 ulong currentMainProgramId = nca.GetProgramIdBase(); 54 55 if (applicationId == 0 && currentMainProgramId != 0) 56 { 57 applicationId = currentMainProgramId; 58 } 59 60 if (applicationId != currentMainProgramId) 61 { 62 // Currently there aren't any known multi-application game cards containing multi-program applications, 63 // so because multi-application game cards are the only way we could run into multiple applications 64 // we'll just return that there's a single program. 65 programCount = 1; 66 67 break; 68 } 69 70 hasIndex[nca.GetProgramIndex()] = true; 71 } 72 73 if (programCount == 0) 74 { 75 for (int i = 0; i < hasIndex.Length && hasIndex[i]; i++) 76 { 77 programCount++; 78 } 79 } 80 81 if (programCount <= 0) 82 { 83 return LibHac.Result.Success; 84 } 85 86 Span<ProgramIndexMapInfo> mapInfo = stackalloc ProgramIndexMapInfo[0x10]; 87 88 for (int i = 0; i < programCount; i++) 89 { 90 mapInfo[i].ProgramId = new ProgramId(applicationId + (uint)i); 91 mapInfo[i].MainProgramId = new ApplicationId(applicationId); 92 mapInfo[i].ProgramIndex = (byte)i; 93 } 94 95 return device.System.LibHacHorizonManager.NsClient.Fs.RegisterProgramIndexMapInfo(mapInfo[..programCount]); 96 } 97 98 public static LibHac.Result EnsureSaveData(Switch device, ApplicationId applicationId, BlitStruct<ApplicationControlProperty> applicationControlProperty) 99 { 100 Logger.Info?.Print(LogClass.Application, "Ensuring required savedata exists."); 101 102 ref ApplicationControlProperty control = ref applicationControlProperty.Value; 103 104 if (LibHac.Common.Utilities.IsZeros(applicationControlProperty.ByteSpan)) 105 { 106 // If the current application doesn't have a loaded control property, create a dummy one and set the savedata sizes so a user savedata will be created. 107 control = ref new BlitStruct<ApplicationControlProperty>(1).Value; 108 109 // The set sizes don't actually matter as long as they're non-zero because we use directory savedata. 110 control.UserAccountSaveDataSize = 0x4000; 111 control.UserAccountSaveDataJournalSize = 0x4000; 112 control.SaveDataOwnerId = applicationId.Value; 113 114 Logger.Warning?.Print(LogClass.Application, "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games."); 115 } 116 117 LibHac.Result resultCode = device.System.LibHacHorizonManager.RyujinxClient.Fs.EnsureApplicationCacheStorage(out _, out _, applicationId, in control); 118 if (resultCode.IsFailure()) 119 { 120 Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationCacheStorage. Result code {resultCode.ToStringWithName()}"); 121 122 return resultCode; 123 } 124 125 Uid userId = device.System.AccountManager.LastOpenedUser.UserId.ToLibHacUid(); 126 127 resultCode = device.System.LibHacHorizonManager.RyujinxClient.Fs.EnsureApplicationSaveData(out _, applicationId, in control, in userId); 128 if (resultCode.IsFailure()) 129 { 130 Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationSaveData. Result code {resultCode.ToStringWithName()}"); 131 } 132 133 return resultCode; 134 } 135 136 public static bool LoadKip(KernelContext context, KipExecutable kip) 137 { 138 uint endOffset = kip.DataOffset + (uint)kip.Data.Length; 139 140 if (kip.BssSize != 0) 141 { 142 endOffset = kip.BssOffset + kip.BssSize; 143 } 144 145 uint codeSize = BitUtils.AlignUp<uint>(kip.TextOffset + endOffset, KPageTableBase.PageSize); 146 int codePagesCount = (int)(codeSize / KPageTableBase.PageSize); 147 ulong codeBaseAddress = kip.Is64BitAddressSpace ? 0x8000000UL : 0x200000UL; 148 ulong codeAddress = codeBaseAddress + kip.TextOffset; 149 150 ProcessCreationFlags flags = 0; 151 152 if (ProcessConst.AslrEnabled) 153 { 154 // TODO: Randomization. 155 156 flags |= ProcessCreationFlags.EnableAslr; 157 } 158 159 if (kip.Is64BitAddressSpace) 160 { 161 flags |= ProcessCreationFlags.AddressSpace64Bit; 162 } 163 164 if (kip.Is64Bit) 165 { 166 flags |= ProcessCreationFlags.Is64Bit; 167 } 168 169 ProcessCreationInfo creationInfo = new(kip.Name, kip.Version, kip.ProgramId, codeAddress, codePagesCount, flags, 0, 0); 170 MemoryRegion memoryRegion = kip.UsesSecureMemory ? MemoryRegion.Service : MemoryRegion.Application; 171 KMemoryRegionManager region = context.MemoryManager.MemoryRegions[(int)memoryRegion]; 172 173 Result result = region.AllocatePages(out KPageList pageList, (ulong)codePagesCount); 174 if (result != Result.Success) 175 { 176 Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\"."); 177 178 return false; 179 } 180 181 KProcess process = new(context); 182 183 var processContextFactory = new ArmProcessContextFactory( 184 context.Device.System.TickSource, 185 context.Device.Gpu, 186 string.Empty, 187 string.Empty, 188 false, 189 codeAddress, 190 codeSize); 191 192 result = process.InitializeKip(creationInfo, kip.Capabilities, pageList, context.ResourceLimit, memoryRegion, processContextFactory); 193 if (result != Result.Success) 194 { 195 Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\"."); 196 197 return false; 198 } 199 200 result = LoadIntoMemory(process, kip, codeBaseAddress); 201 if (result != Result.Success) 202 { 203 Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\"."); 204 205 return false; 206 } 207 208 process.DefaultCpuCore = kip.IdealCoreId; 209 210 result = process.Start(kip.Priority, (ulong)kip.StackSize); 211 if (result != Result.Success) 212 { 213 Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\"."); 214 215 return false; 216 } 217 218 context.Processes.TryAdd(process.Pid, process); 219 220 return true; 221 } 222 223 public static ProcessResult LoadNsos( 224 Switch device, 225 KernelContext context, 226 MetaLoader metaLoader, 227 BlitStruct<ApplicationControlProperty> applicationControlProperties, 228 bool diskCacheEnabled, 229 bool allowCodeMemoryForJit, 230 string name, 231 ulong programId, 232 byte programIndex, 233 byte[] arguments = null, 234 params IExecutable[] executables) 235 { 236 context.Device.System.ServiceTable.WaitServicesReady(); 237 238 LibHac.Result resultCode = metaLoader.GetNpdm(out var npdm); 239 240 if (resultCode.IsFailure()) 241 { 242 Logger.Error?.Print(LogClass.Loader, $"Process initialization failed getting npdm. Result Code {resultCode.ToStringWithName()}"); 243 244 return ProcessResult.Failed; 245 } 246 247 ref readonly var meta = ref npdm.Meta; 248 249 ulong argsStart = 0; 250 uint argsSize = 0; 251 ulong codeStart = ((meta.Flags & 1) != 0 ? 0x8000000UL : 0x200000UL) + CodeStartOffset; 252 uint codeSize = 0; 253 254 var buildIds = executables.Select(e => (e switch 255 { 256 NsoExecutable nso => Convert.ToHexString(nso.BuildId.ItemsRo.ToArray()), 257 NroExecutable nro => Convert.ToHexString(nro.Header.BuildId), 258 _ => "", 259 }).ToUpper()); 260 261 ulong[] nsoBase = new ulong[executables.Length]; 262 263 for (int index = 0; index < executables.Length; index++) 264 { 265 IExecutable nso = executables[index]; 266 267 uint textEnd = nso.TextOffset + (uint)nso.Text.Length; 268 uint roEnd = nso.RoOffset + (uint)nso.Ro.Length; 269 uint dataEnd = nso.DataOffset + (uint)nso.Data.Length + nso.BssSize; 270 271 uint nsoSize = textEnd; 272 273 if (nsoSize < roEnd) 274 { 275 nsoSize = roEnd; 276 } 277 278 if (nsoSize < dataEnd) 279 { 280 nsoSize = dataEnd; 281 } 282 283 nsoSize = BitUtils.AlignUp<uint>(nsoSize, KPageTableBase.PageSize); 284 285 nsoBase[index] = codeStart + codeSize; 286 287 codeSize += nsoSize; 288 289 if (arguments != null && argsSize == 0) 290 { 291 argsStart = codeSize; 292 293 argsSize = (uint)BitUtils.AlignDown(arguments.Length * 2 + ProcessConst.NsoArgsTotalSize - 1, KPageTableBase.PageSize); 294 295 codeSize += argsSize; 296 } 297 } 298 299 int codePagesCount = (int)(codeSize / KPageTableBase.PageSize); 300 int personalMmHeapPagesCount = (int)(meta.SystemResourceSize / KPageTableBase.PageSize); 301 302 ProcessCreationInfo creationInfo = new( 303 name, 304 (int)meta.Version, 305 programId, 306 codeStart, 307 codePagesCount, 308 (ProcessCreationFlags)meta.Flags | ProcessCreationFlags.IsApplication, 309 0, 310 personalMmHeapPagesCount); 311 312 context.Device.System.LibHacHorizonManager.InitializeApplicationClient(new ProgramId(programId), in npdm); 313 314 Result result; 315 316 KResourceLimit resourceLimit = new(context); 317 318 long applicationRgSize = (long)context.MemoryManager.MemoryRegions[(int)MemoryRegion.Application].Size; 319 320 result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize); 321 322 if (result.IsSuccess) 323 { 324 result = resourceLimit.SetLimitValue(LimitableResource.Thread, 608); 325 } 326 327 if (result.IsSuccess) 328 { 329 result = resourceLimit.SetLimitValue(LimitableResource.Event, 700); 330 } 331 332 if (result.IsSuccess) 333 { 334 result = resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128); 335 } 336 337 if (result.IsSuccess) 338 { 339 result = resourceLimit.SetLimitValue(LimitableResource.Session, 894); 340 } 341 342 if (result != Result.Success) 343 { 344 Logger.Error?.Print(LogClass.Loader, "Process initialization failed setting resource limit values."); 345 346 return ProcessResult.Failed; 347 } 348 349 KProcess process = new(context, allowCodeMemoryForJit); 350 351 // NOTE: This field doesn't exists one firmware pre-5.0.0, a workaround have to be found. 352 MemoryRegion memoryRegion = (MemoryRegion)(npdm.Acid.Flags >> 2 & 0xf); 353 if (memoryRegion > MemoryRegion.NvServices) 354 { 355 Logger.Error?.Print(LogClass.Loader, "Process initialization failed due to invalid ACID flags."); 356 357 return ProcessResult.Failed; 358 } 359 360 string displayVersion; 361 362 if (metaLoader.GetProgramId() > 0x0100000000007FFF) 363 { 364 displayVersion = applicationControlProperties.Value.DisplayVersionString.ToString(); 365 } 366 else 367 { 368 displayVersion = device.System.ContentManager.GetCurrentFirmwareVersion()?.VersionString ?? string.Empty; 369 } 370 371 var processContextFactory = new ArmProcessContextFactory( 372 context.Device.System.TickSource, 373 context.Device.Gpu, 374 $"{programId:x16}", 375 displayVersion, 376 diskCacheEnabled, 377 codeStart, 378 codeSize); 379 380 result = process.Initialize( 381 creationInfo, 382 MemoryMarshal.Cast<byte, uint>(npdm.KernelCapabilityData), 383 resourceLimit, 384 memoryRegion, 385 processContextFactory); 386 387 if (result != Result.Success) 388 { 389 Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\"."); 390 391 return ProcessResult.Failed; 392 } 393 394 for (int index = 0; index < executables.Length; index++) 395 { 396 Logger.Info?.Print(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}..."); 397 398 result = LoadIntoMemory(process, executables[index], nsoBase[index]); 399 if (result != Result.Success) 400 { 401 Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\"."); 402 403 return ProcessResult.Failed; 404 } 405 } 406 407 process.DefaultCpuCore = meta.DefaultCpuId; 408 409 context.Processes.TryAdd(process.Pid, process); 410 411 // Keep the build ids because the tamper machine uses them to know which process to associate a 412 // tamper to and also keep the starting address of each executable inside a process because some 413 // memory modifications are relative to this address. 414 ProcessTamperInfo tamperInfo = new( 415 process, 416 buildIds, 417 nsoBase, 418 process.MemoryManager.HeapRegionStart, 419 process.MemoryManager.AliasRegionStart, 420 process.MemoryManager.CodeRegionStart); 421 422 // Once everything is loaded, we can load cheats. 423 device.Configuration.VirtualFileSystem.ModLoader.LoadCheats(programId, tamperInfo, device.TamperMachine); 424 425 ProcessResult processResult = new( 426 metaLoader, 427 applicationControlProperties, 428 diskCacheEnabled, 429 allowCodeMemoryForJit, 430 processContextFactory.DiskCacheLoadState, 431 process.Pid, 432 meta.MainThreadPriority, 433 meta.MainThreadStackSize, 434 device.System.State.DesiredTitleLanguage); 435 436 // Register everything in arp service. 437 device.System.ServiceTable.ArpWriter.AcquireRegistrar(out IRegistrar registrar); 438 registrar.SetApplicationControlProperty(MemoryMarshal.Cast<byte, Horizon.Sdk.Ns.ApplicationControlProperty>(applicationControlProperties.ByteSpan)[0]); 439 // TODO: Handle Version and StorageId when it will be needed. 440 registrar.SetApplicationLaunchProperty(new ApplicationLaunchProperty() 441 { 442 ApplicationId = new Horizon.Sdk.Ncm.ApplicationId(programId), 443 Version = 0x00, 444 Storage = Horizon.Sdk.Ncm.StorageId.BuiltInUser, 445 PatchStorage = Horizon.Sdk.Ncm.StorageId.None, 446 ApplicationKind = ApplicationKind.Application, 447 }); 448 449 device.System.ServiceTable.ArpReader.GetApplicationInstanceId(out ulong applicationInstanceId, process.Pid); 450 device.System.ServiceTable.ArpWriter.AcquireApplicationProcessPropertyUpdater(out IUpdater updater, applicationInstanceId); 451 updater.SetApplicationProcessProperty(process.Pid, new ApplicationProcessProperty() { ProgramIndex = programIndex }); 452 453 return processResult; 454 } 455 456 public static Result LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress) 457 { 458 ulong textStart = baseAddress + image.TextOffset; 459 ulong roStart = baseAddress + image.RoOffset; 460 ulong dataStart = baseAddress + image.DataOffset; 461 ulong bssStart = baseAddress + image.BssOffset; 462 463 ulong end = dataStart + (ulong)image.Data.Length; 464 465 if (image.BssSize != 0) 466 { 467 end = bssStart + image.BssSize; 468 } 469 470 process.CpuMemory.Write(textStart, image.Text); 471 process.CpuMemory.Write(roStart, image.Ro); 472 process.CpuMemory.Write(dataStart, image.Data); 473 474 process.CpuMemory.Fill(bssStart, image.BssSize, 0); 475 476 Result SetProcessMemoryPermission(ulong address, ulong size, KMemoryPermission permission) 477 { 478 if (size == 0) 479 { 480 return Result.Success; 481 } 482 483 size = BitUtils.AlignUp<ulong>(size, KPageTableBase.PageSize); 484 485 return process.MemoryManager.SetProcessMemoryPermission(address, size, permission); 486 } 487 488 Result result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, KMemoryPermission.ReadAndExecute); 489 if (result != Result.Success) 490 { 491 return result; 492 } 493 494 result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, KMemoryPermission.Read); 495 if (result != Result.Success) 496 { 497 return result; 498 } 499 500 return SetProcessMemoryPermission(dataStart, end - dataStart, KMemoryPermission.ReadAndWrite); 501 } 502 } 503 }