Skip to content

Commit

Permalink
Serial Debug Logging (#1071)
Browse files Browse the repository at this point in the history
* - Updated Serial Logging

* - Updated Serial Logging

* - Updated Serial Logging

* - Updated Serial Logging

* - Updated Serial Logging
  • Loading branch information
tgiphil committed Jul 4, 2023
1 parent 1f42424 commit 38b5a5d
Show file tree
Hide file tree
Showing 34 changed files with 1,088 additions and 379 deletions.
4 changes: 3 additions & 1 deletion Source/Docs/command-line-arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ Below are the command line arguments available:
-bochs,Emulator,bochs
-display,Emulator.Display,on
-display-off,Emulator.Display,off
-memory,Emulator.Memory,
-memory,Emulator.Memory
-gdb,Emulator.GDB,true
-term,Emulator.Runtime.Maximum,{value}
-debug,Launcher.Serial.Console,true

Launcher - Emulator - Qemu & VMWare:
-vmware-svga,Emulator.SVGA,vmware
Expand Down
5 changes: 4 additions & 1 deletion Source/Docs/settings-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ Launcher Settings
Launcher.Exit,"If true, exit immediately after launch"
Launcher.PlugKorlib,"If true, automatically include the plugs for CoreLib"
Launcher.GDB,"If true, launch the GNU GDB application after VM launch"
Launcher.Serial.Console,"If true, outout the serial to console"
Launcher.Serial.File,Filename to emit serial output (future)
Launcher.Debugger,"If true, launch the MOSA debugger application after VM launch"
Launcher.Test,"If true, monitors VM serial for success or failure messages"
Launcher.Test,"If true, monitors VM serial for success or failure messages"

Image Settings
--------------
Expand All @@ -151,6 +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.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
2 changes: 1 addition & 1 deletion Source/Mosa.Demo.CoolWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static void Main()

IDT.SetInterruptHandler(manager.ProcessInterrupt);

Logger.Log("<SELFTEST:PASSED>");
Logger.Log("##PASS##");

manager.Start();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Demo.HelloWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public static void Main()

Console.Goto(12, 0);

Logger.Log("<SELFTEST:PASSED>");
Logger.Log("##PASS##");

while (true)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Demo.SVGAWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void Main()
Kernel.x86.Kernel.Setup();
IDT.SetInterruptHandler(ProcessInterrupt);

Console.WriteLine("<SELFTEST:PASSED>");
Console.WriteLine("##PASS##");

HAL = new Hardware();
Random = new Random();
Expand Down
6 changes: 6 additions & 0 deletions Source/Mosa.Kernel.BareMetal.x86/PageTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Mosa.Runtime;
using Mosa.Runtime.x86;
using Mosa.Kernel.BareMetal;

namespace Mosa.Kernel.BareMetal.x86;

Expand All @@ -16,10 +17,15 @@ internal static class PageTable

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

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

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

public static void Initialize()
Expand Down
7 changes: 7 additions & 0 deletions Source/Mosa.Kernel.BareMetal.x86/PlatformPlug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void EntryPoint()
Multiboot.Setup(new Pointer(ebx), eax);

SSE.Setup();
SerialDebug.Setup();
}

[Plug("Mosa.Kernel.BareMetal.Platform::GetBootReservedRegion")]
Expand Down Expand Up @@ -76,4 +77,10 @@ public static void ConsoleWrite(byte c)
{
VGAConsole.Write(c);
}

[Plug("Mosa.Kernel.BareMetal.Platform::Debug")]
public static void Debug(byte c)
{
SerialDebug.Write(c);
}
}
87 changes: 87 additions & 0 deletions Source/Mosa.Kernel.BareMetal.x86/Serial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Runtime.x86;

namespace Mosa.Kernel.BareMetal.x86;

/// <summary>
/// Serial Controller
/// </summary>
public static class Serial
{
public const ushort COM1 = 0x3F8; // Kernel log
public const ushort COM2 = 0x2F8; // Mosa Debugger
public const ushort COM3 = 0x3E8;
public const ushort COM4 = 0x2E8;

private const byte COM_Data = 0x00;
private const byte COM_Interrupt = 0x01;
private const byte COM_LineControl = 0x02;
private const byte COM_ModemControl = 0x03;
private const byte COM_LineStatus = 0x04;
private const byte COM_ModemStatus = 0x05;
private const byte COM_Scratch = 0x06;

public static void SetupPort(ushort com)
{
// Disable all interrupts
Native.Out8((ushort)(com + COM_Interrupt), 0x00);

// Enable DLAB (set baud rate divisor)
Native.Out8((ushort)(com + COM_ModemControl), 0x80);

// Set divisor to 1 (lo byte) 115200 baud
Native.Out8((ushort)(com + COM_Data), 0x01);

// (hi byte)
Native.Out8((ushort)(com + COM_Interrupt), 0x00);

// 8 bits, no parity, one stop bit
Native.Out8((ushort)(com + COM_ModemControl), 0x03);

// Enable FIFO, clear them, with 14-byte threshold
Native.Out8((ushort)(com + COM_LineControl), 0xC7);

// IRQs enabled, RTS/DSR set
Native.Out8((ushort)(com + COM_LineStatus), 0x0B);

// Enable all interrupts
Native.Out8((ushort)(com + COM_Interrupt), 0x0F);
}

public static bool IsDataReady(ushort com)
{
return (Native.In8((ushort)(com + COM_ModemStatus)) & 0x01) == 0x01;
}

public static byte Read(ushort com)
{
while (!IsDataReady(com))
{
//Native.Hlt();
}

return Native.In8(com);
}

public static void WaitForWriteReady(ushort com)
{
while ((Native.In8((ushort)(com + COM_ModemStatus)) & 0x20) == 0x0)
{
//Native.Hlt();
}
}

public static void Write(ushort com, byte c)
{
WaitForWriteReady(com);

Native.Out8(com, c);
}

public static void Write(ushort com, string message)
{
foreach (var c in message)
Write(com, (byte)c);
}
}
61 changes: 61 additions & 0 deletions Source/Mosa.Kernel.BareMetal.x86/SerialDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

namespace Mosa.Kernel.BareMetal.x86;

/// <summary>
/// SerialDebug
/// </summary>
public static class SerialDebug
{
#region Private Members

private static ushort SerialPort = Serial.COM2;
private static byte NewLine = (byte)'\n';

private static bool IsIntialize = false;

#endregion Private Members

#region Public API

public static void Setup()
{
IsIntialize = false;
}

public static void Initalized()
{
if (IsIntialize)
return;

Serial.SetupPort(SerialPort);

IsIntialize = true;

WriteLine("[Debug Mode]");
}

public static void Write(byte c)
{
if (!IsIntialize)
Initalized();

Serial.Write(Serial.COM1, c);
}

#endregion Public API

private static void Write(string s)
{
foreach (var c in s)
{
Write((byte)c);
}
}

private static void WriteLine(string s)
{
Write(s);
Write(NewLine);
}
}
3 changes: 3 additions & 0 deletions Source/Mosa.Kernel.BareMetal.x86/VGAConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Mosa.Kernel.BareMetal.x86;
/// </summary>
public static class VGAConsole
{
// VT100 Info:
// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

private enum ControlState
{ Normal, Escape };

Expand Down
11 changes: 7 additions & 4 deletions Source/Mosa.Kernel.BareMetal/Boot.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using Mosa.DeviceSystem;
using Mosa.DeviceSystem.Service;
using Mosa.Kernel.BareMetal.BootMemory;
using Mosa.Kernel.BareMetal.GC;
using Mosa.Runtime;
Expand All @@ -26,20 +23,26 @@ public static void EntryPoint()

Console.WriteLine(ConsoleColor.BrightYellow, "Initializing kernel...");

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...");
BootPageAllocator.Setup();
Console.WriteLine(ConsoleColor.BrightBlack, " [Completed]");

Console.Write(ConsoleColor.BrightGreen, "> Memory map...");
BootMemoryMap.Setup();

//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
2 changes: 1 addition & 1 deletion Source/Mosa.Kernel.BareMetal/BootMemory/BootMemoryMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static void ImportPlatformMemoryMap()

public static void Dump()
{
Console.WriteLine();
Console.WriteLine(ConsoleColor.Magenta);
Console.WriteLine("BootMemoryMap - Dump:");
Console.WriteLine("=====================");
Console.Write("Entries: ");
Expand Down
11 changes: 11 additions & 0 deletions Source/Mosa.Kernel.BareMetal/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public static void Write(ConsoleColor color, string s)
Write(s);
}

public static void WriteLine(ConsoleColor color)
{
SetForground(color);
Write(Newline);
}

public static void Write(ConsoleColor color)
{
SetForground(color);
}

public static void WriteLine()
{
Write(Newline);
Expand Down

0 comments on commit 38b5a5d

Please sign in to comment.