From 82e3a85517b5207b442be5274e1c2b64be5d2d6f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 19 Apr 2022 19:00:56 +0200 Subject: [PATCH] Refactor & stuff --- Makefile | 4 ++-- bootloader/bios/disk.asm | 2 +- bootloader/bootloader.asm | 3 +++ kernel/idt.c | 23 +++++++++++++++-------- kernel/io.c | 4 +--- kernel/isr.asm | 17 +++++------------ kernel/kernel.c | 32 ++++++++++++++++++++++++-------- kernel/paging.c | 4 ++-- kernel/pic.c | 4 ++-- 9 files changed, 55 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 505088b..5bb97d5 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Compiler/assembler settings CC = gcc -CFLAGS = -fno-pie -m32 -Os -ffreestanding +CFLAGS = -fno-pie -m32 -Os -ffreestanding -g3 AA = nasm AFLAGS = @@ -10,7 +10,7 @@ LDFLAGS = -Wl,--oformat=binary -ffreestanding -nostdlib -shared -Ttext 0x1000 - # VM/Debug settings VM = qemu-system-x86_64 -VMFLAGS = -serial stdio -no-reboot -no-shutdown -d int +VMFLAGS = -serial stdio -no-reboot -no-shutdown #-d int # Do not touch these. diff --git a/bootloader/bios/disk.asm b/bootloader/bios/disk.asm index 9cb7cbf..dd346bf 100644 --- a/bootloader/bios/disk.asm +++ b/bootloader/bios/disk.asm @@ -40,6 +40,6 @@ read_error: jmp $ -read_error_string: db ASCII_CARRIAGE_RETURN, ASCII_LINEBREAK, "ERROR! Disk read failed.", ASCII_END +read_error_string: db ASCII_CARRIAGE_RETURN, ASCII_LINEBREAK, "Disk read failed.", ASCII_END error_code_string: db "Error code: ", ASCII_END sector_error_string: db "Invalid number of sectors read!", ASCII_END diff --git a/bootloader/bootloader.asm b/bootloader/bootloader.asm index f04fef8..717da40 100644 --- a/bootloader/bootloader.asm +++ b/bootloader/bootloader.asm @@ -66,6 +66,9 @@ pm_init: call BEGIN_PM BEGIN_PM: + mov [0xee88], byte GDT_CODE_SEG ; save code seg + mov [0xee89], byte GDT_DATA_SEG ; save data seg + ; Call the kernel call KERNEL_OFFSET jmp $ diff --git a/kernel/idt.c b/kernel/idt.c index f2ed57f..e352010 100644 --- a/kernel/idt.c +++ b/kernel/idt.c @@ -18,23 +18,26 @@ static idt_entry IDT[IDT_MAX_DESCS]; static idtr IDTR; void exception_handler() { - uint* irq_ptr = 0xe222; - uint8 irq = *irq_ptr; + uint* exc_ptr = 0xe222; + uint8 exc = *exc_ptr; + + pic_send_eoi(exc); - pic_send_eoi(irq); char* buf; + set_cursor_pos(0, 0); print("[exc] ", EXC_COLOR); - buf = itoa(irq, buf, 10); + buf = itoa(exc, buf, 10); print(buf, 0x0c); - new_line(); - } void idt_set_desc(uint8 idx, void* isr, uint8 flags) { idt_entry* desc = &IDT[idx]; // get descriptor + uint* gdt_code_ptr = 0xee88; + uint8 gdt_code = *gdt_code_ptr; + desc->offset_1 = (uint) isr & 0xffff; - desc->selector = 0x08; // kernel code selector for the GDT + desc->selector = gdt_code; // kernel code selector for the GDT desc->reserved = 0; desc->type_attr = flags; desc->offset_2 = (uint) isr >> 16; @@ -48,6 +51,10 @@ void idt_init() { for (uint8 idx = 0; idx < 32; idx++) idt_set_desc(idx, isr_stub_table[idx], 0x8e); + + outb_w(PIC1, 0xfd); + outb_w(PIC2, 0xfd); __asm__ __volatile__("lidt %0" : : "m"(IDTR)); - __asm__ __volatile__("sti"); + + //__asm__ __volatile__("sti"); } diff --git a/kernel/io.c b/kernel/io.c index 3db562e..638b3d8 100644 --- a/kernel/io.c +++ b/kernel/io.c @@ -26,9 +26,7 @@ void outw(unsigned short port, unsigned short data) { } -static inline void io_wait() { - outb(0x80, 0); -} +static inline void io_wait() { outb(0x80, 0); } unsigned char inb_w(unsigned short port) { inb(port); diff --git a/kernel/isr.asm b/kernel/isr.asm index 39d4c96..8d789e1 100644 --- a/kernel/isr.asm +++ b/kernel/isr.asm @@ -3,43 +3,36 @@ isr_debug_ptr equ 0xe222 %macro isr_err_stub 1 isr_stub_%+%1: mov [isr_debug_ptr], byte %1 - pusha call exception_handler - sti + hlt cli - ;hlt - popa iret %endmacro %macro isr_no_err_stub 1 isr_stub_%+%1: mov [isr_debug_ptr], byte %1 - pusha call exception_handler - sti + hlt cli - ;hlt - popa iret %endmacro extern exception_handler -isr_no_err_stub 0 -; TODO: make keeb drivers +isr_err_stub 0 isr_err_stub 1 isr_no_err_stub 2 isr_no_err_stub 3 isr_no_err_stub 4 isr_no_err_stub 5 -isr_no_err_stub 6 +isr_err_stub 6 isr_no_err_stub 7 isr_err_stub 8 ; err isr_no_err_stub 9 ; keyboard? isr_err_stub 10 isr_err_stub 11 isr_err_stub 12 -isr_err_stub 13 +isr_no_err_stub 13 isr_err_stub 14 isr_no_err_stub 15 isr_no_err_stub 16 diff --git a/kernel/kernel.c b/kernel/kernel.c index fdda082..eb100e5 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -8,21 +8,37 @@ void print_kernel_motd() { printalign("| __/ |_| |___) | A x86 operating system,", BANNER_COLOR, MIDDLE); printalign(" \\___|\\___/|____/ licenced under GPL-2.0", BANNER_COLOR, MIDDLE); - println(""); - printalign("Fun fact: e = lim[h->0] (1+h)^(1/h)", DEFAULT_COLOR, MIDDLE); + new_line(); + printalign("e = lim[h->0] (1+h)^(1/h)", DEFAULT_COLOR, MIDDLE); printalign("Created by E. Almqvist", DEFAULT_COLOR, MIDDLE); - println(""); + new_line(); } void print_kernel_stats() { char* buf; set_cursor_pos(0, 12); + + // GDT stuff + print("GDT Code Seg: ", 0x0f); + uint* code_ptr = 0xee88; + uint8 code = *code_ptr; + buf = itoa(code, buf, 16); + println(buf, DEFAULT_COLOR); + + print("GDT Data Seg: ", 0x0f); + uint* data_ptr = 0xee89; + uint8 data = *data_ptr; + buf = itoa(data, buf, 16); + println(buf, DEFAULT_COLOR); + + new_line(); + // Memory stats print("MEMORY BITMAP: ", 0x0f); buf = itoa(get_bitmap(), buf, 2); println(buf, DEFAULT_COLOR); - println(""); + new_line(); println("BIOS E820", 0x0f); print("Loaded Entries: ", DEFAULT_COLOR); @@ -33,7 +49,7 @@ void print_kernel_stats() { print("Physical Memory Size: "); println("?", DEFAULT_COLOR); - println(""); + new_line(); // VGA stats println("Display (VGA)", 0x0f); @@ -56,17 +72,18 @@ void print_kernel_stats() { void kernel_init() { pic_init(); // Init the PIC and remap it idt_init(); // Enable interupts + + //enable_paging(); + vga_init(); // Initialize the screen // Allocate VGA memory range pm_malloc_range(VGA_ADDRESS, VGA_ADDRESS_MAX, true); // force alloc the VGA range // ENABLE PAGING // TODO: make this work - //enable_paging(); clear_screen(); print_kernel_motd(); - /* print_kernel_stats(); char* buf; @@ -79,7 +96,6 @@ void kernel_init() { printalign(buf, 0x0f, MIDDLE); ++i; } - */ while(true) { __asm__("hlt"); } // never escape this function } diff --git a/kernel/paging.c b/kernel/paging.c index 3f020b8..6d685e8 100644 --- a/kernel/paging.c +++ b/kernel/paging.c @@ -4,8 +4,8 @@ void enable_paging() { println("Enabling paging...", DEFAULT_COLOR); - // extern int enable_paging_registers(); // Call the assembly SR - // enable_paging_registers(); // and enable paging + extern int enable_paging_registers(); // Call the assembly SR + enable_paging_registers(); // and enable paging return; } diff --git a/kernel/pic.c b/kernel/pic.c index 879175b..efd7899 100644 --- a/kernel/pic.c +++ b/kernel/pic.c @@ -17,8 +17,8 @@ void pic_remap(uint offset_1, uint offset_2) { outb_w(PIC2_CMD, ICW_INIT_MASK); - outb_w(PIC1_DATA, offset_1); // ICW2 master offset - outb_w(PIC2_DATA, offset_2); // ICW2 slave offset + outb_w(PIC1_DATA, offset_1); + outb_w(PIC2_DATA, offset_2); outb_w(PIC1_DATA, 0x4); outb_w(PIC2_DATA, 0x2);