refactor: rework IDT and GDT

This commit is contained in:
d0p1 🏳️‍⚧️ 2023-07-13 16:00:20 +02:00
parent 08a7d5c975
commit 32f1956ca2
11 changed files with 175 additions and 79 deletions

View file

@ -50,12 +50,12 @@ Group: Kernel {
Group: boot {
File: gdt.s (kernel/boot/gdt.s)
File: head.s (kernel/boot/head.s)
File: idt.s (kernel/boot/idt.s)
} # Group: boot
File: kernel.s (kernel/kernel.s)
File: gdt.s (kernel/gdt.s)
File: idt.s (kernel/idt.s)
File: pic.s (kernel/pic.s)
Group: dev {
@ -83,6 +83,7 @@ Group: Kernel {
Group: i386 {
File: cpu.inc (kernel/sys/i386/cpu.inc)
File: cpuid.inc (kernel/sys/i386/cpuid.inc)
File: mmu.inc (kernel/sys/i386/mmu.inc)
File: registers.inc (kernel/sys/i386/registers.inc)
@ -92,7 +93,6 @@ Group: Kernel {
File: vm.inc (kernel/vm/vm.inc)
File: pmap.s (kernel/vm/pmap.s)
File: cpu.inc (kernel/cpu.inc)
} # Group: Kernel
Group: Lib {

View file

@ -1,9 +1,13 @@
KERNEL = vmstupid
SRCS = boot/head.s \
kernel.s gdt.s pic.s isr.s idt.s \
boot/gdt.s \
boot/idt.s \
boot/isr.s \
boot/tss.s \
kernel.s \
lib/log.s dev/at/serial.s \
i18n/msg_en.s \
base/console.s
i18n/msg_en.s
INCS = sys/multiboot.inc \
sys/i386/cpuid.inc \
sys/i386/mmu.inc \

View file

@ -1,13 +1,11 @@
; file: gdt.s
;
;
;; File: gdt.s
;;
[BITS 32]
%include "cpu.inc"
%include "sys/i386/mmu.inc"
section .text
; Function: gdt_setup
;
; in:
@ -18,6 +16,14 @@ section .text
;
global gdt_setup
gdt_setup:
;; install tss
lea eax, gdt_entries.tss
push eax
extern tss_install
call tss_install
pop eax
;; setup gdt
lgdt [gdt_ptr]
mov eax, cr0
or al, 1
@ -83,10 +89,6 @@ gdt_entries:
at gdt_entry.base_high, db 0x00
iend
;;.tss:
;; TSS
;;istruc gdt_entry
;; at gdt_entry.access, db 0x89
;; at gdt_entry.flags, db 0x0
;;iend
.tss:
times gdt_entry_size db 0
.end:

View file

@ -96,6 +96,16 @@ entry_high:
mov esp, stack_top
xor ebp, ebp
;; Setup GDT: we "disable" segmentation
extern gdt_setup
call gdt_setup
extern tss_flush
call tss_flush
extern idt_setup
call idt_setup
push esi ; multiboot struct
push edi ; multiboot magic
extern kmain

View file

@ -1,6 +1,9 @@
; file: idt.s
;
;; File: idt.s
;;
[BITS 32]
%include "sys/i386/cpu.inc"
section .text
idt_set_table:
@ -15,14 +18,14 @@ idt_set_table:
; offset (low)
mov word [idt_entries + (ecx * 8)], ax
; segment selector (kernel code)
mov word [idt_entries + (ecx * 8) + 2], 0x08
mov word [idt_entries + (ecx * 8) + idt_gate.selector], 0x08
; zero (skip)
; attr: 1 (Present) 00 (DPL) 0 1 (D: 32bits) 110
mov byte [idt_entries + (ecx * 8) + 5], 0x8E
mov byte [idt_entries + (ecx * 8) + idt_gate.attributes], 0x8E
; offset (high)
shr eax, 16
mov word [idt_entries + (ecx * 8) + 6], ax
mov word [idt_entries + (ecx * 8) + idt_gate.offset_high], ax
leave
ret
@ -30,7 +33,7 @@ idt_set_table:
global idt_setup
idt_setup:
%assign i 0
%rep 256
%rep 32
push dword i
call idt_set_table
add esp, 4
@ -51,9 +54,4 @@ idt_ptr:
align 8
idt_entries:
times 256 dd 0x00000000, 0x00000000
;; dw offset (low)
;; dw segment selector
;; db zero
;; db attr | P | DPL | 0 D 1 1 0 |
;; dw offset (high)
.end:

View file

@ -1,6 +1,6 @@
[BITS 32]
%include "cpu.inc"
%include "sys/i386/cpu.inc"
%include "base.inc"
%macro ISR_NO_ERR 1

44
kernel/boot/tss.s Normal file
View file

@ -0,0 +1,44 @@
%include "sys/i386/mmu.inc"
%include "sys/i386/task.inc"
section .text
global tss_install
tss_install:
push ebp
mov ebp, esp
push esi
mov esi, [ebp + 8]
mov eax, tss_size
lea ebx, tss_entry
mov [esi + gdt_entry.limit_low], ax
mov [esi + gdt_entry.base_low], bx
shr eax, 16
mov [esi + gdt_entry.base_mid], al
mov al, 0x9 | (1 << 7)
mov [esi + gdt_entry.access], al
shr ebx, 16
and bl, 0xF
mov [esi + gdt_entry.flags], bl
mov [esi + gdt_entry.base_high], ah
mov dword [tss_entry + tss.ss0], 0x10
extern stack_top
mov dword [tss_entry + tss.esp0], stack_top
leave
ret
global tss_flush
tss_flush:
mov ax, (5 * 8) | 0
ltr ax
ret
section .data
tss_entry:
times tss_size db 0

View file

@ -16,11 +16,17 @@ global kmain
kmain:
push ebp
mov ebp, esp
;; TODO: console init
extern serial_init
call serial_init
LOG msg_hello_world
LOG msg_print_boot, esi, edi
int3
;; setup cpu
;; initialize vm
leave
ret
@ -28,5 +34,5 @@ kmain:
section .rodata
msg_hello_world db "StupidOS v", STUPID_VERSION, " (built with ", __NASM_VER__, " on ", __DATE__, " ", __TIME__, ")", 0
msg_print_boot db "ESI: %x | EDI: %x", 0
file db __FILE__, 0

70
kernel/sys/i386/cpu.inc Normal file
View file

@ -0,0 +1,70 @@
;; File: cpu.inc
;; Structure: idt_gate
;; .offset_low - TODO
;; .selector - TODO
;; .zero - TODO
;; .attributes - TODO
;; .offset_high - TODO
;;
struc idt_gate
.offset_low: resw 1
.selector: resw 1
.zero: resb 1
.attributes: resb 1
.offset_high: resw 1
endstruc
;; About: Gates
;; - Task Gate
;; > 31 23 15 7 0
;; > +----------------|----------------+-----------------|-----------------+
;; > | (NOT USED) | P DPL 0 0 1 0 1 (NOT USED) |
;; > +----------------|----------------+-----------------|-----------------+
;; > | SELECTOR | (NOT USED) |
;; > +----------------|----------------+-----------------|-----------------+
;;
;; - Interrupt Gate
;; > 31 23 15 7 0
;; > +----------------|----------------+-----------------|-----------------+
;; > | OFFSET 31..16 | P DPL 0 1 1 1 0 0 0 0 0 0 0 0 0 |
;; > +----------------|----------------+-----------------|-----------------+
;; > | SELECTOR | OFFSET 15..0 |
;; > +--------------- |----------------+-----------------|-----------------+
;;
;; - Trap Gate
;; > 31 23 15 7 0
;; > +----------------|----------------+-----------------|-----------------+
;; > | OFFSET 31..16 | P DPL 0 1 1 1 1 0 0 0 0 0 0 0 0 |
;; > +----------------|----------------+-----------------|-----------------+
;; > | SELECTOR | OFFSET 15..0 |
;; > +--------------- |----------------+-----------------|-----------------+
struc intframe
;; registers
.edi: resd 1
.esi: resd 1
.ebp: resd 1
.esp: resd 1
.ebx: resd 1
.edx: resd 1
.ecx: resd 1
.eax: resd 1
;;
.gs: resd 1
.fs: resd 1
.es: resd 1
.ds: resd 1
.intno: resd 1
;; by x86 hardware
.err: resd 1
.eip: resd 1
.cs: resd 1
.eflags: resd 1
;; crossring
.useresp: resd 1
.ss: resd 1
endstruc

View file

@ -81,3 +81,11 @@ PTE_D equ 1 << 6
PTE_PAT equ 1 << 7
PTE_G equ 1 << 8
struc gdt_entry
.limit_low: resw 1
.base_low: resw 1
.base_mid: resb 1
.access: resb 1
.flags: resb 1
.base_high: resb 1
endstruc

View file

@ -1,5 +1,5 @@
;; File: cpu.inc
;; File: task.inc
;;
;; Structure: tss
;;
@ -97,49 +97,3 @@ struc tss
.iopb: resw 1
resw 1
endstruc
struc gdt_entry
.limit_low: resw 1
.base_low: resw 1
.base_mid: resb 1
.access: resb 1
.flags: resb 1
.base_high: resb 1
endstruc
struc idt_entry
.limit_low: resw 1
.selector: resw 1
.zero: resb 1
.attr: resb 1
.base_high: resw 1
endstruc
struc intframe
;; registers
.edi: resd 1
.esi: resd 1
.ebp: resd 1
.esp: resd 1
.ebx: resd 1
.edx: resd 1
.ecx: resd 1
.eax: resd 1
;;
.gs: resd 1
.fs: resd 1
.es: resd 1
.ds: resd 1
.intno: resd 1
;; by x86 hardware
.err: resd 1
.eip: resd 1
.cs: resd 1
.eflags: resd 1
;; crossring
.useresp: resd 1
.ss: resd 1
endstruc