From 5a119e428a9acb131c9eaf3a04e704a631e12f44 Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 1 Sep 2025 15:55:44 +0200 Subject: [PATCH] ... --- makefile | 6 +- src/boot.asm | 13 ----- src/idt.asm | 58 +++++++++++++------ src/idt_stub.asm | 109 ------------------------------------ src/{idt.c => interrupts.c} | 14 ++--- src/{idt.h => interrupts.h} | 0 src/kernel.c | 10 +++- src/keyboard.c | 2 +- 8 files changed, 58 insertions(+), 154 deletions(-) delete mode 100644 src/idt_stub.asm rename src/{idt.c => interrupts.c} (93%) rename src/{idt.h => interrupts.h} (100%) diff --git a/makefile b/makefile index 6b9aafc..fb87e50 100644 --- a/makefile +++ b/makefile @@ -6,8 +6,8 @@ CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c23 -g LDFLAGS = -g ASFLAGS = -f elf64 -g -F dwarf -SRC_C := src/kernel.c src/string.c src/vga.c src/idt.c src/keyboard.c -SRC_ASM := src/boot.asm src/idt_stub.asm +SRC_C := src/kernel.c src/string.c src/vga.c src/interrupts.c src/keyboard.c +SRC_ASM := src/boot.asm src/idt.asm OBJ_C := $(SRC_C:src/%.c=.build/%.o) OBJ_ASM := $(SRC_ASM:src/%.asm=.build/%.o) @@ -28,7 +28,7 @@ build-dir: cp .build/kernel .build/nub-os/boot/ grub-mkrescue -o .build/nub-os.iso .build/nub-os/ -.build/kernel: $(OBJS) | linker.ld +.build/kernel: $(OBJS) $(LD) $(LDFLAGS) -T linker.ld -o $@ $^ .build/%.o: src/%.c | build-dir diff --git a/src/boot.asm b/src/boot.asm index 9a2f546..ed788b9 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -26,17 +26,6 @@ section .bss pd: resb 4096 -section .bss - align 8 - idt64: - resb 4096 - -section .data - align 8 - idt64_descriptor: - dw 4095 ; IDT size (256 entries * 16 bytes - 1) - dq idt64 ; IDT base address - section .data align 8 gdt64: @@ -154,8 +143,6 @@ section .text mov ss, ax call setup_idt - sti - call kernel_main .hang: hlt diff --git a/src/idt.asm b/src/idt.asm index 9019ca7..09fc59c 100644 --- a/src/idt.asm +++ b/src/idt.asm @@ -14,17 +14,37 @@ isr_stub_%1: jmp isr_common %endmacro -%macro GENERATE_NUB_ISR_NOERR 2 - %assign i %1 - %rep (%2 - %1 + 1) - ISR_NOERR i - %assign i i+1 - %endrep -%endmacro +section .bss + align 4096 + idt64: + resb 4096 + +section .data + idt64_descriptor: + dw 4095 + dq idt64 section .text bits 64 setup_idt: + mov rdi, idt64 + mov rsi, isr_stub_table + mov rcx, 256 + .loop: + mov rax, [rsi] + mov [rdi], ax + mov word [rdi + 2], 0x08 + mov word [rdi + 4], 0x8E00 + shr rax, 16 + mov [rdi + 6], ax + shr rax, 16 + mov [rdi + 8], eax + mov dword [rdi + 12], 0 + add rdi, 16 + add rsi, 8 + loop .loop + lidt [idt64_descriptor] + sti ret isr_common: @@ -43,8 +63,10 @@ section .text push r13 push r14 push r15 + mov rdi, rsp call handle_isr + pop r15 pop r14 pop r13 @@ -60,10 +82,11 @@ section .text pop rcx pop rbx pop rax + add rsp, 16 iretq - ; CPU exceptions 0-31. Some of there contain error codes, so we define them manually + ; CPU exceptions 0-31. Some of these contain error codes, so we define them manually ISR_NOERR 0 ISR_NOERR 1 ISR_NOERR 2 @@ -97,18 +120,17 @@ section .text ISR_ERR 30 ISR_NOERR 31 - ; The remaining isr-s does not have an error code - GENERATE_ISRS 32, 255 - -%macro GENERATE_ISR_TABLE 2 - %assign i %1 - %rep (%2 - %1 + 1) - dq isr_stub_%[i] + ; Hardware interrupts and user-defined interrupts (32-255). These don't have error codes + %assign i 32 + %rep 224 + ISR_NOERR i %assign i i+1 %endrep -%endmacro section .data - global isr_stub_table isr_stub_table: - GENERATE_ISR_TABLE 0, 255 \ No newline at end of file + %assign i 0 + %rep 256 + dq isr_stub_%[i] + %assign i i+1 + %endrep \ No newline at end of file diff --git a/src/idt_stub.asm b/src/idt_stub.asm deleted file mode 100644 index bed39b3..0000000 --- a/src/idt_stub.asm +++ /dev/null @@ -1,109 +0,0 @@ -extern handle_isr - -isr_common: - push rax - push rbx - push rcx - push rdx - push rsi - push rdi - push rbp - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - - mov rdi, rsp - call handle_isr - - pop r15 - pop r14 - pop r13 - pop r12 - pop r11 - pop r10 - pop r9 - pop r8 - pop rbp - pop rdi - pop rsi - pop rdx - pop rcx - pop rbx - pop rax - - add rsp, 16 - iretq - -%macro ISR_NOERR 1 -isr_stub_%1: - push qword 0 - push qword %1 - jmp isr_common -%endmacro - -%macro ISR_ERR 1 -isr_stub_%1: - push qword %1 - jmp isr_common -%endmacro - -; CPU exceptions 0-31 -ISR_NOERR 0 -ISR_NOERR 1 -ISR_NOERR 2 -ISR_NOERR 3 -ISR_NOERR 4 -ISR_NOERR 5 -ISR_NOERR 6 -ISR_NOERR 7 -ISR_ERR 8 -ISR_NOERR 9 -ISR_ERR 10 -ISR_ERR 11 -ISR_ERR 12 -ISR_ERR 13 -ISR_ERR 14 -ISR_NOERR 15 -ISR_NOERR 16 -ISR_ERR 17 -ISR_NOERR 18 -ISR_NOERR 19 -ISR_NOERR 20 -ISR_NOERR 21 -ISR_NOERR 22 -ISR_NOERR 23 -ISR_NOERR 24 -ISR_NOERR 25 -ISR_NOERR 26 -ISR_NOERR 27 -ISR_NOERR 28 -ISR_NOERR 29 -ISR_ERR 30 -ISR_NOERR 31 - -%macro GENERATE_ISRS 2 - %assign i %1 - %rep (%2 - %1 + 1) - ISR_NOERR i - %assign i i+1 - %endrep -%endmacro - -GENERATE_ISRS 32, 255 - -%macro GENERATE_ISR_TABLE 2 - %assign i %1 - %rep (%2 - %1 + 1) - dq isr_stub_%[i] - %assign i i+1 - %endrep -%endmacro - -global isr_stub_table -isr_stub_table: -GENERATE_ISR_TABLE 0, 255 \ No newline at end of file diff --git a/src/idt.c b/src/interrupts.c similarity index 93% rename from src/idt.c rename to src/interrupts.c index b333a19..0a6d834 100644 --- a/src/idt.c +++ b/src/interrupts.c @@ -1,4 +1,4 @@ -#include "idt.h" +#include "interrupts.h" #include "vga.h" #include "kernel.h" @@ -7,12 +7,6 @@ #define PIC2_COMMAND 0xA0 #define PIC2_DATA 0xA1 -typedef struct -{ - u16 limit; - u64 base; -} __attribute__((packed)) idtr_t; - static irq_handler_t irq_handlers[16] = {0}; static const char* exception_messages[32] = { @@ -84,7 +78,11 @@ void handle_isr(isr_frame_t* frame) vga_print_uint(frame->err_code); vga_print("\n"); - __asm__ volatile("cli; hlt"); + while (true) + { + __asm__ volatile ("cli"); + __asm__ volatile ("hlt"); + } } else if (frame->int_no < 48) { diff --git a/src/idt.h b/src/interrupts.h similarity index 100% rename from src/idt.h rename to src/interrupts.h diff --git a/src/kernel.c b/src/kernel.c index 1d82691..ef62a3d 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,5 +1,5 @@ #include "kernel.h" -#include "idt.h" +#include "interrupts.h" #include "keyboard.h" #include "vga.h" @@ -17,11 +17,17 @@ void kernel_main() while (true) { + __asm__ volatile ("cli"); + __asm__ volatile ("hlt"); } } void kernel_panic() { vga_print("Kernel panic!\n"); - __asm__ volatile("cli; hlt"); + while (true) + { + __asm__ volatile ("cli"); + __asm__ volatile ("hlt"); + } } \ No newline at end of file diff --git a/src/keyboard.c b/src/keyboard.c index cb3bb1e..2c4c574 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -1,5 +1,5 @@ #include "keyboard.h" -#include "idt.h" +#include "interrupts.h" #include "kernel.h" #include "vga.h"