Skip to content

Commit

Permalink
Switch to BareMetal Unit Test Engine + Fix for Hello World (#1129)
Browse files Browse the repository at this point in the history
* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - BareMetal UnitTestEngine

* - BareMetal UnitTestEngine

* - BareMetal UnitTestEngine

* - BareMetal UnitTestEngine

* - Stack Corruption Fix
  • Loading branch information
tgiphil committed Aug 27, 2023
1 parent edba799 commit 00034e2
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Demos/Run-Demo.TestWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher -autostart -o0 -output-asm -output-debug -output-hash Mosa.Demo.TestWorld.x86.dll
Mosa.Tool.Launcher -autostart -output-asm -output-debug -output-hash Mosa.Demo.TestWorld.x86.dll
3 changes: 3 additions & 0 deletions Source/Docs/command-line-arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Below are the command line arguments available:
-gdb,Emulator.GDB,true
-timeout,Emulator.MaxRuntime,{value}
-debug,Launcher.Serial,true
-debug,OS.BootOptions,bootoptions=serialdebug

Launcher - Emulator - Qemu & VMWare:
-vmware-svga,Emulator.SVGA,vmware
Expand Down Expand Up @@ -156,6 +157,8 @@ Below are the command line arguments available:

Launcher - Advance:
-plug-korlib,Launcher.PlugKorlib,true
-test,OS.BootOptions,bootoptions=serialdebug
-test,Launcher.Test,true

Operating System:
-osname,OS.Name,{value}
Expand Down
45 changes: 41 additions & 4 deletions Source/Mosa.BareMetal.HelloWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Kernel.BareMetal;
using Mosa.UnitTests.Basic;
using Mosa.UnitTests.Primitive;

namespace Mosa.BareMetal.HelloWorld.x86;
Expand All @@ -12,17 +13,53 @@ public static void Main()
Debug.WriteLine("Boot::Main()");
Debug.WriteLine("MOSA x86 Kernel");

var result = CheckedTests.AddU8U8(18446744073709551615, 1);
var result = ArrayLayoutTests.C_4();

if (result == 95272687)
if (result)
{
Debug.WriteLine("CheckedTests::AddU8U8() -> Pass");
Debug.WriteLine("ArrayLayoutTests.C_4() -> Pass");
}
else
{
Debug.WriteLine("CheckedTests::AddU8U8() -> Fail");
Debug.WriteLine("ArrayLayoutTests.C_4() -> Fail");
}

var result2 = ArrayLayoutTests.C_4a();

if (result2)
{
Debug.WriteLine("ArrayLayoutTests.C_4a() -> Pass");
}
else
{
Debug.WriteLine("ArrayLayoutTests.C_4a() -> Fail");
}

var result3 = ArrayLayoutTests.C_4z();

if (result3)
{
Debug.WriteLine("ArrayLayoutTests.C_4z() -> Pass");
}
else
{
Debug.WriteLine("ArrayLayoutTests.C_4z() -> Fail");
}

//var result4 = ArrayLayoutTests.C_4ab();
//Debug.WriteLine("ArrayLayoutTests.C_4ab() = ", result4);

//var result = CheckedTests.AddU8U8(18446744073709551615, 1);

//if (result == 95272687)
//{
// Debug.WriteLine("CheckedTests::AddU8U8() -> Pass");
//}
//else
//{
// Debug.WriteLine("CheckedTests::AddU8U8() -> Fail");
//}

Debug.WriteLine("##PASS##");

while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ private static bool SignOrZeroExtend(BaseInstruction instruction, ulong operand1
if (instruction == IRInstruction.SignExtend8x32)
{
var value = (byte)operand1;
result = (value & 0x80) == 0 ? value : (byte)value | 0xFFFFFF00;
result = (value & 0x80) == 0 ? value : value | 0xFFFFFF00;
return true;
}
else if (instruction == IRInstruction.SignExtend16x32)
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Compiler.Framework/Platform/BaseMultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class BaseMultibootStage : BaseCompilerStage
/// <summary>
/// This address is the top of the initial kernel stack.
/// </summary>
private const uint StackAddress = 0x00A00000 - 8;
//private const uint StackAddress = 0x00A00000 - 8;

private struct MultibootV2Constants
{
Expand Down Expand Up @@ -99,7 +99,7 @@ private struct MultibootV2Constants

public int Height { get; set; }

public long InitialStackAddress { get; set; }
//public long InitialStackAddress { get; set; }

#endregion Data Members

Expand All @@ -110,7 +110,7 @@ protected override void Initialization()
Width = MosaSettings.MultibootVideoWidth;
Height = MosaSettings.MultibootVideoHeight;

InitialStackAddress = MosaSettings.Settings.GetValue("Multiboot.InitialStackAddress", StackAddress);
//InitialStackAddress = MosaSettings.Settings.GetValue("Multiboot.InitialStackAddress", StackAddress);
}

protected override void Setup()
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Demo.TestWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public static long Test4()
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool Test5()
{
return Mosa.UnitTests.Basic.ArrayLayoutTests.I8a();
return Mosa.UnitTests.Basic.ArrayLayoutTests.I8_4a();
}

public static string str = null!;
Expand Down
31 changes: 31 additions & 0 deletions Source/Mosa.Kernel.BareMetal/BootOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.DeviceSystem;
using Mosa.Runtime;

namespace Mosa.Kernel.BareMetal;
Expand Down Expand Up @@ -38,6 +39,14 @@ public static string GetValue(string key)
return result;
}

public static bool Contains(string value)
{
if (Contains(RuntimeOptions, value))
return true;

return Contains(StaticOptions, value);
}

private static string GetValue(Pointer options, string key)
{
if (options.IsNull)
Expand Down Expand Up @@ -101,4 +110,26 @@ private static string GetValue(Pointer options, string key)
return new string((sbyte*)options, start, len);
}
}

private static bool Contains(Pointer options, string value)
{
var valuelen = value.Length;

var index = 0;

for (var at = 0; ; at++)
{
var c = options.Load8(at);

if (c == 0)
break;

if (c == value[index])
index++;
else
index = 0;
}

return index == valuelen;
}
}
4 changes: 2 additions & 2 deletions Source/Mosa.Kernel.BareMetal/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static class Debug

public static void Setup(bool enable = false)
{
IsEnabled = enable;
IsEnabled = BootOptions.Contains("serialdebug");

WriteLine("[Debug Mode]");
WriteLine("[Serial Debug]");
}

public static void Initialize()
Expand Down
4 changes: 3 additions & 1 deletion Source/Mosa.Kernel.BareMetal/InitialGCMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public static void Initialize()

public static Pointer AllocateMemory(uint size)
{
//Debug.WriteLine("+ Initial Allocation Object: size = ", size, " @ ", new Hex(Available));
Debug.WriteLine("+ Initial Allocation Object: size = ", size, " @ ", new Hex(Available));

var available = Available;

Available += size;

Internal.MemoryClear(Available, size);

return available;
}
}
12 changes: 6 additions & 6 deletions Source/Mosa.Platform.x64/CompilerStages/MultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ private void CreateMultibootMethod()
var multibootEAX = Operand.CreateLabel(MultibootEAX, Architecture.Is32BitPlatform);
var multibootEBX = Operand.CreateLabel(MultibootEBX, Architecture.Is32BitPlatform);

var stackTop = CreateConstant(InitialStackAddress);
//var stackTop = CreateConstant(InitialStackAddress);
var zero = CreateConstant(0);
var offset = CreateConstant(8);
//var offset = CreateConstant(8);

var basicBlocks = new BasicBlocks();

Expand All @@ -41,10 +41,10 @@ private void CreateMultibootMethod()
var context = new Context(prologueBlock);

// Setup the stack and place the sentinel on the stack to indicate the start of the stack
context.AppendInstruction(X64.Mov64, rsp, stackTop);
context.AppendInstruction(X64.Mov64, rbp, stackTop);
context.AppendInstruction(X64.MovStore64, null, rsp, zero, zero);
context.AppendInstruction(X64.MovStore64, null, rsp, offset, zero);
//context.AppendInstruction(X64.Mov64, rsp, stackTop);
//context.AppendInstruction(X64.Mov64, rbp, stackTop);
//context.AppendInstruction(X64.MovStore64, null, rsp, zero, zero);
//context.AppendInstruction(X64.MovStore64, null, rsp, offset, zero);

// Place the multiboot address into a static field
context.AppendInstruction(X64.MovStore64, null, multibootEAX, zero, rax);
Expand Down
16 changes: 8 additions & 8 deletions Source/Mosa.Platform.x86/CompilerStages/MultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ private void CreateMultibootMethod()

var eax = Operand.CreateCPURegister32(CPURegister.EAX);
var ebx = Operand.CreateCPURegister32(CPURegister.EBX);
var ebp = Operand.CreateCPURegister32(CPURegister.EBP);
var esp = Operand.CreateCPURegister32(CPURegister.ESP);
//var ebp = Operand.CreateCPURegister32(CPURegister.EBP);
//var esp = Operand.CreateCPURegister32(CPURegister.ESP);

var multibootEAX = Operand.CreateLabel(MultibootEAX, Architecture.Is32BitPlatform);
var multibootEBX = Operand.CreateLabel(MultibootEBX, Architecture.Is32BitPlatform);

var stackTop = CreateConstant(InitialStackAddress);
//var stackTop = CreateConstant(InitialStackAddress);
var zero = CreateConstant(0);
var offset = CreateConstant(4);
//var offset = CreateConstant(4);

var basicBlocks = new BasicBlocks();

Expand All @@ -41,10 +41,10 @@ private void CreateMultibootMethod()
var context = new Context(prologueBlock);

// Setup the stack and place the sentinel on the stack to indicate the start of the stack
context.AppendInstruction(X86.Mov32, esp, stackTop);
context.AppendInstruction(X86.Mov32, ebp, stackTop);
context.AppendInstruction(X86.MovStore32, null, esp, zero, zero);
context.AppendInstruction(X86.MovStore32, null, esp, offset, zero);
//context.AppendInstruction(X86.Mov32, esp, stackTop);
//context.AppendInstruction(X86.Mov32, ebp, stackTop);
//context.AppendInstruction(X86.MovStore32, null, esp, zero, zero);
//context.AppendInstruction(X86.MovStore32, null, esp, offset, zero);

// Place the multiboot address into a static field
context.AppendInstruction(X86.MovStore32, null, multibootEAX, zero, eax);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using Mosa.Runtime.Plug;
using Mosa.Runtime.x86;

namespace Mosa.Plug.Korlib.System.Runtime.Intrinsics.X64;
namespace Mosa.Plug.Korlib.System.Runtime.Intrinsics.X86;

internal static class PopcntPlug
{
[Plug("System.Runtime.Intrinsics.X64.Popcnt::PopCount")]
[Plug("System.Runtime.Intrinsics.X86.Popcnt::PopCount")]
internal static uint PopCount(uint value)
{
return Native.Popcnt32(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static void InitializeArray(Array array, RuntimeFieldHandle fldHandle)

var arrayElements = arrayAddress + Pointer.Size;
var fieldData = fieldDefinition.FieldData;
uint dataLength = fieldDefinition.Size;
var dataLength = fieldDefinition.Size;

Mosa.Runtime.Internal.MemoryCopy(arrayElements, fieldData, dataLength);
}
Expand Down
39 changes: 6 additions & 33 deletions Source/Mosa.UnitTests.BareMetal.x86/Boot.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using Mosa.Kernel.BareMetal;
using Mosa.Kernel.BareMetal.x86;
using Mosa.Runtime.Plug;
using Mosa.UnitTests.Basic;
using Mosa.UnitTests.Optimization;
using Serial = Mosa.Kernel.BareMetal.x86.Serial;

namespace Mosa.UnitTests.BareMetal.x86;

/// <summary>
/// Boot
/// </summary>
public static class Boot
{
/// <summary>
/// Main
/// </summary>
public static void Main()
{
IDT.SetInterruptHandler(ProcessInterrupt);

UnitTestEngine.Setup(0x3F8); // Serial.COM1

Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("MOSA OS Version 2.4 - UnitTest");
Console.WriteLine();
Console.WriteLine();
var result = ArrayLayoutTests.C_4();

UnitTestEngine.DisplayUpdate();

UnitTestEngine.Setup(1);
IDT.SetInterruptHandler(ProcessInterrupt);

UnitTestEngine.Setup(Serial.COM1);
UnitTestEngine.EnterTestReadyLoop();
}

public static void ProcessInterrupt(uint interrupt, uint errorCode)
{
UnitTestEngine.Process();
}
private static void ProcessInterrupt(uint interrupt, uint errorCode) => UnitTestEngine.Process();

private static void ForceTestCollection()
{
// required to force assembly to be referenced and loaded
CommonTests.OptimizationTest1();
}

[Plug("Mosa.Runtime.StartUp::BootOptions")]
public static void SetBootOptions()
{
BootSettings.EnableDebugOutput = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
<ProjectReference Include="..\Mosa.Korlib\Mosa.Korlib.csproj" />
<ProjectReference Include="..\Mosa.UnitTests\Mosa.UnitTests.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Asm\Return10.o">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

0 comments on commit 00034e2

Please sign in to comment.