From 00579927e43bf55ee06ae02933c1e755fb4120eb Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 28 Nov 2018 20:18:09 -0200 Subject: Better process implementation (#491) * Initial implementation of KProcess * Some improvements to the memory manager, implement back guest stack trace printing * Better GetInfo implementation, improve checking in some places with information from process capabilities * Allow the cpu to read/write from the correct memory locations for accesses crossing a page boundary * Change long -> ulong for address/size on memory related methods to avoid unnecessary casts * Attempt at implementing ldr:ro with new KProcess * Allow BSS with size 0 on ldr:ro * Add checking for memory block slab heap usage, return errors if full, exit gracefully * Use KMemoryBlockSize const from KMemoryManager * Allow all methods to read from non-contiguous locations * Fix for TransactParcelAuto * Address PR feedback, additionally fix some small issues related to the KIP loader and implement SVCs GetProcessId, GetProcessList, GetSystemInfo, CreatePort and ManageNamedPort * Fix wrong check for source pages count from page list on MapPhysicalMemory * Fix some issues with UnloadNro on ldr:ro --- Ryujinx.HLE/Loaders/Executables/IExecutable.cs | 9 +- .../Loaders/Executables/KernelInitialProcess.cs | 149 +++++++++++++++++++++ Ryujinx.HLE/Loaders/Executables/Nro.cs | 65 --------- Ryujinx.HLE/Loaders/Executables/Nso.cs | 127 ------------------ .../Loaders/Executables/NxRelocatableObject.cs | 64 +++++++++ Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs | 109 +++++++++++++++ 6 files changed, 324 insertions(+), 199 deletions(-) create mode 100644 Ryujinx.HLE/Loaders/Executables/KernelInitialProcess.cs delete mode 100644 Ryujinx.HLE/Loaders/Executables/Nro.cs delete mode 100644 Ryujinx.HLE/Loaders/Executables/Nso.cs create mode 100644 Ryujinx.HLE/Loaders/Executables/NxRelocatableObject.cs create mode 100644 Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs (limited to 'Ryujinx.HLE/Loaders/Executables') diff --git a/Ryujinx.HLE/Loaders/Executables/IExecutable.cs b/Ryujinx.HLE/Loaders/Executables/IExecutable.cs index 6f0952ab..d3eefde6 100644 --- a/Ryujinx.HLE/Loaders/Executables/IExecutable.cs +++ b/Ryujinx.HLE/Loaders/Executables/IExecutable.cs @@ -1,20 +1,15 @@ namespace Ryujinx.HLE.Loaders.Executables { - public interface IExecutable + interface IExecutable { - string FilePath { get; } - byte[] Text { get; } byte[] RO { get; } byte[] Data { get; } - long SourceAddress { get; } - long BssAddress { get; } - - int Mod0Offset { get; } int TextOffset { get; } int ROOffset { get; } int DataOffset { get; } + int BssOffset { get; } int BssSize { get; } } } \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/KernelInitialProcess.cs b/Ryujinx.HLE/Loaders/Executables/KernelInitialProcess.cs new file mode 100644 index 00000000..1395d56f --- /dev/null +++ b/Ryujinx.HLE/Loaders/Executables/KernelInitialProcess.cs @@ -0,0 +1,149 @@ +using Ryujinx.HLE.Loaders.Compression; +using System.IO; + +namespace Ryujinx.HLE.Loaders.Executables +{ + class KernelInitialProcess : IExecutable + { + public string Name { get; private set; } + + public long TitleId { get; private set; } + + public int ProcessCategory { get; private set; } + + public byte MainThreadPriority { get; private set; } + public byte DefaultProcessorId { get; private set; } + + public bool Is64Bits { get; private set; } + public bool Addr39Bits { get; private set; } + public bool IsService { get; private set; } + + public byte[] Text { get; private set; } + public byte[] RO { get; private set; } + public byte[] Data { get; private set; } + + public int TextOffset { get; private set; } + public int ROOffset { get; private set; } + public int DataOffset { get; private set; } + public int BssOffset { get; private set; } + public int BssSize { get; private set; } + + public int MainThreadStackSize { get; private set; } + + public int[] Capabilities { get; private set; } + + private struct SegmentHeader + { + public int Offset { get; private set; } + public int DecompressedSize { get; private set; } + public int CompressedSize { get; private set; } + public int Attribute { get; private set; } + + public SegmentHeader( + int Offset, + int DecompressedSize, + int CompressedSize, + int Attribute) + { + this.Offset = Offset; + this.DecompressedSize = DecompressedSize; + this.CompressedSize = CompressedSize; + this.Attribute = Attribute; + } + } + + public KernelInitialProcess(Stream Input) + { + BinaryReader Reader = new BinaryReader(Input); + + string Magic = ReadString(Reader, 4); + + if (Magic != "KIP1") + { + + } + + Name = ReadString(Reader, 12); + + TitleId = Reader.ReadInt64(); + + ProcessCategory = Reader.ReadInt32(); + + MainThreadPriority = Reader.ReadByte(); + DefaultProcessorId = Reader.ReadByte(); + + byte Reserved = Reader.ReadByte(); + byte Flags = Reader.ReadByte(); + + Is64Bits = (Flags & 0x08) != 0; + Addr39Bits = (Flags & 0x10) != 0; + IsService = (Flags & 0x20) != 0; + + SegmentHeader[] Segments = new SegmentHeader[6]; + + for (int Index = 0; Index < Segments.Length; Index++) + { + Segments[Index] = new SegmentHeader( + Reader.ReadInt32(), + Reader.ReadInt32(), + Reader.ReadInt32(), + Reader.ReadInt32()); + } + + TextOffset = Segments[0].Offset; + ROOffset = Segments[1].Offset; + DataOffset = Segments[2].Offset; + BssOffset = Segments[3].Offset; + BssSize = Segments[3].DecompressedSize; + + MainThreadStackSize = Segments[1].Attribute; + + Capabilities = new int[8]; + + for (int Index = 0; Index < Capabilities.Length; Index++) + { + Capabilities[Index] = Reader.ReadInt32(); + } + + Input.Seek(0x100, SeekOrigin.Begin); + + Text = ReadSegment(Segments[0], Input); + RO = ReadSegment(Segments[1], Input); + Data = ReadSegment(Segments[2], Input); + } + + private byte[] ReadSegment(SegmentHeader Header, Stream Input) + { + long End = Input.Position + Header.CompressedSize; + + Input.Seek(End, SeekOrigin.Begin); + + byte[] Data = BackwardsLz.Decompress(Input, Header.DecompressedSize); + + Input.Seek(End, SeekOrigin.Begin); + + return Data; + } + + private static string ReadString(BinaryReader Reader, int MaxSize) + { + string Value = string.Empty; + + for (int Index = 0; Index < MaxSize; Index++) + { + char Chr = (char)Reader.ReadByte(); + + if (Chr == '\0') + { + Reader.BaseStream.Seek(MaxSize - Index - 1, SeekOrigin.Current); + + break; + } + + Value += Chr; + } + + return Value; + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/Nro.cs b/Ryujinx.HLE/Loaders/Executables/Nro.cs deleted file mode 100644 index 6015da21..00000000 --- a/Ryujinx.HLE/Loaders/Executables/Nro.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.IO; - -namespace Ryujinx.HLE.Loaders.Executables -{ - class Nro : IExecutable - { - public string FilePath { get; private set; } - - public byte[] Text { get; private set; } - public byte[] RO { get; private set; } - public byte[] Data { get; private set; } - - public int Mod0Offset { get; private set; } - public int TextOffset { get; private set; } - public int ROOffset { get; private set; } - public int DataOffset { get; private set; } - public int BssSize { get; private set; } - - public long SourceAddress { get; private set; } - public long BssAddress { get; private set; } - - public Nro(Stream Input, string FilePath, long SourceAddress = 0, long BssAddress = 0) - { - this.FilePath = FilePath; - this.SourceAddress = SourceAddress; - this.BssAddress = BssAddress; - - BinaryReader Reader = new BinaryReader(Input); - - Input.Seek(4, SeekOrigin.Begin); - - int Mod0Offset = Reader.ReadInt32(); - int Padding8 = Reader.ReadInt32(); - int Paddingc = Reader.ReadInt32(); - int NroMagic = Reader.ReadInt32(); - int Unknown14 = Reader.ReadInt32(); - int FileSize = Reader.ReadInt32(); - int Unknown1c = Reader.ReadInt32(); - int TextOffset = Reader.ReadInt32(); - int TextSize = Reader.ReadInt32(); - int ROOffset = Reader.ReadInt32(); - int ROSize = Reader.ReadInt32(); - int DataOffset = Reader.ReadInt32(); - int DataSize = Reader.ReadInt32(); - int BssSize = Reader.ReadInt32(); - - this.Mod0Offset = Mod0Offset; - this.TextOffset = TextOffset; - this.ROOffset = ROOffset; - this.DataOffset = DataOffset; - this.BssSize = BssSize; - - byte[] Read(long Position, int Size) - { - Input.Seek(Position, SeekOrigin.Begin); - - return Reader.ReadBytes(Size); - } - - Text = Read(TextOffset, TextSize); - RO = Read(ROOffset, ROSize); - Data = Read(DataOffset, DataSize); - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/Nso.cs b/Ryujinx.HLE/Loaders/Executables/Nso.cs deleted file mode 100644 index c7b48a5f..00000000 --- a/Ryujinx.HLE/Loaders/Executables/Nso.cs +++ /dev/null @@ -1,127 +0,0 @@ -using Ryujinx.HLE.Loaders.Compression; -using System; -using System.IO; - -namespace Ryujinx.HLE.Loaders.Executables -{ - class Nso : IExecutable - { - public string FilePath { get; private set; } - - public byte[] Text { get; private set; } - public byte[] RO { get; private set; } - public byte[] Data { get; private set; } - - public int Mod0Offset { get; private set; } - public int TextOffset { get; private set; } - public int ROOffset { get; private set; } - public int DataOffset { get; private set; } - public int BssSize { get; private set; } - - public long SourceAddress { get; private set; } - public long BssAddress { get; private set; } - - [Flags] - private enum NsoFlags - { - IsTextCompressed = 1 << 0, - IsROCompressed = 1 << 1, - IsDataCompressed = 1 << 2, - HasTextHash = 1 << 3, - HasROHash = 1 << 4, - HasDataHash = 1 << 5 - } - - public Nso(Stream Input, string FilePath) - { - this.FilePath = FilePath; - - SourceAddress = 0; - BssAddress = 0; - - BinaryReader Reader = new BinaryReader(Input); - - Input.Seek(0, SeekOrigin.Begin); - - int NsoMagic = Reader.ReadInt32(); - int Version = Reader.ReadInt32(); - int Reserved = Reader.ReadInt32(); - int FlagsMsk = Reader.ReadInt32(); - int TextOffset = Reader.ReadInt32(); - int TextMemOffset = Reader.ReadInt32(); - int TextDecSize = Reader.ReadInt32(); - int ModNameOffset = Reader.ReadInt32(); - int ROOffset = Reader.ReadInt32(); - int ROMemOffset = Reader.ReadInt32(); - int RODecSize = Reader.ReadInt32(); - int ModNameSize = Reader.ReadInt32(); - int DataOffset = Reader.ReadInt32(); - int DataMemOffset = Reader.ReadInt32(); - int DataDecSize = Reader.ReadInt32(); - int BssSize = Reader.ReadInt32(); - - byte[] BuildId = Reader.ReadBytes(0x20); - - int TextSize = Reader.ReadInt32(); - int ROSize = Reader.ReadInt32(); - int DataSize = Reader.ReadInt32(); - - Input.Seek(0x24, SeekOrigin.Current); - - int DynStrOffset = Reader.ReadInt32(); - int DynStrSize = Reader.ReadInt32(); - int DynSymOffset = Reader.ReadInt32(); - int DynSymSize = Reader.ReadInt32(); - - byte[] TextHash = Reader.ReadBytes(0x20); - byte[] ROHash = Reader.ReadBytes(0x20); - byte[] DataHash = Reader.ReadBytes(0x20); - - NsoFlags Flags = (NsoFlags)FlagsMsk; - - this.TextOffset = TextMemOffset; - this.ROOffset = ROMemOffset; - this.DataOffset = DataMemOffset; - this.BssSize = BssSize; - - //Text segment - Input.Seek(TextOffset, SeekOrigin.Begin); - - Text = Reader.ReadBytes(TextSize); - - if (Flags.HasFlag(NsoFlags.IsTextCompressed) || true) - { - Text = Lz4.Decompress(Text, TextDecSize); - } - - //Read-only data segment - Input.Seek(ROOffset, SeekOrigin.Begin); - - RO = Reader.ReadBytes(ROSize); - - if (Flags.HasFlag(NsoFlags.IsROCompressed) || true) - { - RO = Lz4.Decompress(RO, RODecSize); - } - - //Data segment - Input.Seek(DataOffset, SeekOrigin.Begin); - - Data = Reader.ReadBytes(DataSize); - - if (Flags.HasFlag(NsoFlags.IsDataCompressed) || true) - { - Data = Lz4.Decompress(Data, DataDecSize); - } - - using (MemoryStream TextMS = new MemoryStream(Text)) - { - BinaryReader TextReader = new BinaryReader(TextMS); - - TextMS.Seek(4, SeekOrigin.Begin); - - Mod0Offset = TextReader.ReadInt32(); - } - } - } -} \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/NxRelocatableObject.cs b/Ryujinx.HLE/Loaders/Executables/NxRelocatableObject.cs new file mode 100644 index 00000000..20de5b5d --- /dev/null +++ b/Ryujinx.HLE/Loaders/Executables/NxRelocatableObject.cs @@ -0,0 +1,64 @@ +using System.IO; + +namespace Ryujinx.HLE.Loaders.Executables +{ + class NxRelocatableObject : IExecutable + { + public byte[] Text { get; private set; } + public byte[] RO { get; private set; } + public byte[] Data { get; private set; } + + public int Mod0Offset { get; private set; } + public int TextOffset { get; private set; } + public int ROOffset { get; private set; } + public int DataOffset { get; private set; } + public int BssSize { get; private set; } + + public int BssOffset => DataOffset + Data.Length; + + public ulong SourceAddress { get; private set; } + public ulong BssAddress { get; private set; } + + public NxRelocatableObject(Stream Input, ulong SourceAddress = 0, ulong BssAddress = 0) + { + this.SourceAddress = SourceAddress; + this.BssAddress = BssAddress; + + BinaryReader Reader = new BinaryReader(Input); + + Input.Seek(4, SeekOrigin.Begin); + + int Mod0Offset = Reader.ReadInt32(); + int Padding8 = Reader.ReadInt32(); + int Paddingc = Reader.ReadInt32(); + int NroMagic = Reader.ReadInt32(); + int Unknown14 = Reader.ReadInt32(); + int FileSize = Reader.ReadInt32(); + int Unknown1c = Reader.ReadInt32(); + int TextOffset = Reader.ReadInt32(); + int TextSize = Reader.ReadInt32(); + int ROOffset = Reader.ReadInt32(); + int ROSize = Reader.ReadInt32(); + int DataOffset = Reader.ReadInt32(); + int DataSize = Reader.ReadInt32(); + int BssSize = Reader.ReadInt32(); + + this.Mod0Offset = Mod0Offset; + this.TextOffset = TextOffset; + this.ROOffset = ROOffset; + this.DataOffset = DataOffset; + this.BssSize = BssSize; + + byte[] Read(long Position, int Size) + { + Input.Seek(Position, SeekOrigin.Begin); + + return Reader.ReadBytes(Size); + } + + Text = Read(TextOffset, TextSize); + RO = Read(ROOffset, ROSize); + Data = Read(DataOffset, DataSize); + } + } +} \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs b/Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs new file mode 100644 index 00000000..9fecb650 --- /dev/null +++ b/Ryujinx.HLE/Loaders/Executables/NxStaticObject.cs @@ -0,0 +1,109 @@ +using Ryujinx.HLE.Loaders.Compression; +using System; +using System.IO; + +namespace Ryujinx.HLE.Loaders.Executables +{ + class NxStaticObject : IExecutable + { + public byte[] Text { get; private set; } + public byte[] RO { get; private set; } + public byte[] Data { get; private set; } + + public int TextOffset { get; private set; } + public int ROOffset { get; private set; } + public int DataOffset { get; private set; } + public int BssSize { get; private set; } + + public int BssOffset => DataOffset + Data.Length; + + [Flags] + private enum NsoFlags + { + IsTextCompressed = 1 << 0, + IsROCompressed = 1 << 1, + IsDataCompressed = 1 << 2, + HasTextHash = 1 << 3, + HasROHash = 1 << 4, + HasDataHash = 1 << 5 + } + + public NxStaticObject(Stream Input) + { + BinaryReader Reader = new BinaryReader(Input); + + Input.Seek(0, SeekOrigin.Begin); + + int NsoMagic = Reader.ReadInt32(); + int Version = Reader.ReadInt32(); + int Reserved = Reader.ReadInt32(); + int FlagsMsk = Reader.ReadInt32(); + int TextOffset = Reader.ReadInt32(); + int TextMemOffset = Reader.ReadInt32(); + int TextDecSize = Reader.ReadInt32(); + int ModNameOffset = Reader.ReadInt32(); + int ROOffset = Reader.ReadInt32(); + int ROMemOffset = Reader.ReadInt32(); + int RODecSize = Reader.ReadInt32(); + int ModNameSize = Reader.ReadInt32(); + int DataOffset = Reader.ReadInt32(); + int DataMemOffset = Reader.ReadInt32(); + int DataDecSize = Reader.ReadInt32(); + int BssSize = Reader.ReadInt32(); + + byte[] BuildId = Reader.ReadBytes(0x20); + + int TextSize = Reader.ReadInt32(); + int ROSize = Reader.ReadInt32(); + int DataSize = Reader.ReadInt32(); + + Input.Seek(0x24, SeekOrigin.Current); + + int DynStrOffset = Reader.ReadInt32(); + int DynStrSize = Reader.ReadInt32(); + int DynSymOffset = Reader.ReadInt32(); + int DynSymSize = Reader.ReadInt32(); + + byte[] TextHash = Reader.ReadBytes(0x20); + byte[] ROHash = Reader.ReadBytes(0x20); + byte[] DataHash = Reader.ReadBytes(0x20); + + NsoFlags Flags = (NsoFlags)FlagsMsk; + + this.TextOffset = TextMemOffset; + this.ROOffset = ROMemOffset; + this.DataOffset = DataMemOffset; + this.BssSize = BssSize; + + //Text segment + Input.Seek(TextOffset, SeekOrigin.Begin); + + Text = Reader.ReadBytes(TextSize); + + if (Flags.HasFlag(NsoFlags.IsTextCompressed)) + { + Text = Lz4.Decompress(Text, TextDecSize); + } + + //Read-only data segment + Input.Seek(ROOffset, SeekOrigin.Begin); + + RO = Reader.ReadBytes(ROSize); + + if (Flags.HasFlag(NsoFlags.IsROCompressed)) + { + RO = Lz4.Decompress(RO, RODecSize); + } + + //Data segment + Input.Seek(DataOffset, SeekOrigin.Begin); + + Data = Reader.ReadBytes(DataSize); + + if (Flags.HasFlag(NsoFlags.IsDataCompressed)) + { + Data = Lz4.Decompress(Data, DataDecSize); + } + } + } +} \ No newline at end of file -- cgit v1.2.3