diff options
author | Danny Holman <dholman@gymli.org> | 2024-02-24 14:44:38 -0600 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2024-02-24 14:44:38 -0600 |
commit | 2ce0f8af51dae9e7d591ff5fd038f89d6ca9dbbe (patch) | |
tree | 314f6105fc2996923070ed7f82c3acc8d5b2b20c /arch/i386/boot | |
parent | 5d06824289868c5a345fbcfa8ed4d1e63af84fdb (diff) |
arch: i386: cleanup everything and reorganize
Clean up everything in the i386 arch directory. This code has been in
dire need of refactoring for a long while. All the inline assembly
functions and the data structures related to the architecture should be
placed into their own header file. Now the scheduler can access
registers and ISRs without having to deal with arch-specific code.
Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'arch/i386/boot')
-rw-r--r-- | arch/i386/boot/gdt.c | 58 | ||||
-rw-r--r-- | arch/i386/boot/idt.c | 162 | ||||
-rw-r--r-- | arch/i386/boot/isr.s | 129 |
3 files changed, 0 insertions, 349 deletions
diff --git a/arch/i386/boot/gdt.c b/arch/i386/boot/gdt.c deleted file mode 100644 index c0cf127..0000000 --- a/arch/i386/boot/gdt.c +++ /dev/null @@ -1,58 +0,0 @@ -#include <kernel/gdt.h> -#include <kernel/asm.h> -#include <kernel/string.h> - -struct gdt_entry desc[6]; -struct gdt_ptr gp; -struct tss_entry tss_entry; - -void gdt_set_gate(int num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) { - desc[num].base_low = (base & 0xFFFF); - desc[num].base_middle = (base >> 16) & 0xFF; - desc[num].base_high = (base >> 24) & 0xFF; - - desc[num].limit_low = (limit & 0xFFFF); - desc[num].gran = (limit >> 16) & 0x0F; - desc[num].gran |= (gran & 0xF0); - desc[num].access = access; -} - -void write_tss(int num, uint32_t ss0, uint32_t esp0) { - uint32_t base = (uint32_t)&tss_entry; - uint32_t limit = base + sizeof(struct tss_entry); - - gdt_set_gate(num, base, limit, 0x89, 0x00); - memset(&tss_entry, 0x0, sizeof(tss_entry)); - - tss_entry.ss0 = ss0; - tss_entry.esp0 = esp0; - tss_entry.cs = 0x0B; - tss_entry.ss = 0x13; - tss_entry.ds = 0x13; - tss_entry.es = 0x13; - tss_entry.fs = 0x13; - tss_entry.gs = 0x13; - tss_entry.iomap_base = sizeof(tss_entry); -} - -void set_kernel_esp(uint32_t esp) { - tss_entry.esp0 = esp; -} - -void gdt_install(void) { - gp.limit = (sizeof(struct gdt_entry) * 6) - 1; - gp.base = (uint32_t)&desc; - - gdt_set_gate(0, 0, 0, 0, 0); - gdt_set_gate(1, 0, 0xFFFFF, 0x9A, 0xCF); - gdt_set_gate(2, 0, 0xFFFFF, 0x92, 0xCF); - gdt_set_gate(3, 0, 0xFFFFF, 0xFA, 0xCF); - gdt_set_gate(4, 0, 0xFFFFF, 0xF2, 0xCF); - - uint32_t esp; - __asm__ volatile("movl %%esp, %0" : "=r"(esp)); - write_tss(5, 0x10, esp); - - flush_gdt(); - flush_tss(); -} diff --git a/arch/i386/boot/idt.c b/arch/i386/boot/idt.c deleted file mode 100644 index 7b9401d..0000000 --- a/arch/i386/boot/idt.c +++ /dev/null @@ -1,162 +0,0 @@ -#include <kernel/syscall.h> -#include <kernel/panic.h> -#include <kernel/gdt.h> -#include <kernel/isr.h> -#include <kernel/io.h> -#include <kernel/pic.h> -#include <kernel/string.h> -#include <kernel/paging.h> -#include <stdint.h> - -__attribute__((aligned(0x10))) -struct idt_entry idt[256]; -struct idt_ptr idtr; - -const char* exceptions[] = { - "Division by zero", - "Debug", - "Non-maskable interrupt", - "Breakpoint", - "Overflow", - "Out-of-bounds", - "Invalid opcode", - "FPU not available", - "Double fault", - "RESERVED", - "Invalid TSS", - "Segment not present", - "Stack fault", - "General protection fault", - "Page fault", - "RESERVED", - "FPU exception", - "Alignment check", - "Machine check", - "FPU-SIMD exception", - "Virtualization exception", - "Control protection" - "RESERVED", - "Hypervisor injection", - "VMM exception", - "Security exception", - "RESERVED", - "Triple fault", - "RESERVED" -}; - -__attribute__((noreturn)) -void halt_catch_fire(struct isr_frame *frame) { - dump_reg(frame); - kprintf("ERRNO=%x\n", frame->errno); - __asm__ volatile("cli;hlt"); - while (1); -} - -void exception_handler(struct isr_frame *frame) { - switch (frame->vector) { - case 0x00: - panic("Division by zero in kernel address space"); - halt_catch_fire(frame); - case 0x06: - panic("Invalid opcode in kernel address space"); - halt_catch_fire(frame); - case 0x08: - panic("Double fault in interrupt handler"); - halt_catch_fire(frame); - case 0x0D: - panic("Protection fault in kernel address space"); - halt_catch_fire(frame); - case 0x0E: - page_fault_handler(frame); - break; - default: - panic("Unhandled exception"); - halt_catch_fire(frame); - } -} - -void interrupt_handler(struct isr_frame frame) { - if (frame.vector < 32) { - exception_handler(&frame); - } else if (frame.vector < 48) { - irq_dispatch(&frame); - } else { - switch (frame.vector) { - case 0x80: - handle_syscall(&frame); - break; - default: - panic("Unmapped interrupt"); - halt_catch_fire(&frame); - __asm__ volatile("cli;hlt"); - } - } -} - -void idt_set_gate(uint8_t num, void (*handler)(void), uint16_t cs, uint8_t flags) { - struct idt_entry *desc = &idt[num]; - desc->isr_low = (uint16_t)(((uint32_t)handler >> 0) & 0xFFFF); - desc->isr_high = (uint16_t)(((uint32_t)handler >> 16) & 0xFFFF); - desc->reserved = 0; - desc->kernel_cs = cs; - desc->flags = flags; -} - -void idt_install(void) { - idtr.limit = (uint16_t)sizeof(struct idt_entry) * 256 - 1; - idtr.base = (uint32_t)idt; - - idt_set_gate(0x0, isr_stub_0, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1, isr_stub_1, 0x08, IDT_EXCEPTION); - idt_set_gate(0x2, isr_stub_2, 0x08, IDT_EXCEPTION); - idt_set_gate(0x3, isr_stub_3, 0x08, IDT_EXCEPTION); - idt_set_gate(0x4, isr_stub_4, 0x08, IDT_EXCEPTION); - idt_set_gate(0x5, isr_stub_5, 0x08, IDT_EXCEPTION); - idt_set_gate(0x6, isr_stub_6, 0x08, IDT_EXCEPTION); - idt_set_gate(0x7, isr_stub_7, 0x08, IDT_EXCEPTION); - idt_set_gate(0x8, isr_stub_8, 0x08, IDT_EXCEPTION); - idt_set_gate(0x9, isr_stub_9, 0x08, IDT_EXCEPTION); - idt_set_gate(0xA, isr_stub_10, 0x08, IDT_EXCEPTION); - idt_set_gate(0xB, isr_stub_11, 0x08, IDT_EXCEPTION); - idt_set_gate(0xC, isr_stub_12, 0x08, IDT_EXCEPTION); - idt_set_gate(0xD, isr_stub_13, 0x08, IDT_EXCEPTION); - idt_set_gate(0xE, isr_stub_14, 0x08, IDT_EXCEPTION); - idt_set_gate(0xF, isr_stub_15, 0x08, IDT_EXCEPTION); - idt_set_gate(0x10, isr_stub_16, 0x08, IDT_EXCEPTION); - idt_set_gate(0x11, isr_stub_17, 0x08, IDT_EXCEPTION); - idt_set_gate(0x12, isr_stub_18, 0x08, IDT_EXCEPTION); - idt_set_gate(0x13, isr_stub_19, 0x08, IDT_EXCEPTION); - idt_set_gate(0x14, isr_stub_20, 0x08, IDT_EXCEPTION); - idt_set_gate(0x15, isr_stub_21, 0x08, IDT_EXCEPTION); - idt_set_gate(0x16, isr_stub_22, 0x08, IDT_EXCEPTION); - idt_set_gate(0x17, isr_stub_23, 0x08, IDT_EXCEPTION); - idt_set_gate(0x18, isr_stub_24, 0x08, IDT_EXCEPTION); - idt_set_gate(0x19, isr_stub_25, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1A, isr_stub_26, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1B, isr_stub_27, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1C, isr_stub_28, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1D, isr_stub_29, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1E, isr_stub_30, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1F, isr_stub_31, 0x08, IDT_EXCEPTION); - - idt_set_gate(0x20, irq_stub_0, 0x08, IDT_INTERRUPT); - idt_set_gate(0x21, irq_stub_1, 0x08, IDT_INTERRUPT); - idt_set_gate(0x22, irq_stub_2, 0x08, IDT_INTERRUPT); - idt_set_gate(0x23, irq_stub_3, 0x08, IDT_INTERRUPT); - idt_set_gate(0x24, irq_stub_4, 0x08, IDT_INTERRUPT); - idt_set_gate(0x25, irq_stub_5, 0x08, IDT_INTERRUPT); - idt_set_gate(0x26, irq_stub_6, 0x08, IDT_INTERRUPT); - idt_set_gate(0x27, irq_stub_7, 0x08, IDT_INTERRUPT); - idt_set_gate(0x28, irq_stub_8, 0x08, IDT_INTERRUPT); - idt_set_gate(0x29, irq_stub_9, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2A, irq_stub_10, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2B, irq_stub_11, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2C, irq_stub_12, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2D, irq_stub_13, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2E, irq_stub_14, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2F, irq_stub_15, 0x08, IDT_INTERRUPT); - - idt_set_gate(0x80, syscall_stub, 0x08, IDT_INTERRUPT); - - __asm__ volatile("lidt %0" : : "memory"(idtr)); -} diff --git a/arch/i386/boot/isr.s b/arch/i386/boot/isr.s deleted file mode 100644 index 55db35e..0000000 --- a/arch/i386/boot/isr.s +++ /dev/null @@ -1,129 +0,0 @@ -.section .text - -.macro isr_err_stub num -.global isr_stub_\num -.type isr_stub_\num, @function -isr_stub_\num: - cli - pushl $\num - jmp isr_frame_asm -.endm -.macro isr_no_err_stub num -.global isr_stub_\num -.type isr_stub_\num, @function -isr_stub_\num: - cli - pushl $0 - pushl $\num - jmp isr_frame_asm -.endm - -.macro irq_stub num -.global irq_stub_\num -.type irq_stub_\num, @function -irq_stub_\num: - cli - pushl $0 - pushl $\num+32 - jmp isr_frame_asm -.endm - -.global syscall_stub -syscall_stub: - cli - pushl $0 - pushl $0x80 - jmp isr_frame_asm - -isr_frame_asm: - pushl %eax - pushl %ebx - pushl %ecx - pushl %edx - pushl %esi - pushl %edi - - movl %cr0, %eax - pushl %eax - movl %cr2, %eax - pushl %eax - movl %cr3, %eax - pushl %eax - movl %cr4, %eax - pushl %eax - - pushl %esp - call set_kernel_esp - addl $4, %esp - - cld - call interrupt_handler - - popl %eax - movl %eax, %cr4 - popl %eax - movl %eax, %cr3 - popl %eax - movl %eax, %cr2 - popl %eax - movl %eax, %cr0 - - popl %edi - popl %esi - popl %edx - popl %ecx - popl %ebx - popl %eax - addl $8, %esp - - iret - -isr_no_err_stub 0 -isr_no_err_stub 1 -isr_no_err_stub 2 -isr_no_err_stub 3 -isr_no_err_stub 4 -isr_no_err_stub 5 -isr_no_err_stub 6 -isr_no_err_stub 7 -isr_err_stub 8 -isr_no_err_stub 9 -isr_err_stub 10 -isr_err_stub 11 -isr_err_stub 12 -isr_err_stub 13 -isr_err_stub 14 -isr_no_err_stub 15 -isr_no_err_stub 16 -isr_err_stub 17 -isr_no_err_stub 18 -isr_no_err_stub 19 -isr_no_err_stub 20 -isr_err_stub 21 -isr_no_err_stub 22 -isr_no_err_stub 23 -isr_no_err_stub 24 -isr_no_err_stub 25 -isr_no_err_stub 26 -isr_no_err_stub 27 -isr_no_err_stub 28 -isr_err_stub 29 -isr_err_stub 30 -isr_no_err_stub 31 - -irq_stub 0 -irq_stub 1 -irq_stub 2 -irq_stub 3 -irq_stub 4 -irq_stub 5 -irq_stub 6 -irq_stub 7 -irq_stub 8 -irq_stub 9 -irq_stub 10 -irq_stub 11 -irq_stub 12 -irq_stub 13 -irq_stub 14 -irq_stub 15 |