aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/interrupts
diff options
context:
space:
mode:
authorMarvin Borner2020-06-17 18:31:46 +0200
committerMarvin Borner2020-06-17 18:31:46 +0200
commiteed77bd2970a00d1394ed027ceca5b646e4671ce (patch)
treec44643d98aed2b6818f2b33417c0dea9c5853094 /src/kernel/interrupts
parent49dfa1f4021026bf7c4d77817959c8aa24067016 (diff)
Started rewrite
Diffstat (limited to 'src/kernel/interrupts')
-rw-r--r--src/kernel/interrupts/idt.asm6
-rw-r--r--src/kernel/interrupts/idt.c48
-rw-r--r--src/kernel/interrupts/interrupts.h156
-rw-r--r--src/kernel/interrupts/irq.asm55
-rw-r--r--src/kernel/interrupts/irq.c97
-rw-r--r--src/kernel/interrupts/isr.asm80
-rw-r--r--src/kernel/interrupts/isr.c139
7 files changed, 0 insertions, 581 deletions
diff --git a/src/kernel/interrupts/idt.asm b/src/kernel/interrupts/idt.asm
deleted file mode 100644
index cec0e95..0000000
--- a/src/kernel/interrupts/idt.asm
+++ /dev/null
@@ -1,6 +0,0 @@
-; IDT loader
-global idt_load
-extern idtp
-idt_load:
- lidt [idtp]
- ret \ No newline at end of file
diff --git a/src/kernel/interrupts/idt.c b/src/kernel/interrupts/idt.c
deleted file mode 100644
index a01f1c1..0000000
--- a/src/kernel/interrupts/idt.c
+++ /dev/null
@@ -1,48 +0,0 @@
-#include <lib/lib.h>
-#include <system.h>
-
-struct idt_entry {
- u16 base_low;
- u16 sel; // Kernel segment
- u8 always0; // Always 0
- u8 flags;
- u16 base_high;
-} __attribute__((packed));
-
-struct idt_ptr {
- u16 limit;
- void *base;
-} __attribute__((packed));
-
-// Initialize IDT with 256 entries
-struct idt_entry idt[256];
-struct idt_ptr idtp;
-
-// Defined in idt.asm
-extern void idt_load();
-
-void idt_set_gate(u8 num, unsigned long base, u16 sel, u8 flags)
-{
- // Specify the interrupt routine's base address
- idt[num].base_low = (u16)(base & 0xFFFF);
- idt[num].base_high = (u16)((base >> 16) & 0xFFFF);
-
- // Set selector/segment of IDT entry
- idt[num].sel = sel;
- idt[num].always0 = 0;
- idt[num].flags = (u8)(flags | 0x60);
-}
-
-// Install IDT
-void idt_install()
-{
- // Set IDT pointer and limit
- idtp.limit = (sizeof(struct idt_entry) * 256) - 1;
- idtp.base = &idt;
-
- // Clear IDT by setting memory cells to 0
- memset(&idt, 0, sizeof(struct idt_entry) * 256);
-
- idt_load();
- info("Installed Interrupt Descriptor Table");
-} \ No newline at end of file
diff --git a/src/kernel/interrupts/interrupts.h b/src/kernel/interrupts/interrupts.h
deleted file mode 100644
index 12f8f0a..0000000
--- a/src/kernel/interrupts/interrupts.h
+++ /dev/null
@@ -1,156 +0,0 @@
-#ifndef MELVIX_INTERRUPTS_H
-#define MELVIX_INTERRUPTS_H
-
-#include <stddef.h>
-#include <stdint.h>
-
-/**
- * Initialize the Interrupt Descriptor Table with 256 entries
- */
-void idt_install();
-
-/**
- * Add new gate (Interrupt Service Routine) to the Interrupt Descriptor Table
- * @param num The index of the routine in the IDT
- * @param base The base address of the ISR
- * @param sel The kernel code segment (0x08)
- * @param flags The IDT access byte entry (P DPL 01110)
- */
-void idt_set_gate(u8 num, unsigned long base, u16 sel, u8 flags);
-
-/**
- * Registers that get passed into an IRQ handler
- */
-struct regs {
- u32 gs, fs, es, ds;
- u32 edi, esi, ebp, esp, ebx, edx, ecx, eax;
- u32 int_no, err_code;
- u32 eip, cs, eflags, useresp, ss;
-};
-
-/**
- * Install 32 exception ISRs into the IDT
- */
-void isrs_install();
-
-/**
- * Add a new Interrupt Request Handler
- * @param irq The index of the IRQ routine
- * @param handler The interrupt handler function
- */
-typedef void (*irq_handler_t)(struct regs *);
-
-void isr_install_handler(u32 isr, irq_handler_t handler);
-
-/**
- * Uninstall a handler by index
- * @param irq The index of the IRQ routine that should be removed
- */
-void isr_uninstall_handler(u32 isr);
-
-/**
- * Initialize the Interrupt Requests by mapping the ISRs to the correct
- * entries in the IDT (install the exception handlers)
- */
-void irq_install();
-
-/**
- * Add a new Interrupt Request Handler
- * @param irq The index of the IRQ routine
- * @param handler The interrupt handler function
- */
-void irq_install_handler(int irq, void (*handler)(struct regs *r));
-
-/**
- * Uninstall a handler by index
- * @param irq The index of the IRQ routine that should be removed
- */
-void irq_uninstall_handler(int irq);
-
-/**
- * Execute the handler of the IRQ
- * @param r The ISR that should be handled
- */
-void irq_handler(struct regs *r);
-
-/**
- * Check if an IRQ is installed
- * @param irq The index of the IRQ routine that should be checked
- * @return 1 if installed, 0 if not
- */
-int irq_is_installed(int irq);
-
-/**
- * Logs fault messages and panics
- * @param r The registers
- */
-void fault_handler(struct regs *r);
-
-// Defined in isr.asm
-extern void isr0();
-
-extern void isr1();
-
-extern void isr2();
-
-extern void isr3();
-
-extern void isr4();
-
-extern void isr5();
-
-extern void isr6();
-
-extern void isr7();
-
-extern void isr8();
-
-extern void isr9();
-
-extern void isr10();
-
-extern void isr11();
-
-extern void isr12();
-
-extern void isr13();
-
-extern void isr14();
-
-extern void isr15();
-
-extern void isr16();
-
-extern void isr17();
-
-extern void isr18();
-
-extern void isr19();
-
-extern void isr20();
-
-extern void isr21();
-
-extern void isr22();
-
-extern void isr23();
-
-extern void isr24();
-
-extern void isr25();
-
-extern void isr26();
-
-extern void isr27();
-
-extern void isr28();
-
-extern void isr29();
-
-extern void isr30();
-
-extern void isr31();
-
-extern void isr128();
-
-#endif \ No newline at end of file
diff --git a/src/kernel/interrupts/irq.asm b/src/kernel/interrupts/irq.asm
deleted file mode 100644
index da7323e..0000000
--- a/src/kernel/interrupts/irq.asm
+++ /dev/null
@@ -1,55 +0,0 @@
-%macro IRQ 2
- global irq%1
- irq%1:
- cli
- push byte 0
- push byte %2
- jmp irq_common_stub
-%endmacro
-
-IRQ 0, 32
-IRQ 1, 33
-IRQ 2, 34
-IRQ 3, 35
-IRQ 4, 36
-IRQ 5, 37
-IRQ 6, 38
-IRQ 7, 39
-IRQ 8, 40
-IRQ 9, 41
-IRQ 10, 42
-IRQ 11, 43
-IRQ 12, 44
-IRQ 13, 45
-IRQ 14, 46
-IRQ 15, 47
-
-extern irq_handler
-irq_common_stub:
- pusha
-
- push ds
- push es
- push fs
- push gs
-
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- cld
-
- push esp
- call irq_handler
- add esp, 4
-
- pop gs
- pop fs
- pop es
- pop ds
- popa
-
- add esp, 8
- sti
- iret \ No newline at end of file
diff --git a/src/kernel/interrupts/irq.c b/src/kernel/interrupts/irq.c
deleted file mode 100644
index 9fd0c31..0000000
--- a/src/kernel/interrupts/irq.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include <interrupts/interrupts.h>
-#include <io/io.h>
-#include <system.h>
-
-extern void irq0();
-extern void irq1();
-extern void irq2();
-extern void irq3();
-extern void irq4();
-extern void irq5();
-extern void irq6();
-extern void irq7();
-extern void irq8();
-extern void irq9();
-extern void irq10();
-extern void irq11();
-extern void irq12();
-extern void irq13();
-extern void irq14();
-extern void irq15();
-extern void irq128();
-
-// Array to handle custom IRQ handlers
-void *irq_routines[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-
-// Install custom IRQ handler
-void irq_install_handler(int irq, void (*handler)(struct regs *r))
-{
- irq_routines[irq] = handler;
-}
-
-// Removes the custom IRQ handler
-void irq_uninstall_handler(int irq)
-{
- irq_routines[irq] = 0;
-}
-
-int irq_is_installed(int irq)
-{
- return irq_routines[irq] != 0;
-}
-
-// Remap IRQs for protected mode compatibility via the PIC
-void irq_remap(void)
-{
- outb(0x20, 0x11);
- outb(0xA0, 0x11);
- outb(0x21, 0x20);
- outb(0xA1, 0x28);
- outb(0x21, 0x04);
- outb(0xA1, 0x02);
- outb(0x21, 0x01);
- outb(0xA1, 0x01);
- outb(0x21, 0x0);
- outb(0xA1, 0x0);
-}
-
-// Map ISRs to the correct entries in the IDT
-void irq_install()
-{
- irq_remap();
- idt_set_gate(32, (unsigned)irq0, 0x08, 0x8E);
- idt_set_gate(33, (unsigned)irq1, 0x08, 0x8E);
- idt_set_gate(34, (unsigned)irq2, 0x08, 0x8E);
- idt_set_gate(35, (unsigned)irq3, 0x08, 0x8E);
- idt_set_gate(36, (unsigned)irq4, 0x08, 0x8E);
- idt_set_gate(37, (unsigned)irq5, 0x08, 0x8E);
- idt_set_gate(38, (unsigned)irq6, 0x08, 0x8E);
- idt_set_gate(39, (unsigned)irq7, 0x08, 0x8E);
- idt_set_gate(40, (unsigned)irq8, 0x08, 0x8E);
- idt_set_gate(41, (unsigned)irq9, 0x08, 0x8E);
- idt_set_gate(42, (unsigned)irq10, 0x08, 0x8E);
- idt_set_gate(43, (unsigned)irq11, 0x08, 0x8E);
- idt_set_gate(44, (unsigned)irq12, 0x08, 0x8E);
- idt_set_gate(45, (unsigned)irq13, 0x08, 0x8E);
- idt_set_gate(46, (unsigned)irq14, 0x08, 0x8E);
- idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E);
- info("Installed Interrupt Requests");
-}
-
-// Handle IRQ ISRs
-void irq_handler(struct regs *r)
-{
- void (*handler)(struct regs * r);
-
- // Execute custom handler if exists
- handler = irq_routines[r->int_no - 32];
- if (handler)
- handler(r);
-
- // Send end of interrupt to second (slave) IRQ controller
- if (r->int_no >= 40)
- outb(0xA0, 0x20);
-
- // Send end of interrupt to master interrupt controller
- outb(0x20, 0x20);
-} \ No newline at end of file
diff --git a/src/kernel/interrupts/isr.asm b/src/kernel/interrupts/isr.asm
deleted file mode 100644
index 7bfa876..0000000
--- a/src/kernel/interrupts/isr.asm
+++ /dev/null
@@ -1,80 +0,0 @@
-%macro ISR_NOERRCODE 1
- global isr%1
- isr%1:
- cli
- push byte 0
- push %1
- jmp isr_common_stub
-%endmacro
-
-%macro ISR_ERRCODE 1
- global isr%1
- isr%1:
- cli
- push byte %1
- jmp isr_common_stub
-%endmacro
-
-ISR_NOERRCODE 0
-ISR_NOERRCODE 1
-ISR_NOERRCODE 2
-ISR_NOERRCODE 3
-ISR_NOERRCODE 4
-ISR_NOERRCODE 5
-ISR_NOERRCODE 6
-ISR_NOERRCODE 7
-ISR_ERRCODE 8
-ISR_NOERRCODE 9
-ISR_ERRCODE 10
-ISR_ERRCODE 11
-ISR_ERRCODE 12
-ISR_ERRCODE 13
-ISR_ERRCODE 14
-ISR_NOERRCODE 15
-ISR_NOERRCODE 16
-ISR_NOERRCODE 17
-ISR_NOERRCODE 18
-ISR_NOERRCODE 19
-ISR_NOERRCODE 20
-ISR_NOERRCODE 21
-ISR_NOERRCODE 22
-ISR_NOERRCODE 23
-ISR_NOERRCODE 24
-ISR_NOERRCODE 25
-ISR_NOERRCODE 26
-ISR_NOERRCODE 27
-ISR_NOERRCODE 28
-ISR_NOERRCODE 29
-ISR_NOERRCODE 30
-ISR_NOERRCODE 31
-ISR_NOERRCODE 128
-
-extern fault_handler
-isr_common_stub:
- pusha
-
- push ds
- push es
- push fs
- push gs
-
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- cld
-
- push esp
- call fault_handler
- add esp, 4
-
- pop gs
- pop fs
- pop es
- pop ds
- popa
-
- add esp, 8
- sti
- iret \ No newline at end of file
diff --git a/src/kernel/interrupts/isr.c b/src/kernel/interrupts/isr.c
deleted file mode 100644
index ae72a0f..0000000
--- a/src/kernel/interrupts/isr.c
+++ /dev/null
@@ -1,139 +0,0 @@
-#include <graphics/vesa.h>
-#include <interrupts/interrupts.h>
-#include <io/io.h>
-#include <lib/lib.h>
-#include <lib/stdio.h>
-#include <lib/string.h>
-#include <stdint.h>
-#include <system.h>
-#include <tasks/process.h>
-
-// Install ISRs in IDT
-void isrs_install()
-{
- idt_set_gate(0, (unsigned)isr0, 0x08, 0x8E);
- idt_set_gate(1, (unsigned)isr1, 0x08, 0x8E);
- idt_set_gate(2, (unsigned)isr2, 0x08, 0x8E);
- idt_set_gate(3, (unsigned)isr3, 0x08, 0x8E);
- idt_set_gate(4, (unsigned)isr4, 0x08, 0x8E);
- idt_set_gate(5, (unsigned)isr5, 0x08, 0x8E);
- idt_set_gate(6, (unsigned)isr6, 0x08, 0x8E);
- idt_set_gate(7, (unsigned)isr7, 0x08, 0x8E);
-
- idt_set_gate(8, (unsigned)isr8, 0x08, 0x8E);
- idt_set_gate(9, (unsigned)isr9, 0x08, 0x8E);
- idt_set_gate(10, (unsigned)isr10, 0x08, 0x8E);
- idt_set_gate(11, (unsigned)isr11, 0x08, 0x8E);
- idt_set_gate(12, (unsigned)isr12, 0x08, 0x8E);
- idt_set_gate(13, (unsigned)isr13, 0x08, 0x8E);
- idt_set_gate(14, (unsigned)isr14, 0x08, 0x8E);
- idt_set_gate(15, (unsigned)isr15, 0x08, 0x8E);
-
- idt_set_gate(16, (unsigned)isr16, 0x08, 0x8E);
- idt_set_gate(17, (unsigned)isr17, 0x08, 0x8E);
- idt_set_gate(18, (unsigned)isr18, 0x08, 0x8E);
- idt_set_gate(19, (unsigned)isr19, 0x08, 0x8E);
- idt_set_gate(20, (unsigned)isr20, 0x08, 0x8E);
- idt_set_gate(21, (unsigned)isr21, 0x08, 0x8E);
- idt_set_gate(22, (unsigned)isr22, 0x08, 0x8E);
- idt_set_gate(23, (unsigned)isr23, 0x08, 0x8E);
-
- idt_set_gate(24, (unsigned)isr24, 0x08, 0x8E);
- idt_set_gate(25, (unsigned)isr25, 0x08, 0x8E);
- idt_set_gate(26, (unsigned)isr26, 0x08, 0x8E);
- idt_set_gate(27, (unsigned)isr27, 0x08, 0x8E);
- idt_set_gate(28, (unsigned)isr28, 0x08, 0x8E);
- idt_set_gate(29, (unsigned)isr29, 0x08, 0x8E);
- idt_set_gate(30, (unsigned)isr30, 0x08, 0x8E);
- idt_set_gate(31, (unsigned)isr31, 0x08, 0x8E);
-
- idt_set_gate(0x80, (unsigned)isr128, 0x08, 0x8E);
-
- info("Installed Interrupt Service Routines");
-}
-
-irq_handler_t isr_routines[256] = { 0 };
-
-// Install custom IRQ handler
-void isr_install_handler(u32 isr, irq_handler_t handler)
-{
- isr_routines[isr] = handler;
-}
-
-// Remove the custom IRQ handler
-void isr_uninstall_handler(u32 isr)
-{
- isr_routines[isr] = 0;
-}
-
-// Error exception messages
-const char *exception_messages[32] = { "Division By Zero",
- "Debug",
- "Non Maskable Interrupt",
- "Breakpoint",
- "Into Detected Overflow",
- "Out of Bounds",
- "Invalid Opcode",
- "No Coprocessor",
-
- "Double Fault",
- "Coprocessor Segment Overrun",
- "Bad TSS",
- "Segment Not Present",
- "Stack Fault",
- "General Protection Fault",
- "Page Fault",
- "Unknown Interrupt",
-
- "Coprocessor Fault",
- "Alignment Check",
- "Machine Check",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
-
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved",
- "Reserved" };
-
-// Master exception/interrupt/fault handler - called by asm
-void fault_handler(struct regs *r)
-{
- irq_handler_t handler = isr_routines[r->int_no];
- if (handler) {
- handler(r);
- } else {
- u32 faulting_address;
- asm("mov %%cr2, %0" : "=r"(faulting_address));
-
- log("\n[DEBUG]\nEIP: 0x%x\nEAX: 0x%x\nEBX: 0x%x\nECX: 0x%x\nEDX: 0x%x\nESP: 0x%x\nFault addr: 0x%x\nErr flag: 0x%x\nErr code: 0x%x\nINT code: 0x%x\nINT msg: %s",
- r->eip, r->eax, r->ebx, r->ecx, r->edx, r->esp, faulting_address, r->eflags,
- r->err_code, r->int_no, exception_messages[r->int_no]);
-
- char message[128];
- if (r->int_no <= 32) {
- strcpy(message, (char *)exception_messages[r->int_no]);
- strcat(message, " Exception");
- } else {
- strcpy(message, "Unknown Exception");
- }
-
- if (current_proc != NULL) {
- warn("%s: Suspending process %s with ID %d", message, current_proc->name,
- current_proc->pid);
- memcpy(&current_proc->regs, r, sizeof(struct regs));
- process_suspend(current_proc->pid);
- process_force_switch();
- } else {
- debug(RED "%s before multitasking started!" RES, message);
- halt_loop();
- }
- }
-} \ No newline at end of file