diff --git a/README.md b/README.md index 248bad2..fcfd634 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,21 @@ This is my custom kernel for learning purposes. I'm not a osdev at all, so please excuse the mess. -Right now it compiles for x86_64, 32-bit is not supported. Most of this is just skeleton code from osdev.org, but I cleaned +Right now it compiles for `x86_64`, 32-bit is not supported. Most of this is just skeleton code from osdev.org, but I cleaned some stuff. I use Stivale for higher half kernel loading and not having to gaff about a bootloader for now. Limine is included as my bootloader of choice. +## Organization + +This file organization is definitely not final, and will be shuffled around as sen is developed. Currently it is: +* `src/` - All of the source files for sen, including ld configs etc. + * `boot/` - Bootloader-related sources and entrypoints. + * `drivers/` - Drivers for specific subsystems in the kernel, such as tty drivers. + * `kernel/` - Vendor-agnostic stuff like the actual loop, the startup sequence, etc. This also contains the public kernel interface. + ## Building -Currently it requires GNU make, and some version of GCC. If you wish to build the iso, you also need xorriso. +Currently, sen requires GNU make, and some version of GCC. If you wish to build the iso, you also need xorriso. Run make to build the elf: diff --git a/src/boot/stivale/stivale.c b/src/boot/stivale/stivale.c index 4b22f01..e628f37 100644 --- a/src/boot/stivale/stivale.c +++ b/src/boot/stivale/stivale.c @@ -1,6 +1,5 @@ -#include -#include -#include +#include +#include // We need to tell the stivale bootloader where we want our stack to be. // We are going to allocate our stack as an array in .bss. @@ -76,45 +75,12 @@ static struct stivale2_header stivale_hdr = { // points to the first one in the linked list. .tags = (uintptr_t)&framebuffer_hdr_tag }; - -// We will now write a helper function which will allow us to scan for tags -// that we want FROM the bootloader (structure tags). -void *stivale2_get_tag(struct stivale2_struct *stivale2_struct, uint64_t id) { - struct stivale2_tag *current_tag = (void *)stivale2_struct->tags; - for (;;) { - // If the tag pointer is NULL (end of linked list), we did not find - // the tag. Return NULL to signal this. - if (current_tag == NULL) { - return NULL; - } - - // Check whether the identifier matches. If it does, return a pointer - // to the matching tag. - if (current_tag->identifier == id) { - return current_tag; - } - - // Get a pointer to the next tag in the linked list and repeat. - current_tag = (void *)current_tag->next; - } -} -struct stivale2_struct_tag_terminal *term_str_tag = NULL; -void *term_write_ptr = NULL; -void (*term_write)(const char *string, size_t length) = NULL; +struct stivale2_struct *stivale2_struct; // The following will be our kernel's entry point. -void _start(struct stivale2_struct *stivale2_struct) { - // Let's get the terminal structure tag from the bootloader. - term_str_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_TERMINAL_ID); - - // Check if the tag was actually found. - if (term_str_tag == NULL) { - // It wasn't found, just hang... - for (;;) { - asm ("hlt"); - } - } +void _start(struct stivale2_struct *in) { + stivale2_struct = in; kernel_main(); @@ -122,19 +88,4 @@ void _start(struct stivale2_struct *stivale2_struct) { for (;;) { asm ("hlt"); } -} - -void terminal_initialize() { - // stub for stivale - // Let's get the address of the terminal write function. - term_write_ptr = (void *)term_str_tag->term_write; - - // Now, let's assign this pointer to a function pointer which - // matches the prototype described in the stivale2 specification for - // the stivale2_term_write function. - term_write = term_write_ptr; -} - -void tty_print(const char* str, int length) { - term_write(str, length); -} +} \ No newline at end of file diff --git a/src/boot/stivale/stivale.h b/src/boot/stivale/stivale.h new file mode 100644 index 0000000..28360f6 --- /dev/null +++ b/src/boot/stivale/stivale.h @@ -0,0 +1,10 @@ +#ifndef _BOOT_STIVALE_H +#define _BOOT_STIVALE_H + +#include +#include +#include + +extern struct stivale2_struct *stivale2_struct; + +#endif \ No newline at end of file diff --git a/src/drivers/tty/stivale.c b/src/drivers/tty/stivale.c new file mode 100644 index 0000000..9bad029 --- /dev/null +++ b/src/drivers/tty/stivale.c @@ -0,0 +1,49 @@ +#include +#include + +// We will now write a helper function which will allow us to scan for tags +// that we want FROM the bootloader (structure tags). +void *stivale2_get_tag(struct stivale2_struct *stivale2_struct, uint64_t id) { + struct stivale2_tag *current_tag = (void *)stivale2_struct->tags; + for (;;) { + // If the tag pointer is NULL (end of linked list), we did not find + // the tag. Return NULL to signal this. + if (current_tag == NULL) { + return NULL; + } + + // Check whether the identifier matches. If it does, return a pointer + // to the matching tag. + if (current_tag->identifier == id) { + return current_tag; + } + + // Get a pointer to the next tag in the linked list and repeat. + current_tag = (void *)current_tag->next; + } +} + +struct stivale2_struct_tag_terminal *terminal_tag = NULL; + +// stivale function stubs +void (*term_write)(const char *string, size_t length) = NULL; + +// for this driver we simply load all the function pointers from the terminal tag +void terminal_initialize() { + terminal_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_TERMINAL_ID); + + // Check if the tag was actually found. + if (terminal_tag == NULL) { + // It wasn't found, just hang... + for (;;) { + asm ("hlt"); + } + } + + // the address from this struct is uint64, so we must convert to a pointer + term_write = (void *)terminal_tag->term_write; +} + +void tty_print(const char* str, int length) { + term_write(str, length); +} diff --git a/src/kernel.c b/src/kernel.c deleted file mode 100644 index 4e856c9..0000000 --- a/src/kernel.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -void kernel_main(void) { - terminal_initialize(); - tty_print("Hello, world!", 12); -} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c new file mode 100644 index 0000000..f7f0042 --- /dev/null +++ b/src/kernel/kernel.c @@ -0,0 +1,9 @@ +#include +#include + +void kernel_main(void) { + // initialize subsystems, currently only tty + terminal_initialize(); + + tty_print("Loading sen...", 15); +} diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h new file mode 100644 index 0000000..c172be9 --- /dev/null +++ b/src/kernel/kernel.h @@ -0,0 +1,2 @@ +// this is the main kernel entrypoint +void kernel_main(void); \ No newline at end of file diff --git a/src/tty.h b/src/kernel/tty/tty.h similarity index 100% rename from src/tty.h rename to src/kernel/tty/tty.h