Skip to content

Commit

Permalink
feat: allocate stack for multiboot using bss (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
charsleysa committed Aug 27, 2023
1 parent 00034e2 commit 6afbdc7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
1 change: 0 additions & 1 deletion Source/Docs/settings-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ Multiboot Settings
Multiboot.Video,"If true, enable the framebuffer provided by Multiboot"
Multiboot.Video.Width,Video Width
Multiboot.Video.Height,Video Height
Multiboot.InitialStackAddress,Initial Stack Address

Debugger Settings
-----------------
Expand Down
10 changes: 4 additions & 6 deletions Source/Mosa.Compiler.Framework/Platform/BaseMultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public abstract class BaseMultibootStage : BaseCompilerStage

public const string MultibootEAX = "<$>mosa-multiboot-eax";
public const string MultibootEBX = "<$>mosa-multiboot-ebx";
public const string MultibootInitialStack = "<$>mosa-multiboot-initial-stack";

#region Constants

/// <summary>
/// This address is the top of the initial kernel stack.
/// This is the size of the initial kernel stack. (8KiB)
/// </summary>
//private const uint StackAddress = 0x00A00000 - 8;
protected const uint StackSize = 0x2000;

private struct MultibootV2Constants
{
Expand Down Expand Up @@ -99,8 +100,6 @@ private struct MultibootV2Constants

public int Height { get; set; }

//public long InitialStackAddress { get; set; }

#endregion Data Members

protected override void Initialization()
Expand All @@ -109,8 +108,6 @@ protected override void Initialization()
HasVideo = MosaSettings.MultibootVideo;
Width = MosaSettings.MultibootVideoWidth;
Height = MosaSettings.MultibootVideoHeight;

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

protected override void Setup()
Expand All @@ -119,6 +116,7 @@ protected override void Setup()

Linker.DefineSymbol(MultibootEAX, SectionKind.BSS, Architecture.NativeAlignment, Architecture.NativePointerSize);
Linker.DefineSymbol(MultibootEBX, SectionKind.BSS, Architecture.NativeAlignment, Architecture.NativePointerSize);
Linker.DefineSymbol(MultibootInitialStack, SectionKind.BSS, Architecture.NativeAlignment, StackSize);

multibootMethod = Compiler.CreateLinkerMethod("MultibootInit");
var methodData = Compiler.GetMethodData(multibootMethod);
Expand Down
15 changes: 9 additions & 6 deletions Source/Mosa.Platform.x64/CompilerStages/MultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ private void CreateMultibootMethod()

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

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

var basicBlocks = new BasicBlocks();

Expand All @@ -41,10 +42,12 @@ 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, stackBottom);
context.AppendInstruction(X64.Add64, rsp, rsp, stackTopOffset);
context.AppendInstruction(X64.Mov64, rbp, stackBottom);
context.AppendInstruction(X64.Add64, rbp, rbp, stackTopOffset);
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
19 changes: 11 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,16 @@ 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 stackBottom = Operand.CreateLabel(MultibootInitialStack, Architecture.Is32BitPlatform);

//var stackTop = CreateConstant(InitialStackAddress);
var stackTopOffset = CreateConstant(StackSize - 8);
var zero = CreateConstant(0);
//var offset = CreateConstant(4);
var offset = CreateConstant(4);

var basicBlocks = new BasicBlocks();

Expand All @@ -41,10 +42,12 @@ 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, stackBottom);
context.AppendInstruction(X86.Add32, esp, esp, stackTopOffset);
context.AppendInstruction(X86.Mov32, ebp, stackBottom);
context.AppendInstruction(X86.Add32, ebp, ebp, stackTopOffset);
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
1 change: 0 additions & 1 deletion Source/Mosa.Utility.Configuration/Name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public static class Name
public const string Linker_ShortSymbolNames = "Linker.ShortSymbolNames";
public const string Linker_StaticRelocations = "Linker.StaticRelocations";
public const string Linker_Symbols = "Linker.Symbols";
public const string Multiboot_InitialStackAddress = "Multiboot.InitialStackAddress";
public const string Multiboot_Version = "Multiboot.Version";
public const string Multiboot_Video = "Multiboot.Video";
public const string Multiboot_Video_Height = "Multiboot.Video.Height";
Expand Down

0 comments on commit 6afbdc7

Please sign in to comment.