From 32f1956ca2894866ac4df47084e9f352ceb38bc6 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Thu, 13 Jul 2023 16:00:20 +0200 Subject: [PATCH] refactor: rework IDT and GDT --- docs/config/Menu.txt | 6 +-- kernel/Makefile | 10 ++-- kernel/{ => boot}/gdt.s | 24 ++++----- kernel/boot/head.s | 10 ++++ kernel/{ => boot}/idt.s | 20 ++++---- kernel/{ => boot}/isr.s | 2 +- kernel/boot/tss.s | 44 +++++++++++++++++ kernel/kernel.s | 10 +++- kernel/sys/i386/cpu.inc | 70 +++++++++++++++++++++++++++ kernel/sys/i386/mmu.inc | 8 +++ kernel/{cpu.inc => sys/i386/task.inc} | 50 +------------------ 11 files changed, 175 insertions(+), 79 deletions(-) rename kernel/{ => boot}/gdt.s (88%) rename kernel/{ => boot}/idt.s (70%) rename kernel/{ => boot}/isr.s (98%) create mode 100644 kernel/boot/tss.s create mode 100644 kernel/sys/i386/cpu.inc rename kernel/{cpu.inc => sys/i386/task.inc} (85%) diff --git a/docs/config/Menu.txt b/docs/config/Menu.txt index 2fda16b..d05a502 100644 --- a/docs/config/Menu.txt +++ b/docs/config/Menu.txt @@ -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 { diff --git a/kernel/Makefile b/kernel/Makefile index ce08cbf..a7bf698 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -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 \ diff --git a/kernel/gdt.s b/kernel/boot/gdt.s similarity index 88% rename from kernel/gdt.s rename to kernel/boot/gdt.s index ef0cb9c..f5d02d7 100644 --- a/kernel/gdt.s +++ b/kernel/boot/gdt.s @@ -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: diff --git a/kernel/boot/head.s b/kernel/boot/head.s index 0d7ccb5..00da87e 100644 --- a/kernel/boot/head.s +++ b/kernel/boot/head.s @@ -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 diff --git a/kernel/idt.s b/kernel/boot/idt.s similarity index 70% rename from kernel/idt.s rename to kernel/boot/idt.s index c510f0d..d41b737 100644 --- a/kernel/idt.s +++ b/kernel/boot/idt.s @@ -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: diff --git a/kernel/isr.s b/kernel/boot/isr.s similarity index 98% rename from kernel/isr.s rename to kernel/boot/isr.s index a392dbd..8e47cb8 100644 --- a/kernel/isr.s +++ b/kernel/boot/isr.s @@ -1,6 +1,6 @@ [BITS 32] -%include "cpu.inc" +%include "sys/i386/cpu.inc" %include "base.inc" %macro ISR_NO_ERR 1 diff --git a/kernel/boot/tss.s b/kernel/boot/tss.s new file mode 100644 index 0000000..e20172e --- /dev/null +++ b/kernel/boot/tss.s @@ -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 diff --git a/kernel/kernel.s b/kernel/kernel.s index f903317..edba150 100644 --- a/kernel/kernel.s +++ b/kernel/kernel.s @@ -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 diff --git a/kernel/sys/i386/cpu.inc b/kernel/sys/i386/cpu.inc new file mode 100644 index 0000000..063d4c9 --- /dev/null +++ b/kernel/sys/i386/cpu.inc @@ -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 diff --git a/kernel/sys/i386/mmu.inc b/kernel/sys/i386/mmu.inc index c86fbb0..3dec584 100644 --- a/kernel/sys/i386/mmu.inc +++ b/kernel/sys/i386/mmu.inc @@ -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 diff --git a/kernel/cpu.inc b/kernel/sys/i386/task.inc similarity index 85% rename from kernel/cpu.inc rename to kernel/sys/i386/task.inc index daeaf18..0cbaa01 100644 --- a/kernel/cpu.inc +++ b/kernel/sys/i386/task.inc @@ -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