Skip to content

Commit

Permalink
Add memory initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mempler committed Dec 16, 2023
1 parent e2d52ee commit 3545c69
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
3 changes: 3 additions & 0 deletions kernel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ nasm_cc_binary(
name = "kernel",
srcs = [
"include/Kernel.h",
"include/Kernel/Bootstrap.h",
"include/Kernel/X64/GDT.h",
"include/Kernel/X64/IDT.h",
"source/Bootstrap/Common.c",
"source/Bootstrap/Limine.c",
"source/Main.c",
"source/X64/GDT.c",
Expand Down Expand Up @@ -84,6 +86,7 @@ nasm_cc_binary(
"//visibility:public",
],
deps = [
"//lib/compat_c",
"//lib/runtime",
"@limine",
],
Expand Down
2 changes: 2 additions & 0 deletions kernel/include/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <RuntimeLib.h>

#include "Kernel/Bootstrap.h"

#include "Kernel/X64/GDT.h"
#include "Kernel/X64/IDT.h"

Expand Down
10 changes: 10 additions & 0 deletions kernel/include/Kernel/Bootstrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#ifndef __KERNEL_H__
#error "This file should be included from Kernel.h"
#endif

VOID
CommonStartupRoutine(
PAGE_ALLOCATOR *BumpAllocator
);
17 changes: 17 additions & 0 deletions kernel/source/Bootstrap/Common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <RuntimeLib.h>
#include <Kernel.h>

PAGE_ALLOCATOR* g_PageAllocator;

VOID
CommonStartupRoutine(
PAGE_ALLOCATOR *BumpAllocator
)
{
// TODO: g_PageAllocator = BuddyAllocatorInit (BumpAllocator);
g_PageAllocator = BumpAllocator;

KernelMain ();
}

// TODO: General purpose memory allocation functions using slab allocator
48 changes: 45 additions & 3 deletions kernel/source/Bootstrap/Limine.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
#include <RuntimeLib.h>
#include <Kernel.h>

#include "limine.h"

BUMP_ALLOCATOR g_BumpAllocator;

//
// Set the base revision to 1, this is recommended as this is the latest
// base revision described by the Limine boot protocol specification.
// See specification for further info.
//

LIMINE_BASE_REVISION(1);

//
// Memory map
//

struct limine_memmap_request g_LimineMemmap = {
.id = LIMINE_MEMMAP_REQUEST,
.revision = 0
};

VOID
StartupRoutine (
VOID
)
{
// TODO: pass the correct arguments
// TODO: init architecture
UINTN BumperIdx;

GDTInit ();
IDTInit ();

KernelMain ();
BumperIdx = 0;
for (UINT64 i = 0; i < g_LimineMemmap.response->entry_count; i++)
{
if (g_LimineMemmap.response->entries[i]->type == LIMINE_MEMMAP_USABLE)
{
BUMPER *bumper = &g_BumpAllocator.bumpers[BumperIdx++];
bumper->heap_start = g_LimineMemmap.response->entries[i]->base;
bumper->heap_end = g_LimineMemmap.response->entries[i]->base + g_LimineMemmap.response->entries[i]->length;
bumper->next = bumper->heap_start;

if (BumperIdx == MAXIMUM_BUMPERS)
{
// Memory is too fragmented, we can't continue. This is fine but we'll use
// some usable memory.

// TODO: log warning
break;
}
}
}

CommonStartupRoutine ((PAGE_ALLOCATOR *)&g_BumpAllocator);
}

0 comments on commit 3545c69

Please sign in to comment.