This commit is contained in:
nub31
2025-09-01 15:55:44 +02:00
parent 25342b507d
commit 5a119e428a
8 changed files with 58 additions and 154 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
%assign i 0
%rep 256
dq isr_stub_%[i]
%assign i i+1
%endrep

View File

@@ -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

View File

@@ -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)
{

View File

@@ -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");
}
}

View File

@@ -1,5 +1,5 @@
#include "keyboard.h"
#include "idt.h"
#include "interrupts.h"
#include "kernel.h"
#include "vga.h"