From 93585dc4da3be099e1ffe7e757aa7caff2e1f013 Mon Sep 17 00:00:00 2001 From: Aiden Gall Date: Tue, 14 May 2024 19:43:28 +0100 Subject: initial commit --- kernel/main.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 kernel/main.c (limited to 'kernel/main.c') diff --git a/kernel/main.c b/kernel/main.c new file mode 100644 index 0000000..e69af30 --- /dev/null +++ b/kernel/main.c @@ -0,0 +1,83 @@ +#include +#include "util.h" + +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 + +struct e820_entry { + uint64_t base; + uint64_t length; + uint32_t type; + uint32_t ext_attr; +}; + +static uint64_t * +memsetu64(uint64_t *const dst, const uint64_t c, const uint64_t n) +{ + for (uint64_t i = 0; i < n; ++i) dst[i] = c; + return dst; +} + +static uint16_t * +print_hex(uint16_t *dst, uint64_t num) +{ + dst[0] = 0x0f00 | '0'; + dst[1] = 0x0f00 | 'x'; + + dst += 2; + + for (int i = 0; i < 16; ++i) { + uint16_t x = (num >> 60); + + *(dst++) = 0x0f00 | (x < 0xa ? x+'0' : x+'a'-0xa); + + num <<= 4; + } + + return dst; +} + +static uint16_t * +puts(uint16_t *dst, const char *str) +{ + for (; *str; ++str, ++dst) *dst = 0x0f00|(*str); + return dst; +} + +static uint16_t * +print_e820_table(uint16_t *dst, const struct e820_entry *const entries, const uint32_t count) +{ + for (uint32_t i = 0; i < count; ++i, dst += VGA_WIDTH) { + uint16_t *cursor; + + cursor = print_hex(dst, entries[i].base); + + *(cursor++) = 0x0f00 | '-'; + + cursor = print_hex(cursor, entries[i].base + entries[i].length - 1); + + ++cursor; + cursor = puts(cursor, entries[i].type == 1 ? "available" : "reserved"); + } + + return dst; +} + +void +kmain(void) +{ + vga_cursor_hide(); + + uint16_t *const vga_memory = (uint16_t *)0xb8000; + memsetu64((uint64_t *)vga_memory, 0x0f200f200f200f20, (VGA_WIDTH*VGA_HEIGHT*sizeof(uint16_t))/sizeof(uint64_t)); + + puts(vga_memory, "ProvidenceOS v0.1"); + puts(vga_memory+VGA_WIDTH*2, "e820 memory map:"); + + const uint32_t e820_size = *(uint32_t *)0xf000; + const struct e820_entry *const e820_mmap = (struct e820_entry *)0xf004; + + print_e820_table(vga_memory+(VGA_WIDTH*3), e820_mmap, e820_size); + + halt(); +} -- cgit v1.2.3