Skip to content

Commit

Permalink
WIP - Physical Page Allocator + Bug Fix (#1073)
Browse files Browse the repository at this point in the history
* - WIP - Physical Page Allocator
- Fixed Alignment bug!

* = Update docs
  • Loading branch information
tgiphil committed Jul 4, 2023
1 parent 38b5a5d commit 7771aa4
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Source/Docs/command-line-arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Below are the command line arguments available:
-display-off,Emulator.Display,off
-memory,Emulator.Memory
-gdb,Emulator.GDB,true
-term,Emulator.Runtime.Maximum,{value}
-term,Emulator.MaxRuntime,{value}
-debug,Launcher.Serial.Console,true

Launcher - Emulator - Qemu & VMWare:
Expand Down
2 changes: 1 addition & 1 deletion Source/Docs/settings-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Emulator Settings
Emulator.Memory,Amount of memory for the virtual machine in MB
Emulator.Display,"If true, show the video display"
Emulator.SVGA,"SVGA mode: std, cirrus, vbe, virtio or vmware"
Emulator.Runtime.Maximum,Maximum runtime of virtual machine in seconds (future)
Emulator.MaxRuntime,Maximum runtime of the virtual machine in seconds (future)
Emulator.GDB,"If true, enables GDB within emulator"
Emulator.Serial,"Serial Emulation type None, Pipe, TCPServer, TCPClient"
Emulator.Serial.Host,Serial Host Name or IP address
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Compiler.Common/Alignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public static ulong AlignUp(ulong position, uint alignment)

public static uint AlignDown(uint position, uint alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}

public static int AlignDown(int position, int alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}

public static ulong AlignDown(ulong position, uint alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}
}
27 changes: 27 additions & 0 deletions Source/Mosa.Compiler.Framework.xUnit/AlignmmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Common;
using Xunit;

namespace Mosa.Compiler.Framework.xUnit;

public class AlignmmentTests
{
[Fact]
public void AlignUp()
{
Assert.True(Alignment.AlignUp(3, 1) == 3);
Assert.True(Alignment.AlignUp(3, 2) == 4);
Assert.True(Alignment.AlignUp(4096 * 2, 4096) == 4096 * 2);
Assert.True(Alignment.AlignUp(4096 + 1, 4096) == 4096 * 2);
}

[Fact]
public void AlignDown()
{
Assert.True(Alignment.AlignDown(3, 1) == 3);
Assert.True(Alignment.AlignDown(3, 2) == 2);
Assert.True(Alignment.AlignDown(4096 * 2, 4096) == 4096 * 2);
Assert.True(Alignment.AlignDown((4096 * 2) + 10, 4096) == 4096 * 2);
}
}
9 changes: 5 additions & 4 deletions Source/Mosa.Kernel.BareMetal.x86/PageTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ internal static class PageTable

public static void Setup()
{
Debug.Write(ConsoleColor.BrightMagenta, "a");
Debug.WriteLine(ConsoleColor.BrightMagenta, "PageTable:Setup()");

GDTTable = new GDTTable(PhysicalPageAllocator.Reserve());
Debug.Write(ConsoleColor.BrightMagenta, "b");
Debug.WriteLine(ConsoleColor.BrightMagenta, " > GDTTable");

PageDirectory = PhysicalPageAllocator.Reserve(1024);
Debug.Write(ConsoleColor.BrightMagenta, "c");
Debug.WriteLine(ConsoleColor.BrightMagenta, " > PageDirectory");

PageTables = PhysicalPageAllocator.Reserve(1024);
Debug.Write(ConsoleColor.BrightMagenta, "d");
Debug.WriteLine(ConsoleColor.BrightMagenta, " > PageTables");
}

public static void Initialize()
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Kernel.BareMetal/Alignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static ulong AlignUp(long position, uint alignment)

public static uint AlignDown(uint position, uint alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}

public static int AlignDown(int position, int alignment)
Expand All @@ -41,7 +41,7 @@ public static int AlignDown(int position, int alignment)

public static ulong AlignDown(ulong position, uint alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}

public static ulong AlignDown(ulong position, int alignment)
Expand All @@ -61,6 +61,6 @@ public static ulong AlignDown(long position, uint alignment)

public static ulong AlignDown(ulong position, ulong alignment)
{
return position - (alignment - position % alignment) % alignment;
return position - (position % alignment);
}
}
5 changes: 1 addition & 4 deletions Source/Mosa.Kernel.BareMetal/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static void EntryPoint()

Console.Write(ConsoleColor.BrightGreen, "> Enabling debug logging...");
Debug.Setup(true);
Debug.WriteLine("Booting...");
Console.WriteLine(ConsoleColor.BrightBlack, " [Completed]");

Console.Write(ConsoleColor.BrightGreen, "> Boot page allocator...");
Expand All @@ -34,15 +33,13 @@ public static void EntryPoint()

Console.Write(ConsoleColor.BrightGreen, "> Memory map...");
BootMemoryMap.Setup();
//BootMemoryMap.Dump();
BootMemoryMap.Dump();
Console.WriteLine(ConsoleColor.BrightBlack, " [Completed]");

Console.Write(ConsoleColor.BrightGreen, "> Physical page allocator...");
PhysicalPageAllocator.Setup();
Console.WriteLine(ConsoleColor.BrightBlack, " [Completed]");

Debug.Kill();

Console.Write(ConsoleColor.BrightGreen, "> Page table...");
PageTable.Setup();
Console.WriteLine(ConsoleColor.BrightBlack, " [Completed]");
Expand Down
38 changes: 22 additions & 16 deletions Source/Mosa.Kernel.BareMetal/BootMemory/BootMemoryMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static void Setup()
Count = 0
};

ImportPlatformMemoryMap();
ImportMultibootV1MemoryMap();
ImportPlatformMemoryMap();
}

public static BootMemoryMapEntry SetMemoryMap(AddressRange range, BootMemoryType type)
Expand Down Expand Up @@ -70,6 +70,8 @@ public static Pointer GetAvailableMemory()

private static void ImportMultibootV1MemoryMap()
{
Debug.WriteLine("BootMemoryMap::ImportMultibootV1MemoryMap()");

if (!Multiboot.IsAvailable)
return;

Expand All @@ -78,6 +80,8 @@ private static void ImportMultibootV1MemoryMap()

AvailableMemory = new Pointer(Multiboot.MultibootV1.MemoryUpper * 1024);

Debug.WriteLineHex(" > Available Memory: ", AvailableMemory.ToInt64());

var memoryMapEnd = Multiboot.MultibootV1.MemoryMapStart + Multiboot.MultibootV1.MemoryMapLength;

var entry = new MultibootV1MemoryMapEntry(Multiboot.MultibootV1.MemoryMapStart);
Expand All @@ -102,26 +106,28 @@ private static void ImportPlatformMemoryMap()

public static void Dump()
{
Console.WriteLine(ConsoleColor.Magenta);
Console.WriteLine("BootMemoryMap - Dump:");
Console.WriteLine("=====================");
Console.Write("Entries: ");
Console.WriteValue(List.Count);
Console.WriteLine();

for (uint slot = 0; slot < List.Count; slot++)
Debug.WriteLine(ConsoleColor.Magenta);
Debug.WriteLine("BootMemoryMap - Dump:");
Debug.WriteLine("=====================");
Debug.Write("Entries: ");
Debug.WriteValue(List.Count);
Debug.WriteLine();

for (var slot = 0u; slot < List.Count; slot++)
{
var entry = GetBootMemoryMapEntry(slot);

Console.Write("Start: 0x");
Console.WriteValueAsHex(entry.StartAddress.ToUInt64(), 8);
Console.Write(" Size: 0x");
Console.WriteValueAsHex(entry.Size, 8);
Console.Write(" Type: ");
Console.WriteValue((byte)entry.Type);
Console.WriteLine();
Debug.Write("Start: 0x");
Debug.WriteValueAsHex(entry.StartAddress.ToUInt64(), 8);
Debug.Write(" Size: 0x");
Debug.WriteValueAsHex(entry.Size, 8);
Debug.Write(" Type: ");
Debug.WriteValue((byte)entry.Type);
Debug.WriteLine();
}

Debug.WriteLine();

#endregion Diagnostic
}
}
6 changes: 3 additions & 3 deletions Source/Mosa.Kernel.BareMetal/BootMemory/BootMemoryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mosa.Kernel.BareMetal.BootMemory;

public enum BootMemoryType
{
Available,
Reserved,
Kernel,
Available = 0,
Reserved = 1,
Kernel = 2,
}
75 changes: 73 additions & 2 deletions Source/Mosa.Kernel.BareMetal/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Mosa.Kernel.BareMetal;
/// </summary>
public static class Debug
{
private static byte NewLine = (byte)'\n';

public static bool IsEnabled { get; set; } = false;

#region Public API
Expand All @@ -29,13 +31,21 @@ public static void Kill()
WriteLine("##KILL##");
}

public static void WriteLine()
{
if (!IsEnabled)
return;

Write(NewLine);
}

public static void WriteLine(string message)
{
if (!IsEnabled)
return;

Write(message);
Write('\n');
Write(NewLine);
}

public static void Write(string message)
Expand All @@ -50,15 +60,76 @@ public static void Write(string message)
}

public static void Write(char c)
{
Write((byte)c);
}

public static void Write(byte b)
{
if (!IsEnabled)
return;

Platform.Debug((byte)c);
Platform.Debug(b);
}

// Helpers

public static void WriteLine(string message, ulong value)
{
if (!IsEnabled)
return;

Write(message);
WriteValue(value);
Write(NewLine);
}

public static void WriteLine(string message, long value)
{
WriteLine(message, (ulong)value);
}

public static void WriteLine(string message1, ulong value1, string message2, ulong value2)
{
if (!IsEnabled)
return;

Write(message1);
WriteValue(value1);
Write(message2);
WriteValue(value2);
Write(NewLine);
}

public static void WriteLineHex(string message, ulong value)
{
if (!IsEnabled)
return;

Write(message);
WriteValueAsHex(value);
Write(NewLine);
}

public static void WriteLineHex(string message, long value)
{
WriteLineHex(message, (ulong)value);
}

public static void WriteLineHex(string message1, ulong value1, string message2, ulong value2)
{
if (!IsEnabled)
return;

Write(message1);
WriteValueAsHex(value1);
Write(message2);
WriteValueAsHex(value2);
Write(NewLine);
}

// Helpers with Color

public static void WriteLine(ConsoleColor color, string s)
{
//SetForground(color);
Expand Down

0 comments on commit 7771aa4

Please sign in to comment.