diff --git a/src/bootloader/stage2/main.c b/src/bootloader/stage2/main.c index 8c036ec..87046ba 100644 --- a/src/bootloader/stage2/main.c +++ b/src/bootloader/stage2/main.c @@ -10,9 +10,7 @@ #include "fat.h" #include "memdefs.h" #include "memory.h" - -#define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" -#define VERSION "v0.0.1" +#include "../../version.h" uint8_t* KernelLoadBuffer = (uint8_t*)MEMORY_LOAD_KERNEL; @@ -32,8 +30,8 @@ void* g_data = (void*)0x20000; void __attribute__((cdecl)) start(uint16_t bootDrive) { // Clear screen and Print Startup logo clrscr(); - printf("%s", LOGO); - printf("The Nano OS %s\n-------------------------------------\n", VERSION); + printf("%s", BOOTLOGO); + printf("The Nano Loader %s\n------------------------------\n", VERSION); // Test Real Mode printf("Testing Real Mode..."); diff --git a/src/kernel/arch/i686/gdt.asm b/src/kernel/arch/i686/gdt.asm new file mode 100644 index 0000000..015acf7 --- /dev/null +++ b/src/kernel/arch/i686/gdt.asm @@ -0,0 +1,40 @@ +;/////////////////////; +;Nanite OS ; +;COPYRIGHT (C) 2024 ; +;Tyler McGurrin ; +;/////////////////////; +[bits 32] + +; void __attribute__((cdecl)) i686_GDT_Load(GDTDescriptor* descriptor, uint16_t codeSegment, uint16_t dataSegment); +global i686_GDT_Load +i686_GDT_Load: + ; make new call frame + push ebp ; save old call frame + mov ebp, esp ; initialize new call frame + + ; load GDT + mov eax, [ebp + 8] + lgdt [eax] + + ; Reload Code Segment + mov eax, [ebp + 12] + push eax + push .reload_cs + retf + +.reload_cs: + + ; reload data segments + mov ax, [ebp + 16] + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax, + mov ss, ax + + + + ; restore old call frame + mov esp, ebp + pop ebp + ret \ No newline at end of file diff --git a/src/kernel/arch/i686/gdt.c b/src/kernel/arch/i686/gdt.c new file mode 100644 index 0000000..de7cb75 --- /dev/null +++ b/src/kernel/arch/i686/gdt.c @@ -0,0 +1,92 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#include "gdt.h" +#include + +typedef struct { + uint16_t LimitLow; + uint16_t BaseLow; + uint8_t BaseMiddle; + uint8_t Access; + uint8_t FlagsLimitHi; + uint8_t BaseHigh; +} __attribute__((packed)) GDTEntry; + +typedef struct { + uint16_t Limit; + GDTEntry* Ptr; +} __attribute__((packed)) GDTDescriptor; + +typedef enum { + GDT_ACCESS_CODE_READABLE = 0x02, + GDT_ACCESS_DATA_WRITEABLE = 0x02, + + GDT_ACCESS_CODE_CONFORMING = 0x04, + GDT_ACCESS_DATA_DIRECTION_NORMAL = 0x00, + GDT_ACCESS_DATA_DIRECTION_DOWN = 0x04, + + GDT_ACCESS_DATA_SEGMENT = 0x10, + GDT_ACCESS_CODE_SEGMENT = 0x18, + + GDT_ACCESS_DESCRIPTOR_TSS = 0x00, + + GDT_ACCESS_RING0 = 0x00, + GDT_ACCESS_RING1 = 0x20, + GDT_ACCESS_RING2 = 0x40, + GDT_ACCESS_RING3 = 0x60, + + GDT_ACCESS_PRESENT = 0x80, + +} GDT_ACCESS; + +typedef enum { + GDT_FLAG_64BIT = 0x20, + GDT_FLAG_32BIT = 0x40, + GDT_FLAG_16BIT = 0x00, + + GDT_FLAG_GRANULARITY_1B = 0x00, + GDT_FLAG_GRANULARITY_4k = 0x80, +} GDT_FLAGS; + +// macros! + +#define GDT_LIMIT_LOW(limit) (limit & 0xFFFF) +#define GDT_BASE_LOW(base) (base & 0xFFFF) +#define GDT_BASE_MIDDLE(base) ((base >> 16) & 0xFF) +#define GDT_FLAGS_LIMIT_HI(limit, flags)(((limit >> 16) & 0xF) | (flags & 0xF0)) +#define GDT_BASE_HIGH(base) ((base >> 24) & 0xFF) + +#define GDT_ENTRY(base, limit, access, flags){ \ + GDT_LIMIT_LOW(limit), \ + GDT_BASE_LOW(base), \ + GDT_BASE_MIDDLE(base), \ + access, \ + GDT_FLAGS_LIMIT_HI(limit, flags), \ + GDT_BASE_HIGH(base) \ +} +GDTEntry g_GDT[] = { + // NULL + GDT_ENTRY(0, 0, 0, 0), + + // 32-bit kernel code + GDT_ENTRY(0, + 0xFFFFF, + GDT_ACCESS_PRESENT | GDT_ACCESS_RING0 | GDT_ACCESS_CODE_SEGMENT | GDT_ACCESS_CODE_READABLE, + GDT_FLAG_32BIT | GDT_FLAG_GRANULARITY_4k), + // 32-bit data segment + GDT_ENTRY(0, + 0xFFFFF, + GDT_ACCESS_PRESENT | GDT_ACCESS_RING0 | GDT_ACCESS_DATA_SEGMENT | GDT_ACCESS_DATA_WRITEABLE, + GDT_FLAG_32BIT | GDT_FLAG_GRANULARITY_4k), +}; + +GDTDescriptor g_GDTDescriptor = { sizeof(g_GDT) - 1, g_GDT}; + +void __attribute__((cdecl)) i686_GDT_Load(GDTDescriptor* descriptor, uint16_t codeSegment, uint16_t dataSegment); + +void i686_GDT_Initialize() { + i686_GDT_Load(&g_GDTDescriptor, i686_GDT_CODE_SEGMENT, i686_GDT_DATA_SEGMENT); +} \ No newline at end of file diff --git a/src/kernel/arch/i686/gdt.h b/src/kernel/arch/i686/gdt.h new file mode 100644 index 0000000..592e621 --- /dev/null +++ b/src/kernel/arch/i686/gdt.h @@ -0,0 +1,11 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#pragma once + +#define i686_GDT_CODE_SEGMENT 0x8 +#define i686_GDT_DATA_SEGMENT 0x10 + +void i686_GDT_Initialize(); \ No newline at end of file diff --git a/src/kernel/arch/i686/idt.h b/src/kernel/arch/i686/idt.h new file mode 100644 index 0000000..ec047f2 --- /dev/null +++ b/src/kernel/arch/i686/idt.h @@ -0,0 +1,5 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ \ No newline at end of file diff --git a/src/kernel/hal/hal.c b/src/kernel/hal/hal.c new file mode 100644 index 0000000..8dc2318 --- /dev/null +++ b/src/kernel/hal/hal.c @@ -0,0 +1,6 @@ +#include "hal.h" +#include + +void HAL_Initialize() { + i686_GDT_Initialize(); +} \ No newline at end of file diff --git a/src/kernel/hal/hal.h b/src/kernel/hal/hal.h new file mode 100644 index 0000000..4ca63a4 --- /dev/null +++ b/src/kernel/hal/hal.h @@ -0,0 +1,3 @@ +#pragma once + +void HAL_Initialize(); \ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index 632f98d..c7aa4cc 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -5,19 +5,25 @@ \*----------------*/ #include #include -#include +#include +#include "../version.h" extern uint8_t __bss_start; extern uint8_t __end; void __attribute__((section(".entry"))) start(uint16_t bootDrive) { + + // print logo clrscr(); printf("%s", LOGO); printf("The Nano OS %s\n-------------------------------------\n", VERSION); - printf("Kernel Loaded!"); + printf("Loaded Kernel!\n"); + + // init HAL + printf("Initializing HAL..."); + HAL_Initialize(); + printf("Done!\n"); end: for (;;); diff --git a/src/version.h b/src/version.h new file mode 100644 index 0000000..1e3bd4b --- /dev/null +++ b/src/version.h @@ -0,0 +1,10 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#pragma once + +#define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" +#define VERSION "v0.0.1" +#define BOOTLOGO " _ ______ ____ ____ ______\n / | / / __ )/ __ \\/ __ /_ __/\n / |/ / __ / / / / / / // / \n / /| / /_/ / /_/ / /_/ // / \n/_/ |_/_____/\\____/\\____//_/ \n" \ No newline at end of file