Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
czhower committed Aug 9, 2017
2 parents d0a3dad + 8308512 commit 0ccfdde
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 20 deletions.
21 changes: 21 additions & 0 deletions Cosmos/Tests/Cosmos.TestRunner.Core/Cosmos.TestRunner.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
<AssemblyOriginatorKeyFile>..\..\Cosmos.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<Compile Update="Engine.Bochs.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
<Compile Update="Engine.Helpers.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
<Compile Update="Engine.HyperV.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
<Compile Update="Engine.Run.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
<Compile Update="Engine.Running.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
<Compile Update="Engine.VMware.cs">
<DependentUpon>Engine.cs</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void Apply(Engine engine)
engine.DebugIL2CPU = false;
engine.TraceAssembliesLevel = TraceAssemblies.User;
//engine.EnableStackCorruptionChecks = false;
engine.KernelPkg = "X86G3";
engine.KernelPkg = "X86";

engine.EnableStackCorruptionChecks = true;
engine.StackCorruptionChecksLevel = StackCorruptionDetectionLevel.AllInstructions;
Expand Down
28 changes: 26 additions & 2 deletions Cosmos/Tests/Cosmos.TestRunner.Core/Engine.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ private void RunExtractMapFromElfFile(string workingDir, string kernelFileName)
RunObjDump(CosmosPaths.Build, workingDir, kernelFileName, OutputHandler.LogError, OutputHandler.LogMessage);
}

private void RunTheRingMaster(string kernelFileName)
{
var xArgs = new List<string>() { kernelFileName };

bool xUsingUserKit = false;
string xTheRingMasterPath = Path.Combine(FindCosmosRoot(), "source", "TheRingMaster");
if (!Directory.Exists(xTheRingMasterPath))
{
xUsingUserKit = true;
xTheRingMasterPath = Path.Combine(GetCosmosUserkitFolder(), "Build", "TheRingMaster");
}

if (xUsingUserKit)
{
RunProcess("TheRingMaster.exe", xTheRingMasterPath, xArgs);
}
else
{
xArgs.Insert(0, "run");
xArgs.Insert(1, "--no-build");
RunProcess("dotnet", xTheRingMasterPath, xArgs);
}
}

private void RunIL2CPU(string kernelFileName, string outputFile)
{
References = new List<string>() { kernelFileName };
Expand Down Expand Up @@ -227,8 +251,8 @@ private void RunIL2CPU(string kernelFileName, string outputFile)
else
{
xArgs.Insert(0, "run");
xArgs.Insert(3, "--no-build");
xArgs.Insert(4, " -- ");
xArgs.Insert(1, "--no-build");
xArgs.Insert(2, " -- ");
RunProcess("dotnet", xIL2CPUPath, xArgs, true);
}
}
Expand Down
4 changes: 4 additions & 0 deletions Cosmos/Tests/Cosmos.TestRunner.Core/Engine.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ private bool ExecuteKernel(string assemblyFileName, RunConfiguration configurati
var xTempObjectFile = Path.Combine(mBaseWorkingDirectory, "Kernel.o");
var xIsoFile = Path.Combine(mBaseWorkingDirectory, "Kernel.iso");

if (KernelPkg == "X86")
{
RunTask("TheRingMaster", () => RunTheRingMaster(assemblyFileName));
}
RunTask("IL2CPU", () => RunIL2CPU(assemblyFileName, xAssemblyFile));
RunTask("Nasm", () => RunNasm(xAssemblyFile, xObjectFile, configuration.IsELF));
if (configuration.IsELF)
Expand Down
1 change: 1 addition & 0 deletions Cosmos/Tests/Cosmos.TestRunner/Cosmos.TestRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\..\source\IL2CPU\IL2CPU.csproj" />
<ProjectReference Include="..\..\source\TheRingMaster\TheRingMaster.csproj" />
<ProjectReference Include="..\..\Tools\NASM\NASM.csproj" />
<ProjectReference Include="..\Cosmos.TestRunner.Core\Cosmos.TestRunner.Core.csproj" />
</ItemGroup>
Expand Down
19 changes: 13 additions & 6 deletions Cosmos/source/Cosmos.Build.MSBuild/Cosmos.targets
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<CopyBuildOutputToOutputDirectory>False</CopyBuildOutputToOutputDirectory>
<DisableImplicitFrameworkReferences>True</DisableImplicitFrameworkReferences>
<ImplicitlyExpandDesignTimeFacades>False</ImplicitlyExpandDesignTimeFacades>
<NoStdLib>true</NoStdLib>
</PropertyGroup>

<PropertyGroup>
<CosmosDir Condition="$(CosmosDir) == ''">$(AppData)\Cosmos User Kit</CosmosDir>
<BuildToolsDir Condition="$(BuildToolsDir) == ''">$(CosmosDir)\Build\Tools</BuildToolsDir>
Expand Down Expand Up @@ -44,7 +44,7 @@
<UsingTask TaskName="Ld" AssemblyFile="$(CosmosBuildTaskAssemblyFile)"/>
<UsingTask TaskName="ReadNAsmMapToDebugInfo" AssemblyFile="$(CosmosBuildTaskAssemblyFile)"/>
<UsingTask TaskName="ExtractMapFromElfFile" AssemblyFile="$(CosmosBuildTaskAssemblyFile)"/>

<Target Name="CoreCompile">
<Error Text="The Release configuration isn't currently supported!" Condition="$(Configuration) == 'Release'" />
<Error Text="File %(Compile.Identity) is set to compile, but .Cosmos projects don't compile any source themselves!" Condition="$(Compile) != ''"/>
Expand All @@ -54,8 +54,15 @@
<CreateProperty Value="false" Condition="$(BinFormat) == 'bin'">
<Output PropertyName="IsELF" TaskParameter="Value"/>
</CreateProperty>

<!--
<TheRingMaster AssemblyPath="$(CosmosKernelAssembly)" WorkingDir="$(TargetDir)" />
<CreateProperty Value="%(ReferencePath.Identity)" Condition="%(ReferencePath)">
<Output PropertyName="CosmosKernelAssembly" TaskParameter="Value"/>
</CreateProperty>
-->

<!--
<TheRingMaster AssemblyPath="$(CosmosKernelAssembly)" WorkingDir="$(TargetDir)" Condition="$(KernelPkg) == 'X86'" />
-->
<IL2CPU KernelPkg="$(KernelPkg)" DebugMode="$(DebugMode)" DebugEnabled="$(DebugEnabled)" StackCorruptionDetectionEnabled="$(StackCorruptionDetectionEnabled)" StackCorruptionDetectionLevel="$(StackCorruptionDetectionLevel)" TraceAssemblies="$(TraceAssemblies)" IgnoreDebugStubAttribute="$(IgnoreDebugStubAttribute)" DebugCom="1" References="@(ReferencePath)" AssemblySearchDirs="$(AssemblySearchPaths)" OutputFilename="$(TargetDir)$(MSBuildProjectName).asm" EnableLogging="true" EmitDebugSymbols="$(DebugSymbols)" CosmosBuildDir="$(CosmosDir)\Build" WorkingDir="$(TargetDir)"/>
<NAsm InputFile="$(TargetDir)$(MSBuildProjectName).asm" OutputFile="$(TargetDir)$(MSBuildProjectName).obj" IsELF="$(IsELF)" ExePath="$(NasmFile)"/>
Expand All @@ -75,7 +82,7 @@
<!-- <MakeISO InputFile="$(TargetDir)$(MSBuildProjectName).bin" OutputFile="$(TargetDir)$(MSBuildProjectName).iso" CosmosBuildDir="$(CosmosDir)\Build" /> -->
<Delete Files="$(TargetDir)$(MSBuildProjectName).iso"/>
</Target>

<Target Name="CreateManifestResourceNames" />

</Project>
17 changes: 14 additions & 3 deletions Cosmos/source/Cosmos.Build.MSBuild/TheRingMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Build.Framework;

using Cosmos.Build.Common;

namespace Cosmos.Build.MSBuild
{
public class RingCheck : BaseToolTask
public class TheRingMaster : BaseToolTask
{
[Required]
public string KernelAssemblyPath { get; set; }
Expand All @@ -21,7 +20,19 @@ public class RingCheck : BaseToolTask

public override bool Execute()
{
return ExecuteTool(WorkingDir, Path.Combine(CosmosPaths.Build, "RingCheck", "RingCheck.exe"), KernelAssemblyPath, "Ring Check");
var xSW = Stopwatch.StartNew();

try
{
Log.LogMessage(MessageImportance.High, $"Invoking TheRingMaster.exe {KernelAssemblyPath}");
return ExecuteTool(WorkingDir, Path.Combine(CosmosPaths.Build, "TheRingMaster", "TheRingMaster.exe"), KernelAssemblyPath, "The Ring Master");
}
finally
{
xSW.Stop();
Log.LogMessage(MessageImportance.High, $"TheRingMaster invoked with KernelAssemblyPath = '{KernelAssemblyPath}'");
Log.LogMessage(MessageImportance.High, "TheRingMaster task took {0}", xSW.Elapsed);
}
}
}
}
4 changes: 0 additions & 4 deletions Cosmos/source/Cosmos.System2/Cosmos.System2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@
<ProjectReference Include="..\Cosmos.IL2CPU.API\Cosmos.IL2CPU.API.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
182 changes: 178 additions & 4 deletions Cosmos/source/TheRingMaster/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,193 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

using Cosmos.Build.Common;

namespace TheRingMaster
{
class Program
public class Program
{
static void Main(string[] args)
static Dictionary<Assembly, string> RingCache = new Dictionary<Assembly, string>();
static string KernelDir;

public static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("ARGS:");

foreach (var xArg in args)
{
Console.WriteLine(xArg);
}

Console.WriteLine("Usage: theringmaster <path-to-kernel>");
return;
}

var xKernelAssemblyPath = args[1];
var xKernelAssemblyPath = args[0];

if (!File.Exists(xKernelAssemblyPath))
{
throw new FileNotFoundException("Kernel Assembly not found! Path: '" + xKernelAssemblyPath + "'");
}

KernelDir = Path.GetDirectoryName(xKernelAssemblyPath);
AssemblyLoadContext.Default.Resolving += Default_Resolving;

var xKernelAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(xKernelAssemblyPath);
CheckRings(xKernelAssembly, "Application");

void CheckRings(Assembly aAssembly, string aRing, string aSourceAssemblyName = null)
{
RingCache.TryGetValue(aAssembly, out var xRing);

if (xRing == null)
{
var xManifestName = aAssembly.GetManifestResourceNames()
.Where(n => n == aAssembly.GetName().Name + ".Cosmos.cfg")
.SingleOrDefault();

if (xManifestName != null)
{
Dictionary<string, string> xCfg;

using (var xManifestStream = aAssembly.GetManifestResourceStream(xManifestName))
{
xCfg = ParseCfg(xManifestStream);
}

if (xCfg == null)
{
throw new Exception("Invalid Cosmos configuration! Resource name: " + xManifestName);
}

xCfg.TryGetValue("Ring", out xRing);

if (!new string[] { "CPU", "Platform", "HAL", "System", "Application", /*"Plug",*/ "Debug" }.Contains(xRing))
{
throw new Exception("Unknown ring! Ring: " + xRing);
}
}

if (xRing == null)
{
xRing = "External";
}
}

RingCache.Add(aAssembly, xRing);

// Check Rings

bool xValid = false;

// Same rings
// OR
// External ring, can be referenced by any ring
// OR
// One of the assemblies is Debug
if (aRing == xRing || xRing == "External" || aRing == "Debug" || xRing == "Debug")
{
xValid = true;
}

if (!xValid)
{
switch (aRing)
{
case "Application" when xRing == "System":
case "System" when xRing == "HAL":
case "Platform" when xRing == "CPU":
case "Platform" when xRing == "HAL":
//case "Plug" when xRing == "System":
return;
}
}

if (!xValid)
{
var xExceptionMessage = "Invalid rings! Source assembly: " + (aSourceAssemblyName ?? "(no assembly)") +
": Ring " + aRing + "; Referenced assembly: " + aAssembly.GetName().Name +
": Ring " + xRing;

throw new Exception(xExceptionMessage);
}

foreach (var xReference in aAssembly.GetReferencedAssemblies())
{
try
{
CheckRings(AssemblyLoadContext.Default.LoadFromAssemblyName(xReference), xRing, aAssembly.GetName().Name);
}
catch (FileNotFoundException)
{
}
}
}
}

private static Assembly Default_Resolving(AssemblyLoadContext aContext, AssemblyName aAssemblyName)
{
Assembly xAssembly = null;

if (ResolveAssemblyForDir(KernelDir, out xAssembly))
{
return xAssembly;
}

if (ResolveAssemblyForDir(CosmosPaths.Kernel, out xAssembly))
{
return xAssembly;
}

return xAssembly;

bool ResolveAssemblyForDir(string aDir, out Assembly aAssembly)
{
aAssembly = null;

var xFiles = Directory.GetFiles(aDir, aAssemblyName.Name + ".*", SearchOption.TopDirectoryOnly);

if (xFiles.Any(f => Path.GetExtension(f) == ".dll"))
{
aAssembly = aContext.LoadFromAssemblyPath(xFiles.Where(f => Path.GetExtension(f) == ".dll").Single());
}

if (xFiles.Any(f => Path.GetExtension(f) == ".exe"))
{
aAssembly = aContext.LoadFromAssemblyPath(xFiles.Where(f => Path.GetExtension(f) == ".exe").Single());
}

return aAssembly != null;
}
}

static Dictionary<string, string> ParseCfg(Stream aStream)
{
var xCfg = new Dictionary<string, string>();

using (var xReader = new StreamReader(aStream))
{
while (xReader.Peek() >= 0)
{
var xLine = xReader.ReadLine();

if (!xLine.Contains(':') || xLine.Count(c => c == ':') > 1)
{
return null;
}

var xProperty = xLine.Split(':');
xCfg.Add(xProperty[0].Trim(), xProperty[1].Trim());
}
}

return xCfg;
}
}
}
Loading

0 comments on commit 0ccfdde

Please sign in to comment.