cleanup arch api

This commit is contained in:
nub31
2025-09-06 01:27:13 +02:00
parent a3822dd350
commit 7b115e5b66
10 changed files with 115 additions and 43 deletions

14
.vscode/settings.json vendored
View File

@@ -3,6 +3,18 @@
"bitset": "c",
"algorithm": "c",
"format": "c",
"multiboot.h": "c"
"multiboot.h": "c",
"irq.h": "c",
"idt.h": "c",
"util.h": "c",
"deque": "c",
"forward_list": "c",
"list": "c",
"string": "c",
"unordered_map": "c",
"unordered_set": "c",
"vector": "c",
"any": "c",
"system_error": "c"
}
}

View File

@@ -2,19 +2,35 @@
#include "std.h"
typedef void (*arch_console_putchar_t)(char c);
typedef void (*arch_console_clear_t)();
typedef struct
{
const arch_console_putchar_t putchar;
const arch_console_clear_t clear;
} arch_console_api_t;
typedef u64 (*arch_mem_get_page_size_t)();
typedef struct
{
const arch_mem_get_page_size_t get_page_size;
} arch_mem_api_t;
typedef void (*arch_panic_t)(const char*);
typedef void (*arch_putchar_t)(char c);
typedef void (*arch_enable_interrupts_t)();
typedef void (*arch_disable_interrupts_t)();
typedef void (*arch_halt_t)();
typedef struct
{
arch_panic_t panic;
arch_putchar_t putchar;
arch_enable_interrupts_t enable_interrupts;
arch_disable_interrupts_t disable_interrupts;
arch_halt_t halt;
const arch_panic_t panic;
const arch_enable_interrupts_t enable_interrupts;
const arch_disable_interrupts_t disable_interrupts;
const arch_halt_t halt;
const arch_console_api_t console;
const arch_mem_api_t mem;
} arch_api_t;
extern arch_api_t arch_api;

58
src/arch/x86_64/arch.c Normal file
View File

@@ -0,0 +1,58 @@
#include "arch.h"
#include "console.h"
#include "mem.h"
#include "panic.h"
#include "util.h"
static void arch_halt()
{
halt();
}
static void arch_disable_interrupts()
{
disable_interrupts();
}
static void arch_enable_interrupts()
{
enable_interrupts();
}
static void arch_panic(const char* msg)
{
panic(msg);
}
static void arch_console_putchar(char c)
{
console_putchar(c, VGA_DEFAULT_COLOR);
}
static void arch_console_clear()
{
console_clear();
}
static u64 arch_get_page_size()
{
return PAGE_SIZE;
}
static const arch_mem_api_t arch_mem_api = {
.get_page_size = arch_get_page_size,
};
static const arch_console_api_t arch_console_api = {
.putchar = arch_console_putchar,
.clear = arch_console_clear,
};
arch_api_t arch_api = {
.halt = arch_halt,
.disable_interrupts = arch_disable_interrupts,
.enable_interrupts = arch_enable_interrupts,
.panic = arch_panic,
.console = arch_console_api,
.mem = arch_mem_api,
};

View File

@@ -13,7 +13,7 @@ static vga_char_t* vga_buffer = (vga_char_t*)0xb8000;
static u8 cursor_row = 0;
static u8 cursor_col = 0;
void console_put_char(char c, u8 color)
void console_putchar(char c, u8 color)
{
switch (c)
{

View File

@@ -21,5 +21,5 @@
#define VGA_DEFAULT_COLOR VGA_LIGHT_GRAY | VGA_BLACK << 4
void console_put_char(char character, u8 color);
void console_putchar(char character, u8 color);
void console_clear();

View File

@@ -1,4 +1,3 @@
#include "arch.h"
#include "console.h"
#include "interrupts/idt.h"
#include "interrupts/irq.h"
@@ -22,24 +21,10 @@ void x86_64_main(u32 magic, multiboot_info_t* info)
}
idt_init();
mem_init(info);
mem_alloc_2mb(MiB(128) + 1);
remap_pic();
enable_interrupts();
mem_init(info);
kernel_main();
}
void console_putchar(char c)
{
console_put_char(c, VGA_DEFAULT_COLOR);
}
arch_api_t arch_api = {
.putchar = console_putchar,
.halt = halt,
.disable_interrupts = disable_interrupts,
.enable_interrupts = enable_interrupts,
.panic = panic,
};

View File

@@ -18,12 +18,6 @@ typedef struct
static memory_region_t usable_regions[USABLE_REGION_SIZE];
static size_t num_regions = 0;
// Fixed at 2mb for now
#define PAGE_SIZE MiB(2)
// Defines the theoretical max memory the kernel can allocate, not the actual memory of the system
#define MAX_MEMORY GiB(64)
#define BITMAP_PAGE_COUNT (MAX_MEMORY / PAGE_SIZE)
#define BITMAP_SIZE (BITMAP_PAGE_COUNT / 8)
@@ -157,9 +151,9 @@ void mem_free_2mb_physical_page(u64 address)
static u64 create_pte(u64 physical_address)
{
if (physical_address & MiB(2) - 1)
if (physical_address & (PAGE_SIZE - 1))
{
printf("Physical address not 2MB aligned (0x%x)\n", physical_address);
printf("Physical address not page aligned (0x%x)\n", physical_address);
panic("Failed to create PTE");
}

View File

@@ -8,6 +8,12 @@
#define GiB(count) (MiB((u64)count) * 1024)
#define TiB(count) (GiB((u64)count) * 1024)
// Fixed at 2mb for now
#define PAGE_SIZE MiB(2)
// Defines the theoretical max memory the kernel can allocate, not the actual memory of the system
// The value must be a multible of 8
#define MAX_MEMORY GiB(64)
void mem_init(multiboot_info_t* info);
u64 mem_alloc_2mb_physical_page();

View File

@@ -4,5 +4,6 @@
void panic(const char* msg)
{
printf(msg);
disable_interrupts();
halt();
}

View File

@@ -15,20 +15,20 @@ void printf(const char* fmt, ...)
if (fmt[i] == '%')
{
arch_api.putchar('%');
arch_api.console.putchar('%');
}
else if (fmt[i] == 's')
{
const char* str = va_arg(args, const char*);
for (size_t j = 0; str[j] != '\0'; j++)
{
arch_api.putchar(str[j]);
arch_api.console.putchar(str[j]);
}
}
else if (fmt[i] == 'c')
{
char character = (char)va_arg(args, u64);
arch_api.putchar(character);
arch_api.console.putchar(character);
}
else if (fmt[i] == 'd')
{
@@ -37,7 +37,7 @@ void printf(const char* fmt, ...)
itoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
arch_api.putchar(buf[j]);
arch_api.console.putchar(buf[j]);
}
}
else if (fmt[i] == 'u')
@@ -47,7 +47,7 @@ void printf(const char* fmt, ...)
uitoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
arch_api.putchar(buf[j]);
arch_api.console.putchar(buf[j]);
}
}
else if (fmt[i] == 'x')
@@ -57,12 +57,12 @@ void printf(const char* fmt, ...)
uitoa64_hex(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
arch_api.putchar(buf[j]);
arch_api.console.putchar(buf[j]);
}
}
else
{
arch_api.putchar(fmt[i]);
arch_api.console.putchar(fmt[i]);
}
}
else if (fmt[i] == '%')
@@ -71,7 +71,7 @@ void printf(const char* fmt, ...)
}
else
{
arch_api.putchar(fmt[i]);
arch_api.console.putchar(fmt[i]);
}
}