319 lines
6.7 KiB
C
319 lines
6.7 KiB
C
#include "../include/idt.h"
|
|
#include "../include/isr.h"
|
|
#include "../include/types.h"
|
|
#include "../include/pic.h"
|
|
#include "../include/pit.h"
|
|
#include "../include/keyboard.h"
|
|
#include "../include/task.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
typedef size_t uword_t;
|
|
struct interrupt_frame
|
|
{
|
|
uword_t ip;
|
|
uword_t cs;
|
|
uword_t flags;
|
|
uword_t sp;
|
|
uword_t ss;
|
|
};
|
|
|
|
__attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame);
|
|
void isr_custom();
|
|
void default_handler();
|
|
|
|
extern void sys_exit_handler();
|
|
extern void page_fault_handler();
|
|
|
|
void isr_install() {
|
|
idt_set_descriptor(0, (uint32)isr0, 0x8E);
|
|
idt_set_descriptor(1, (uint32)isr1, 0x8E);
|
|
idt_set_descriptor(2, (uint32)isr2, 0x8E);
|
|
idt_set_descriptor(3, (uint32)isr3, 0x8E);
|
|
idt_set_descriptor(4, (uint32)isr4, 0x8E);
|
|
idt_set_descriptor(5, (uint32)isr5, 0x8E);
|
|
idt_set_descriptor(6, (uint32)isr6, 0x8E);
|
|
idt_set_descriptor(7, (uint32)isr7, 0x8E);
|
|
idt_set_descriptor(8, (uint32)isr8, 0x8E);
|
|
idt_set_descriptor(9, (uint32)isr9, 0x8E);
|
|
idt_set_descriptor(10, (uint32)isr10, 0x8E);
|
|
idt_set_descriptor(11, (uint32)isr11, 0x8E);
|
|
idt_set_descriptor(12, (uint32)isr12, 0x8E);
|
|
idt_set_descriptor(13, (uint32)isr13, 0x8E);
|
|
idt_set_descriptor(14, (uint32)page_fault_handler, 0x8E);
|
|
idt_set_descriptor(15, (uint32)isr15, 0x8E);
|
|
idt_set_descriptor(16, (uint32)isr16, 0x8E);
|
|
idt_set_descriptor(17, (uint32)isr17, 0x8E);
|
|
idt_set_descriptor(18, (uint32)isr18, 0x8E);
|
|
idt_set_descriptor(19, (uint32)isr19, 0x8E);
|
|
idt_set_descriptor(20, (uint32)isr20, 0x8E);
|
|
idt_set_descriptor(21, (uint32)isr21, 0x8E);
|
|
idt_set_descriptor(22, (uint32)isr22, 0x8E);
|
|
idt_set_descriptor(23, (uint32)isr23, 0x8E);
|
|
idt_set_descriptor(24, (uint32)isr24, 0x8E);
|
|
idt_set_descriptor(25, (uint32)isr25, 0x8E);
|
|
idt_set_descriptor(26, (uint32)isr26, 0x8E);
|
|
idt_set_descriptor(27, (uint32)isr27, 0x8E);
|
|
idt_set_descriptor(28, (uint32)isr28, 0x8E);
|
|
idt_set_descriptor(29, (uint32)isr29, 0x8E);
|
|
idt_set_descriptor(30, (uint32)isr30, 0x8E);
|
|
idt_set_descriptor(31, (uint32)isr31, 0x8E);
|
|
|
|
for(int i = 32; i < 255; i++)
|
|
idt_set_descriptor(i, (uint32)default_handler, 0x8E);
|
|
|
|
idt_set_descriptor(0x20, (uint32)isr_timer, 0x8E);
|
|
idt_set_descriptor(0x21, (uint32)isr_custom, 0x8E);
|
|
|
|
idt_set_descriptor(0x80, (uint32)sys_exit_handler, 0xEE);
|
|
|
|
idt_set(); // Load with ASM
|
|
printf("isr_install done\n");
|
|
}
|
|
|
|
void default_handler()
|
|
{
|
|
|
|
}
|
|
|
|
#include "../include/paging.h"
|
|
|
|
void handle_page_fault(uint32_t error_code, uint32_t faulting_address)
|
|
{
|
|
int present = !(error_code & 0x1); // Bit 0: 0 = not present, 1 = protection violation
|
|
int write = error_code & 0x2; // Bit 1: 0 = read, 1 = write
|
|
int user = error_code & 0x4; // Bit 2: 0 = supervisor, 1 = user
|
|
int reserved = error_code & 0x8; // Bit 3: 0 = no reserved bits, 1 = reserved bits
|
|
int instruction = error_code & 0x10; // Bit 4: 0 = data, 1 = instruction fetch
|
|
|
|
uint32_t pte = get_pte((void*)faulting_address);
|
|
|
|
printf("Page fault at address 0x%x\n", faulting_address);
|
|
printf("Present: %X, Write: %X, User: %X, Reserved: %X, Instruction: %X\n",
|
|
present, write, user, reserved, instruction);
|
|
|
|
printf("PTE: 0x%X (phys: 0x%X, %s, %s)\n", pte, pte & ~0xFFF,
|
|
(pte & 0x2) ? "writable" : "read-only",
|
|
(pte & 0x4) ? "user" : "supervisor");
|
|
|
|
printf("ERR: 0x%X\n", error_code);
|
|
|
|
asm("hlt");
|
|
}
|
|
|
|
__attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame)
|
|
{
|
|
pit_init(100);
|
|
pic_end_int(0x0);
|
|
|
|
scheduler_lock();
|
|
schedule();
|
|
scheduler_unlock();
|
|
}
|
|
|
|
__attribute__((interrupt)) void isr_custom(struct interrupt_frame *frame)
|
|
{
|
|
//terminal_writestring("YOOOOO!!!!!! I LOVE KATYA!!!!");
|
|
handle_keyboard();
|
|
pic_end_int(0x1);
|
|
}
|
|
|
|
void isr0()
|
|
{
|
|
printf(exception_messages[0]);
|
|
asm("hlt");
|
|
}
|
|
void isr1()
|
|
{
|
|
printf(exception_messages[1]);
|
|
asm("hlt");
|
|
}
|
|
void isr2()
|
|
{
|
|
printf(exception_messages[2]);
|
|
asm("hlt");
|
|
}
|
|
void isr3()
|
|
{
|
|
printf(exception_messages[3]);
|
|
asm("hlt");
|
|
}
|
|
void isr4()
|
|
{
|
|
printf(exception_messages[4]);
|
|
asm("hlt");
|
|
}
|
|
void isr5()
|
|
{
|
|
printf(exception_messages[5]);
|
|
asm("hlt");
|
|
}
|
|
void isr6()
|
|
{
|
|
printf(exception_messages[6]);
|
|
asm("hlt");
|
|
}
|
|
void isr7()
|
|
{
|
|
printf(exception_messages[7]);
|
|
asm("hlt");
|
|
}
|
|
void isr8()
|
|
{
|
|
printf(exception_messages[8]);
|
|
asm("hlt");
|
|
}
|
|
void isr9()
|
|
{
|
|
printf(exception_messages[9]);
|
|
asm("hlt");
|
|
}
|
|
void isr10()
|
|
{
|
|
printf(exception_messages[10]);
|
|
asm("hlt");
|
|
}
|
|
void isr11()
|
|
{
|
|
printf(exception_messages[11]);
|
|
asm("hlt");
|
|
}
|
|
void isr12()
|
|
{
|
|
printf(exception_messages[12]);
|
|
asm("hlt");
|
|
}
|
|
void isr13()
|
|
{
|
|
printf(exception_messages[13]);
|
|
asm("hlt");
|
|
}
|
|
void isr14()
|
|
{
|
|
printf(exception_messages[14]);
|
|
asm("hlt");
|
|
}
|
|
void isr15()
|
|
{
|
|
printf(exception_messages[15]);
|
|
asm("hlt");
|
|
}
|
|
void isr16()
|
|
{
|
|
printf(exception_messages[16]);
|
|
asm("hlt");
|
|
}
|
|
void isr17()
|
|
{
|
|
printf(exception_messages[17]);
|
|
asm("hlt");
|
|
}
|
|
void isr18()
|
|
{
|
|
printf(exception_messages[18]);
|
|
asm("hlt");
|
|
}
|
|
void isr19()
|
|
{
|
|
printf(exception_messages[19]);
|
|
asm("hlt");
|
|
}
|
|
void isr20()
|
|
{
|
|
printf(exception_messages[20]);
|
|
asm("hlt");
|
|
}
|
|
void isr21()
|
|
{
|
|
printf(exception_messages[21]);
|
|
asm("hlt");
|
|
}
|
|
void isr22()
|
|
{
|
|
printf(exception_messages[22]);
|
|
asm("hlt");
|
|
}
|
|
void isr23()
|
|
{
|
|
printf(exception_messages[23]);
|
|
asm("hlt");
|
|
}
|
|
void isr24()
|
|
{
|
|
printf(exception_messages[24]);
|
|
asm("hlt");
|
|
}
|
|
void isr25()
|
|
{
|
|
printf(exception_messages[25]);
|
|
asm("hlt");
|
|
}
|
|
void isr26()
|
|
{
|
|
printf(exception_messages[26]);
|
|
asm("hlt");
|
|
}
|
|
void isr27()
|
|
{
|
|
printf(exception_messages[27]);
|
|
asm("hlt");
|
|
}
|
|
void isr28()
|
|
{
|
|
printf(exception_messages[28]);
|
|
asm("hlt");
|
|
}
|
|
void isr29()
|
|
{
|
|
printf(exception_messages[29]);
|
|
asm("hlt");
|
|
}
|
|
void isr30()
|
|
{
|
|
printf(exception_messages[30]);
|
|
asm("hlt");
|
|
}
|
|
void isr31()
|
|
{
|
|
printf(exception_messages[31]);
|
|
asm("hlt");
|
|
}
|
|
|
|
static string exception_messages[] = {
|
|
"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"
|
|
};
|