Skip to content

Commit

Permalink
Added a kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
oystedal committed Apr 23, 2017
1 parent cf9cf50 commit 9081656
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
48 changes: 48 additions & 0 deletions kernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "util.h"
#include "stdint.h"

struct mem_region {
uint32_t valid;
uint32_t base;
uint32_t length;
};

struct mem_regions {
uint32_t count;
struct mem_region regions[1];
};

static const char* fact = "Today is tomorrow's yesterday.";

uint32_t
fib(uint32_t n)
{
if (n == 0) return 0;
if (n == 1) return 1;
return n + fib(n-1);
}

void _start(uint32_t meaning_of_life)
{
__asm__ volatile ("andq $0xFFFFFFFFFFFFFFFF, %rsp");

cls();
kprintf("The kernel has discovered the following facts:\n");
kprintf(" 2+2 = %d\n", 2+2);
kprintf(" fib(10) = %d\n", fib(10));
kprintf(" meaning_of_life = %d\n", meaning_of_life);
kprintf(" %s\n", fact);
kprintf("\n");
kprintf("Please stand by for more interesting facts as they are discovered...\n");

struct mem_regions *regions = (void*)meaning_of_life;
for (uint32_t i = 0; i < regions->count; ++i) {
struct mem_region *region = &(regions->regions[i]);
kprintf("Region %d: base: 0x%x length 0x%x\n", i, region->base, region->length);
}

for (;;) {
__asm__ volatile ("cli");
__asm__ volatile ("hlt");
}
}
6 changes: 3 additions & 3 deletions loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ load_kernel(const multiboot_info_t *info)

unsigned char *mod_addr = (unsigned char*)m;

if (*(mod_addr) != 0x7f &&
*(mod_addr+1) != 'E' &&
*(mod_addr+2) != 'L' &&
if (*(mod_addr) != 0x7f &&
*(mod_addr+1) != 'E' &&
*(mod_addr+2) != 'L' &&
*(mod_addr+3) != 'F')
{
kprintf("Kernel does not appear to be an ELF binary.\n");
Expand Down
7 changes: 4 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ unsigned char* const video = (unsigned char*)0xB8000;
uint32_t xpos = 0;
uint32_t ypos = 0;


void
cls(void)
{
int i;

for (i = 0; i < COLUMNS * LINES * 2; i++)
*(video + i) = 0;
for (i = 0; i < COLUMNS * LINES * 2; i++) {
if (*(video + i) != 0)
*(video + i) = 0;
}

xpos = 0;
ypos = 0;
Expand Down

0 comments on commit 9081656

Please sign in to comment.