I am a beginner, so don't expect much. If I'm neglecting some important aspect, please let me know. Thanks.
For the past day I've been trying to figure this out. My x86 kernel boots with GRUB, I'm defining everything IDT and GDT related in Assembly (then call _load_gdt() and _load_idt() from C). I read the OSDev Wiki, James Molloy's guide, the Intel manuals (I'm still trying to fully understand them) and random blog posts. I mentioned the GDT because I suspect it might be problematic too (from what I've heard there is no real way to know if it's set up correctly).
Ok, here's the code (I apologize in advance for any stupid mistakes, I'm still learning):
Code: Select all
// main.c
#include <kernel/lime_tty.h>
extern void _load_gdt(); // From assembly
extern void _load_idt();
void lime_main()
{
lime_tty_init(TtyTextMode);
lime_tty_put_string("[ LIME ] Welcome to the Lime kernel!\n");
_load_gdt();
lime_tty_put_string("[ LIME ] Loaded GDT successfully!\n");
_load_idt();
lime_tty_put_string("[ LIME ] Loaded IDT successfully!\n");
asm ("int $0x03");
}
Code: Select all
; interrupts.asm
%macro ISRNOERR 1
isr%1:
cli
push byte 0
push byte %1
jmp isr_common_stub
isr%1_end:
%endmacro
%macro ISRERR 1
isr%1:
cli
push byte %1
jmp isr_common_stub
isr%1_end:
%endmacro
ISRNOERR 0
ISRNOERR 1
ISRNOERR 2
ISRNOERR 3
ISRNOERR 4
ISRNOERR 5
ISRNOERR 6
ISRNOERR 7
ISRERR 8
ISRNOERR 9
ISRERR 10
ISRERR 11
ISRERR 12
ISRERR 13
ISRERR 14
ISRNOERR 15
ISRNOERR 16
ISRNOERR 17
ISRNOERR 18
ISRNOERR 19
ISRNOERR 20
ISRNOERR 21
ISRNOERR 22
ISRNOERR 23
ISRNOERR 24
ISRNOERR 25
ISRNOERR 26
ISRNOERR 27
ISRNOERR 28
ISRNOERR 29
ISRNOERR 30
ISRNOERR 31
ISRNOERR 32
isr_common_stub:
pusha
mov ax, ds
push eax
mov ax, 0x10 ; Data segment descriptor (gdt.asm)
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
extern handler
call handler
pop eax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa
add esp, 8
sti
iret
Code: Select all
; idt.asm
section .text
global _load_idt
_load_idt:
lidt [idt_info]
ret
%macro IRQ 1
irq%1:
dd isr%1
dw 0x0008
db 0x00
db 10101110b
dd isr%1_end
%endmacro
%include "interrupts.asm"
section .rodata
idt:
IRQ 0
IRQ 1
IRQ 2
IRQ 3
IRQ 4
IRQ 5
IRQ 6
IRQ 7
IRQ 8
IRQ 9
IRQ 10
IRQ 11
IRQ 12
IRQ 13
IRQ 14
IRQ 15
IRQ 16
IRQ 17
IRQ 18
IRQ 19
IRQ 20
IRQ 21
IRQ 22
IRQ 23
IRQ 24
IRQ 25
IRQ 26
IRQ 27
IRQ 28
IRQ 29
IRQ 30
IRQ 31
IRQ 32
idt_info:
dw idt_info - idt - 1
dd idt
Code: Select all
; gdt.asm
section .data
ALIGN 4
section .text
global _load_gdt
_load_gdt:
cli
lgdt [gdt_desc]
jmp 0x08:gdt_flush
gdt_flush:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
ret
section .rodata
gdt:
gdt_null:
dd 0h
dd 0h
gdt_code:
dw 0FFFFh
dw 00000h
db 00h
db 10011010b
db 11001111b
db 0
gdt_data:
dw 0FFFFh
dw 00000h
db 00h
db 10010010b
db 11001111b
db 0
gdt_desc:
dw gdt_desc - gdt - 1
dd gdt