Skip to content

Commit

Permalink
refactor: rework IDT and GDT
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Jul 13, 2023
1 parent 08a7d5c commit 32f1956
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 79 deletions.
6 changes: 3 additions & 3 deletions docs/config/Menu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions kernel/Makefile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
24 changes: 13 additions & 11 deletions kernel/gdt.s → kernel/boot/gdt.s
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
10 changes: 10 additions & 0 deletions kernel/boot/head.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions kernel/idt.s → kernel/boot/idt.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
; file: idt.s
;
;; File: idt.s
;;
[BITS 32]

%include "sys/i386/cpu.inc"

section .text

idt_set_table:
Expand All @@ -15,22 +18,22 @@ 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

global idt_setup
idt_setup:
%assign i 0
%rep 256
%rep 32
push dword i
call idt_set_table
add esp, 4
Expand All @@ -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:
2 changes: 1 addition & 1 deletion kernel/isr.s → kernel/boot/isr.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[BITS 32]

%include "cpu.inc"
%include "sys/i386/cpu.inc"
%include "base.inc"

%macro ISR_NO_ERR 1
Expand Down
44 changes: 44 additions & 0 deletions kernel/boot/tss.s
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions kernel/kernel.s
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ 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

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 changes: 70 additions & 0 deletions kernel/sys/i386/cpu.inc
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions kernel/sys/i386/mmu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 2 additions & 48 deletions kernel/cpu.inc → kernel/sys/i386/task.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;; File: cpu.inc

;; File: task.inc
;;

;; Structure: tss
;;
Expand Down Expand Up @@ -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

0 comments on commit 32f1956

Please sign in to comment.