Skip to content

Commit

Permalink
Merge branch 'master'.
Browse files Browse the repository at this point in the history
  • Loading branch information
jp2masa committed May 22, 2017
2 parents 2139371 + f3a10fb commit 9c90f7e
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 167 deletions.
Binary file added Build/HyperV/Filesystem.vhdx
Binary file not shown.
2 changes: 2 additions & 0 deletions Setup/Cosmos.iss
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Source: ".\Artwork\Cosmos.ico"; DestDir: "{app}"; Flags: ignoreversion uninsremo
Source: ".\Artwork\XSharp\XSharp.ico"; DestDir: "{app}\XSharp\"; Flags: ignoreversion uninsremovereadonly
Source: ".\source\Cosmos.Debug.DebugStub\*.xs"; DestDir: "{app}\XSharp\DebugStub\"; Flags: ignoreversion uninsremovereadonly
; VMware
Source: ".\Build\HyperV\*"; DestDir: "{app}\Build\HyperV"; Flags: ignoreversion uninsremovereadonly overwritereadonly recursesubdirs
; VMware
Source: ".\Build\VMware\*"; DestDir: "{app}\Build\VMware"; Flags: ignoreversion uninsremovereadonly overwritereadonly recursesubdirs
; ISO
Source: ".\Build\ISO\*"; DestDir: "{app}\Build\ISO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@ public static void Execute()
{
{"echo", "ECHO"},
{"reboot", "REBOOT" },
{"shutdown", "SHUTDOWN"}
{"shutdown", "SHUTDOWN"},
{"integer", 500}
};

Assert.IsTrue(commands.ContainsKey("echo"), "Dictionary ContainsKey does not work");
Assert.IsTrue(commands.ContainsKey("echo"), "Dictionary ContainsKey does not work1");
Assert.IsFalse(commands.ContainsKey("musterror"), "Dictionary ContainsKey does not work 2");



//String test
Assert.IsTrue((string)commands["echo"] == "ECHO", "Dictionary string not work");
commands["echo"] = "notEcho";
Assert.IsTrue((string)commands["echo"] == "notEcho", "Dictionary string been reset not working");


//Integer test
Assert.IsTrue((int)commands["integer"] == 500, "Dictionary integer not working");
commands["integer"] = 321;
Assert.IsTrue((int)commands["integer"] == 321, "Dictionary integer been reset not working");


}
}
}
1 change: 1 addition & 0 deletions Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void Apply(Engine engine)
// If you want to test only specific platforms, add them to the list, like next line. By default, all platforms are run.
engine.RunTargets.Add(RunTargetEnum.Bochs);
//engine.RunTargets.Add(RunTargetEnum.VMware);
//engine.RunTargets.Add(RunTargetEnum.HyperV);

// If you're working on the compiler (or other lower parts), you can choose to run the compiler in process
// one thing to keep in mind though, is that this only works with 1 kernel at a time!
Expand Down
36 changes: 36 additions & 0 deletions Tests/Cosmos.TestRunner.Core/Engine.HyperV.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.IO;

using Cosmos.Build.Common;
using Cosmos.Debug.DebugConnectors;
using Cosmos.Debug.Hosts;

namespace Cosmos.TestRunner.Core
{
partial class Engine
{
private void RunIsoInHyperV(string iso, string harddisk)
{
if (!File.Exists(harddisk))
{
throw new FileNotFoundException("Harddisk file not found!", harddisk);
}

var xParams = new Dictionary<string, string>();

xParams.Add("ISOFile", iso);
xParams.Add(BuildPropertyNames.VisualStudioDebugPortString, "Pipe: CosmosSerial");

var xDebugConnector = new DebugConnectorPipeClient(DebugConnectorPipeClient.DefaultCosmosPipeName);
InitializeDebugConnector(xDebugConnector);

var xHyperV = new HyperV(xParams, RunWithGDB, harddisk);
xHyperV.OnShutDown = (a, b) =>
{
mKernelRunning = false;
};

HandleRunning(xDebugConnector, xHyperV);
}
}
}
21 changes: 18 additions & 3 deletions Tests/Cosmos.TestRunner.Core/Engine.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ private bool ExecuteKernel(string assemblyFileName, RunConfiguration configurati
RunTask("Ld", () => RunLd(xTempObjectFile, xObjectFile));
RunTask("ExtractMapFromElfFile", () => RunExtractMapFromElfFile(mBaseWorkingDirectory, xObjectFile));
}
var xHarddiskPath = Path.Combine(mBaseWorkingDirectory, "Harddisk.vmdk");
var xOriginalHarddiskPath = Path.Combine(GetCosmosUserkitFolder(), "Build", "VMware", "Workstation", "Filesystem.vmdk");
File.Copy(xOriginalHarddiskPath, xHarddiskPath);

string xHarddiskPath;
if (configuration.RunTarget == RunTargetEnum.HyperV)
{
xHarddiskPath = Path.Combine(mBaseWorkingDirectory, "Harddisk.vhdx");
var xOriginalHarddiskPath = Path.Combine(GetCosmosUserkitFolder(), "Build", "HyperV", "Filesystem.vhdx");
File.Copy(xOriginalHarddiskPath, xHarddiskPath);
}
else
{
xHarddiskPath = Path.Combine(mBaseWorkingDirectory, "Harddisk.vmdk");
var xOriginalHarddiskPath = Path.Combine(GetCosmosUserkitFolder(), "Build", "VMware", "Workstation", "Filesystem.vmdk");
File.Copy(xOriginalHarddiskPath, xHarddiskPath);
}

RunTask("MakeISO", () => MakeIso(xObjectFile, xIsoFile));
switch (configuration.RunTarget)
{
Expand All @@ -41,6 +53,9 @@ private bool ExecuteKernel(string assemblyFileName, RunConfiguration configurati
case RunTargetEnum.VMware:
RunTask("RunISO", () => RunIsoInVMware(xIsoFile, xHarddiskPath));
break;
case RunTargetEnum.HyperV:
RunTask("RunISO", () => RunIsoInHyperV(xIsoFile, xHarddiskPath));
break;
default:
throw new ArgumentOutOfRangeException("RunTarget " + configuration.RunTarget + " not implemented!");
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/Cosmos.TestRunner.Core/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum RunTargetEnum
{
Bochs,
VMware
VMware,
HyperV
}
}
11 changes: 9 additions & 2 deletions source/Cosmos.Build.Common/BuildProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ public void LoadProfile(string aName)
Description = "Makes a USB device such as a flash drive or external hard disk bootable.";
Deployment = DeploymentType.USB;
Launch = LaunchType.None;

}
else if (aName == "VMware")
{
Description = "Use VMware Player or Workstation to deploy and debug.";
Deployment = DeploymentType.ISO;
Launch = LaunchType.VMware;

VisualStudioDebugPort = @"Pipe: Cosmos\Serial";
}
else if (aName == "PXE")
{
Expand All @@ -126,13 +125,21 @@ public void LoadProfile(string aName)
Description = "Use Bochs emulator to deploy and debug.";
Deployment = DeploymentType.ISO;
Launch = LaunchType.Bochs;
VisualStudioDebugPort = @"Pipe: Cosmos\Serial";
}
else if (aName == "IntelEdison")
{
Description = "Connect to Intel Edison device to deploy and debug.";
Deployment = DeploymentType.BinaryImage;
Launch = LaunchType.IntelEdison;
}
else if (aName == "HyperV")
{
Description = "Use Hyper-V to deploy and debug.";
Deployment = DeploymentType.ISO;
Launch = LaunchType.HyperV;
VisualStudioDebugPort = "Pipe: CosmosSerial";
}
}

public void DeleteProfile(string aPrefix)
Expand Down
4 changes: 3 additions & 1 deletion source/Cosmos.Build.Common/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace Cosmos.Build.Common
{
Expand Down Expand Up @@ -31,6 +31,8 @@ public enum LaunchType
Bochs,
[Description("Intel Edison")]
IntelEdison,
[Description("Hyper-V")]
HyperV
}

public enum VMwareEdition
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.Debug.Common/Cosmos.Debug.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
</ItemGroup>

</Project>
</Project>
74 changes: 74 additions & 0 deletions source/Cosmos.Debug.DebugConnectors/DebugConnectorPipeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Threading;

namespace Cosmos.Debug.DebugConnectors
{
/// <summary>Use a named pipe client to implement wire transfer protocol between a Debug Stub
/// hosted in a debugged Cosmos Kernel and our Debug Engine hosted in Visual Studio.
/// Hyper-V provides a pipe server to expose guest serial ports.</summary>
public class DebugConnectorPipeClient : DebugConnectorStreamWithoutTimeouts
{
// private AutoResetEvent mWaitConnectEvent = new AutoResetEvent(false);
private NamedPipeClientStream mPipe;

public const string DefaultCosmosPipeName = "CosmosSerial";

public DebugConnectorPipeClient(string aName)
{
mPipe = new NamedPipeClientStream(".", aName, PipeDirection.InOut, PipeOptions.WriteThrough);
Start();
}

protected override int TryRead(byte[] buffer, int offset, int count, int timeout)
{
var xStream = mStream;
if (xStream == null)
{
return 0;
}
uint xBytesAvailable = 0;
if (PeekNamedPipe(mPipe.SafePipeHandle.DangerousGetHandle(), null, 0, IntPtr.Zero, ref xBytesAvailable, IntPtr.Zero))
{
if (xBytesAvailable > 0)
{
return xStream.Read(buffer, offset, count);
}
}
Thread.Sleep(timeout);
if (PeekNamedPipe(mPipe.SafePipeHandle.DangerousGetHandle(), null, 0, IntPtr.Zero, ref xBytesAvailable, IntPtr.Zero))
{
if (xBytesAvailable > 0)
{
return xStream.Read(buffer, offset, count);
}
}
return 0;
}

public override bool IsConnected
{
get
{
return base.IsConnected && mPipe.IsConnected;
}
}

protected override void InitializeBackground()
{
mPipe.Connect();
mStream = mPipe;
}

protected override bool GetIsConnectedToDebugStub()
{
return mPipe.IsConnected;
}

[DllImport("kernel32.dll", EntryPoint = "PeekNamedPipe", SetLastError = true)]
private static extern bool PeekNamedPipe(IntPtr handle,
byte[] buffer, uint nBufferSize, IntPtr bytesRead,
ref uint bytesAvail, IntPtr BytesLeftThisMessage);
}
}
109 changes: 109 additions & 0 deletions source/Cosmos.Debug.Hosts/HyperV.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
#if false
using System.Management.Automation;
using System.Management.Automation.Runspaces;
#endif
using System.Security.Principal;

using Cosmos.Build.Common;

namespace Cosmos.Debug.Hosts
{
public class HyperV : Host
{
protected string mHarddiskPath;
protected Process mProcess;

#if false
private static bool IsProcessAdministrator => (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);
#else
private static bool IsProcessAdministrator => true;
#endif

public HyperV(Dictionary<string, string> aParams, bool aUseGDB, string harddisk = "Filesystem.vhdx") : base(aParams, aUseGDB)
{
if (!IsProcessAdministrator)
{
throw new Exception("Visual Studio must be run as administrator for Hyper-V to work");
}

mHarddiskPath = Path.Combine(CosmosPaths.Build, harddisk);
}

public override void Start()
{
CreateVirtualMachine();

// Target exe or file
var info = new ProcessStartInfo(@"C:\Windows\sysnative\VmConnect.exe", @"""localhost"" ""Cosmos""")
{
UseShellExecute = true
};

mProcess = new Process();
mProcess.StartInfo = info;
mProcess.EnableRaisingEvents = true;
mProcess.Exited += (Object aSender, EventArgs e) =>
{
OnShutDown?.Invoke(aSender, e);
};
mProcess.Start();

RunPowershellScript("Start-VM -Name Cosmos");
}

public override void Stop()
{
RunPowershellScript("Stop-VM -Name Cosmos -TurnOff -ErrorAction Ignore");
mProcess.Kill();
}

protected void CreateVirtualMachine()
{
RunPowershellScript("Stop-VM -Name Cosmos -TurnOff -ErrorAction Ignore");

RunPowershellScript("Remove-VM -Name Cosmos -Force -ErrorAction Ignore");
RunPowershellScript("New-VM -Name Cosmos -MemoryStartupBytes 268435456 -BootDevice CD");
if (!File.Exists(mHarddiskPath))
{
RunPowershellScript($@"New-VHD -SizeBytes 268435456 -Dynamic -Path ""{mHarddiskPath}""");
}
RunPowershellScript($@"Add-VMHardDiskDrive -VMName Cosmos -ControllerNumber 0 -ControllerLocation 0 -Path ""{mHarddiskPath}""");
RunPowershellScript($@"Set-VMDvdDrive -VMName Cosmos -ControllerNumber 1 -ControllerLocation 0 -Path ""{mParams["ISOFile"]}""");
RunPowershellScript(@"Set-VMComPort -VMName Cosmos -Path \\.\pipe\CosmosSerial -Number 1");
}

private static void RunPowershellScript(string text)
{
// Workaround
ProcessStartInfo xStartInfo = new ProcessStartInfo("powershell");
xStartInfo.Arguments = text;

var xProcess = Process.Start(xStartInfo);
xProcess.WaitForExit();

#if false
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

pipeline.Commands.AddScript(text);
pipeline.Commands.Add("Out-String");

Collection<PSObject> results = pipeline.Invoke();
foreach (PSObject obj in results)
{
System.Diagnostics.Debug.WriteLine(obj.ToString());
}
}
#endif
}
}
}
Loading

0 comments on commit 9c90f7e

Please sign in to comment.