forked from CosmosOS/Cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
515 additions
and
167 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
public enum RunTargetEnum | ||
{ | ||
Bochs, | ||
VMware | ||
VMware, | ||
HyperV | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
<PackageReference Include="System.Threading.Thread" Version="4.3.0" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</Project> |
74 changes: 74 additions & 0 deletions
74
source/Cosmos.Debug.DebugConnectors/DebugConnectorPipeClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.