public static bool LoadKernelInitalProcess(Horizon system, KipExecutable 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.Header.Flags & 0x10) != 0 ? 0x8000000UL : 0x200000UL; ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset; int mmuFlags = 0; if (AslrEnabled) { // TODO: Randomization. mmuFlags |= 0x20; } if ((kip.Header.Flags & 0x10) != 0) { mmuFlags |= (int)AddressSpaceType.Addr39Bits << 1; } if ((kip.Header.Flags & 0x08) != 0) { mmuFlags |= 1; } ProcessCreationInfo creationInfo = new ProcessCreationInfo( kip.Header.Name, kip.Header.ProcessCategory, kip.Header.TitleId, codeAddress, codePagesCount, mmuFlags, 0, 0); MemoryRegion memoryRegion = (kip.Header.Flags & 0x20) != 0 ? 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.Header.DefaultCore; result = process.Start(kip.Header.MainThreadPriority, (ulong)kip.Header.Sections[1].Attribute); 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 LoadKip(KernelContext context, KipExecutable 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.Is64BitAddressSpace ? 0x8000000UL : 0x200000UL; ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset; int mmuFlags = 0; if (AslrEnabled) { // TODO: Randomization. mmuFlags |= 0x20; } if (kip.Is64BitAddressSpace) { mmuFlags |= (int)AddressSpaceType.Addr39Bits << 1; } if (kip.Is64Bit) { mmuFlags |= 1; } ProcessCreationInfo creationInfo = new ProcessCreationInfo( kip.Name, kip.Version, kip.ProgramId, codeAddress, codePagesCount, mmuFlags, 0, 0); MemoryRegion memoryRegion = kip.UsesSecureMemory ? MemoryRegion.Service : MemoryRegion.Application; KMemoryRegionManager region = context.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(context); result = process.InitializeKip( creationInfo, kip.Capabilities, pageList, context.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.IdealCoreId; result = process.Start(kip.Priority, (ulong)kip.StackSize); if (result != KernelResult.Success) { Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\"."); return(false); } context.Processes.TryAdd(process.Pid, process); return(true); }
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 MemRegion = Kip.IsService ? MemoryRegion.Service : MemoryRegion.Application; KMemoryRegionManager Region = System.MemoryRegions[(int)MemRegion]; 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, MemRegion); 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); } 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); }