diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..d307b08 --- /dev/null +++ b/.gdbinit @@ -0,0 +1,2 @@ +file kernel/bin/kernel-x86_64 +target remote :1234 \ No newline at end of file diff --git a/Makefile b/Makefile index 8dfe302..e2ae18c 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,6 @@ run: $(KERNEL_HDD) -drive file=$(KERNEL_HDD),format=raw \ -smp 2 \ -enable-kvm \ - -debugcon stdio \ $(QEMUFLAGS) bochs: $(KERNEL_HDD) ext/bochsrc diff --git a/kernel/Makefile b/kernel/Makefile index cc60313..f4c5127 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -8,7 +8,7 @@ LDSCRIPT = make/$(ARCH)/linker.ld -include make/$(ARCH)/make.config -CFLAGS ?= -O2 +CFLAGS ?= -Og -g CFLAGS := $(CFLAGS) $(ARCH_CFLAGS) \ -Wall -Wextra -pedantic -Wno-language-extension-token -std=gnu11 -I../ext -Isrc -fPIE LDFLAGS := $(LDFLAGS) -T $(LDSCRIPT) diff --git a/kernel/src/allocator/arena.c b/kernel/src/allocator/arena.c index b794692..50e880f 100644 --- a/kernel/src/allocator/arena.c +++ b/kernel/src/allocator/arena.c @@ -14,4 +14,4 @@ void* arena_alloc(struct arena_allocator* alloc, size_t len, size_t align) { alloc->begin += len; alloc->length -= len; return retval; -} \ No newline at end of file +} diff --git a/kernel/src/allocator/arena.h b/kernel/src/allocator/arena.h index 81e821b..fa290f9 100644 --- a/kernel/src/allocator/arena.h +++ b/kernel/src/allocator/arena.h @@ -5,4 +5,4 @@ struct arena_allocator { size_t length; }; -void *arena_alloc(struct arena_allocator* alloc, size_t length, size_t align); \ No newline at end of file +void *arena_alloc(struct arena_allocator* alloc, size_t length, size_t align); diff --git a/kernel/src/arch/x86/ops.c b/kernel/src/arch/x86/ops.c index 55f785d..ffa61d2 100644 --- a/kernel/src/arch/x86/ops.c +++ b/kernel/src/arch/x86/ops.c @@ -1,4 +1,9 @@ + +#include void halt_catch_fire() { asm volatile ("cli"); - while(1) asm volatile ("hlt"); + while(1) { + asm volatile ("hlt"); + puts("INTERRUPTED!"); + }; } diff --git a/kernel/src/arch/x86_64/entry-stivale2.c b/kernel/src/arch/x86_64/entry-stivale2.c index 822c706..214ca30 100644 --- a/kernel/src/arch/x86_64/entry-stivale2.c +++ b/kernel/src/arch/x86_64/entry-stivale2.c @@ -7,6 +7,8 @@ #include +#include "gdt.h" + static uint8_t stack[4096] = {0}; void stivale2_main(struct stivale2_struct *info); @@ -28,7 +30,7 @@ struct stivale2_header header2 = { void stivale2_main(struct stivale2_struct *info) { parse_stivale2(info); - print_stivale2_memmap(info); + // print_stivale2_memmap(info); uint64_t* cr3; __asm__ __volatile__ ( @@ -39,6 +41,14 @@ void stivale2_main(struct stivale2_struct *info) { ); printf("PAGE TABLE AT: %x\n", cr3); + struct gdt_ptr pGDT; + __asm__ __volatile__("sgdt %0" : : "m"(pGDT) : "memory"); + printf("GDT: %x (%d)\n", pGDT.base, pGDT.limit); + for(int i = 0; i < (pGDT.limit+1)/8; i++) { + uint64_t *gdt_ent = pGDT.base + 8*i; + printf("GDTent(%d at %x): %x\n", i, gdt_ent, *gdt_ent); + } + char *argv[3] = {"stivale2", info->bootloader_brand, info->bootloader_version}; kmain(3, argv); } diff --git a/kernel/src/arch/x86_64/gdt.h b/kernel/src/arch/x86_64/gdt.h new file mode 100644 index 0000000..c759881 --- /dev/null +++ b/kernel/src/arch/x86_64/gdt.h @@ -0,0 +1,9 @@ +#include + +struct gdt_ptr +{ + uint16_t limit; + uint64_t base; +} __attribute__((packed)); + +void loadgdt(); \ No newline at end of file diff --git a/kernel/src/arch/x86_64/gdt.nasm b/kernel/src/arch/x86_64/gdt.nasm new file mode 100644 index 0000000..475e779 --- /dev/null +++ b/kernel/src/arch/x86_64/gdt.nasm @@ -0,0 +1,16 @@ +section .rodata +gdt64: + dq 0 +.code: equ $-gdt64 + dq (1<<44) | (1<<47) | (1<<41) | (1<<43) | (1<<53) +.data: equ $ - gdt64 + dq (1<<44) | (1<<47) | (1<<41) +.pointer: + dw .pointer - gdt64 - 1 + dq gdt64 + +section .text +global loadgdt +loadgdt: + lgdt [gdt64.pointer] + \ No newline at end of file diff --git a/kernel/src/main.c b/kernel/src/main.c index d2d1a5e..318f28c 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -11,7 +11,7 @@ void kmain() { printf("Bootloader type: %s (%s %s)\n", boot_info.boot_protocol, boot_info.bootloader_name, boot_info.bootloader_ver); printf("System time: %d\n", boot_info.epoch); printf("Usable memory: %h (at %x)\n", boot_info.usable.len, boot_info.usable.start); - printf("Cmdline: %s", boot_info.cmdline); + printf("Cmdline: %s\n", boot_info.cmdline); vga_setcolor(VGA_DARK_GRAY); vga_write_elsewhere("(c) Quinten Kock 2020 (MIT License)", 24, 0); diff --git a/kernel/src/print/print.c b/kernel/src/print/print.c index fd5e482..fa06909 100644 --- a/kernel/src/print/print.c +++ b/kernel/src/print/print.c @@ -32,8 +32,12 @@ static void printhex(size_t num) { num /= 16; } - i++; puts("0x"); + for(int j = i; j >= 0; j--) { + putchar('0'); + } + + i++; puts(&buf[i]); }