feat(kernel): pic initialize and remap

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-13 19:02:35 +02:00
parent 649b68ee39
commit bd7468e69c
10 changed files with 114 additions and 36 deletions

View file

@ -16,3 +16,7 @@ vga_console_clear:
rep stosb rep stosb
ret ret
vga_console_putc:
ret

1
kernel/fs/fat.inc Normal file
View file

@ -0,0 +1 @@
;; File: fat.inc

5
kernel/fs/stpdfs.inc Normal file
View file

@ -0,0 +1,5 @@
;; File: stpdfs.inc
STPDFS_SB_MAGIC = 0x44505453
STPDFS_SB_REV = 1

View file

@ -21,9 +21,9 @@ gdt:
db 0x00, 0xF2, 0xCF, 0x00 db 0x00, 0xF2, 0xCF, 0x00
; Tss ; Tss
;.tss: .tss:
; dw ? dw ?
; dd ? dd ?
.end: .end:

3
kernel/irq.inc Normal file
View file

@ -0,0 +1,3 @@
irq_timer:
iret

View file

@ -26,6 +26,15 @@ idt_setup:
cmp ecx, 32 cmp ecx, 32
jb @b jb @b
mov ecx, 0x42
mov eax, isr_syscall
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
lidt [pIDT] lidt [pIDT]
sti sti
ret ret

View file

@ -2,6 +2,8 @@
format binary format binary
include 'const.inc' include 'const.inc'
include 'sys/bootinfo.inc'
include 'sys/cpu.inc'
org KBASE org KBASE
use32 use32
@ -52,15 +54,16 @@ kmain:
add ebx, KERNEL_VIRT_BASE add ebx, KERNEL_VIRT_BASE
call pmm_free_range call pmm_free_range
call pic_init
; load kernel gdt ; load kernel gdt
lgdt [pGDT] lgdt [pGDT]
; I don't think i need to reload segment cuz their value are already correct ; I don't think i need to reload segment cuz their value are already correct
xchg bx, bx
call pic_disable
call idt_setup call idt_setup
int 0x42
.halt: .halt:
hlt hlt
@ -71,20 +74,23 @@ kmain:
call klog call klog
jmp .halt jmp .halt
include 'sys/bootinfo.inc'
include 'klog.inc' include 'klog.inc'
include 'dev/vga_console.inc' include 'dev/vga_console.inc'
include 'mm/mm.inc' include 'mm/mm.inc'
include 'lock.inc' include 'lock.inc'
include 'gdt.inc' include 'gdt.inc'
include 'syscall.inc'
include 'isr.inc' include 'isr.inc'
include 'pic.inc' include 'pic.inc'
szMsgKernelAlive db "Kernel is alive", 0 szMsgKernelAlive db "Kernel is alive", 0
szErrorBootProtocol db "Error: wrong magic number", 0 szErrorBootProtocol db "Error: wrong magic number", 0
boot_structure BootInfo boot_structure BootInfo
kTSS TSS
align 4096 align 4096
stack_bottom: stack_bottom:
rb 0x4000 rb 0x4000

View file

@ -6,10 +6,47 @@ PIC1_DATA = 0x21
PIC2_COMMAND = 0xA0 PIC2_COMMAND = 0xA0
PIC2_DATA = 0xA1 PIC2_DATA = 0xA1
PIC_ICW1 = 0x10
PIC_ICW1_IC4 = 0x01
PIC_ICW1_SNGL = 0x02
PIC_ICW1_ADI = 0x04
PIC_ICW1_LTIM = 0x08
PIC_ICW4_8086 = 0x01
PIC_ICW4_AEOI = 0x02
PIC_ICW4_MSTR = 0x04
PIC_ICW4_BUF = 0x08
PIC_ICW4_SFNM = 0x10
PIC_EOI = 0x20 PIC_EOI = 0x20
pic_init: pic_init:
; ICW 1
mov al, PIC_ICW1 or PIC_ICW1_IC4
out PIC1_COMMAND, al
out PIC2_COMMAND, al
; ICW 2
mov al, 0x20
out PIC1_DATA, al
mov al, 0x28
out PIC2_DATA, al
; ICW 3
mov al, 0x4
out PIC1_DATA, al
mov al, 0x2
out PIC2_DATA, al
; ICW 4
mov al, PIC_ICW4_8086
out PIC1_DATA, al
out PIC2_DATA, al
xor al, al
out PIC1_DATA, al
out PIC2_DATA, al
ret ret

View file

@ -88,21 +88,24 @@ struc TSS {
.iomap dw ? .iomap dw ?
} }
;; Structure: idt_gate struc GDTGate {
}
;; Structure: IDTGate
;; .offset_low - TODO ;; .offset_low - TODO
;; .selector - TODO ;; .selector - TODO
;; .zero - TODO ;; .zero - TODO
;; .attributes - TODO ;; .attributes - TODO
;; .offset_high - TODO ;; .offset_high - TODO
;; ;;
struc idt_gate struc IDTGate {
.offset_low: resw 1 .offset_low dw ?
.selector: resw 1 .selector dw ?
.zero: resb 1 .zero db 0
.attributes: resb 1 .attributes db ?
.offset_high: resw 1 .offset_high dw ?
endstruc }
defn idt_gate
;; About: Gates ;; About: Gates
;; - Task Gate ;; - Task Gate
@ -130,31 +133,31 @@ defn idt_gate
;; > +--------------- |----------------+-----------------|-----------------+ ;; > +--------------- |----------------+-----------------|-----------------+
struc intframe struc intframe {
;; registers ;; registers
.edi: resd 1 .edi dd ?
.esi: resd 1 .esi dd ?
.ebp: resd 1 .ebp dd ?
.esp: resd 1 .esp dd ?
.ebx: resd 1 .ebx dd ?
.edx: resd 1 .edx dd ?
.ecx: resd 1 .ecx dd ?
.eax: resd 1 .eax dd ?
;; ;;
.gs: resd 1 .gs dd ?
.fs: resd 1 .fs dd ?
.es: resd 1 .es dd ?
.ds: resd 1 .ds dd ?
.intno: resd 1 .intno dd ?
;; by x86 hardware ;; by x86 hardware
.err: resd 1 .err dd ?
.eip: resd 1 .eip dd ?
.cs: resd 1 .cs dd ?
.eflags: resd 1 .eflags dd ?
;; crossring ;; crossring
.useresp: resd 1 .useresp dd ?
.ss: resd 1 .ss dd ?
endstruc }

10
kernel/syscall.inc Normal file
View file

@ -0,0 +1,10 @@
SYSCALL_EXIT = 0x01
SYSCALL_FORK = 0x02
isr_syscall:
push eax
mov esi, szMsgSyscall
call klog
iret
szMsgSyscall db "syscall %x", 0