2026-06-11 23:23:39 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2026-06-26 20:05:01 -05:00
|
|
|
#include <lib/print.h>
|
2026-06-13 19:53:57 -04:00
|
|
|
|
2026-06-26 20:05:01 -05:00
|
|
|
#include "syscall.h"
|
2026-06-11 23:23:39 -05:00
|
|
|
|
2026-06-26 20:05:01 -05:00
|
|
|
#include <drivers/pic/pic.h>
|
|
|
|
|
#include <drivers/ps2/ps2.h>
|
|
|
|
|
|
|
|
|
|
#define IDT_MAX_DESCRIPTORS 256
|
|
|
|
|
#define ATTR_KERN_32 0x8E // active(1), ring 0 (00), interrupt gate(0), 32 bit (1110)
|
|
|
|
|
#define ATTR_USER_32 0xEE // active, ring 3, 32bit
|
|
|
|
|
#define SEGMENT_KERN 0x08
|
|
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
|
uint16_t isr_low;
|
|
|
|
|
uint16_t selector;
|
|
|
|
|
uint8_t reserved;
|
|
|
|
|
uint8_t attributes;
|
|
|
|
|
uint16_t isr_high;
|
|
|
|
|
} idt_entry;
|
|
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
|
uint16_t limit;
|
|
|
|
|
uint32_t base;
|
|
|
|
|
} idt_ptr;
|
|
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) {
|
2026-06-11 23:23:39 -05:00
|
|
|
uint32_t edi, esi, ebp, esp_dummy, ebx, edx, ecx, eax;
|
|
|
|
|
|
|
|
|
|
uint32_t int_no;
|
|
|
|
|
uint32_t error_code;
|
|
|
|
|
|
|
|
|
|
uint32_t eip, cs, eflags, useresp, ss;
|
2026-06-26 20:05:01 -05:00
|
|
|
} registers;
|
|
|
|
|
|
2026-06-28 17:58:58 -05:00
|
|
|
// (interrupts.S)
|
2026-06-26 20:05:01 -05:00
|
|
|
extern void load_idt(void);
|
|
|
|
|
extern void isr0();
|
|
|
|
|
extern void isr1();
|
|
|
|
|
extern void isr8();
|
|
|
|
|
extern void isr13();
|
|
|
|
|
extern void isr14();
|
|
|
|
|
extern void isr32();
|
|
|
|
|
extern void isr33();
|
|
|
|
|
|
|
|
|
|
static void* isr_stub_table[256] = {
|
|
|
|
|
[0] = isr0,
|
|
|
|
|
[1] = isr1,
|
|
|
|
|
[8] = isr8,
|
|
|
|
|
[13] = isr13,
|
|
|
|
|
[14] = isr14,
|
|
|
|
|
[32] = isr32,
|
|
|
|
|
[33] = isr33,
|
2026-06-11 23:23:39 -05:00
|
|
|
};
|
|
|
|
|
|
2026-06-26 20:05:01 -05:00
|
|
|
static const char* err_messages[32] = {
|
2026-06-11 23:23:39 -05:00
|
|
|
[0] = "Divide by 0",
|
|
|
|
|
[1] = "",
|
|
|
|
|
[8] = "",
|
2026-06-13 20:43:09 -04:00
|
|
|
[13] = "General Protection Fault",
|
2026-06-11 23:23:39 -05:00
|
|
|
[14] = "Page Fault",
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-26 20:05:01 -05:00
|
|
|
idt_entry idt[IDT_MAX_DESCRIPTORS];
|
|
|
|
|
idt_ptr idtr;
|
|
|
|
|
|
|
|
|
|
static void set_idtr(uint8_t vector, void* isr, uint16_t sel, uint8_t attributes) {
|
|
|
|
|
idt_entry* desc = &idt[vector];
|
|
|
|
|
|
|
|
|
|
desc->isr_low = (uint32_t)isr & 0xFFFF;
|
|
|
|
|
desc->selector = SEGMENT_KERN;
|
|
|
|
|
desc->reserved = 0;
|
|
|
|
|
desc->attributes = attributes;
|
|
|
|
|
desc->isr_high = ((uint32_t)isr >> 16) & 0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init_idt() {
|
|
|
|
|
idtr.limit = (sizeof(idt_entry) * IDT_MAX_DESCRIPTORS) - 1;
|
|
|
|
|
idtr.base = (uint32_t)&idt;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < IDT_MAX_DESCRIPTORS; i++) {
|
|
|
|
|
if (isr_stub_table[i] != 0) {
|
|
|
|
|
set_idtr(i, isr_stub_table[i], SEGMENT_KERN, ATTR_KERN_32);
|
|
|
|
|
} else {
|
|
|
|
|
set_idtr(i, 0, SEGMENT_KERN, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_idtr(0x80, syscall_handler, SEGMENT_KERN, ATTR_USER_32);
|
|
|
|
|
|
|
|
|
|
load_idt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void common_interrupt_handler(registers *args) {
|
2026-06-13 19:53:57 -04:00
|
|
|
switch (args->int_no) {
|
2026-06-11 23:23:39 -05:00
|
|
|
case 0:
|
|
|
|
|
case 1:
|
2026-06-13 19:53:57 -04:00
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
case 4:
|
|
|
|
|
case 5:
|
|
|
|
|
case 6:
|
|
|
|
|
case 7:
|
2026-06-11 23:23:39 -05:00
|
|
|
case 8:
|
2026-06-13 19:53:57 -04:00
|
|
|
case 9:
|
|
|
|
|
case 10:
|
|
|
|
|
case 11:
|
|
|
|
|
case 12:
|
2026-06-11 23:23:39 -05:00
|
|
|
case 13:
|
2026-06-13 19:53:57 -04:00
|
|
|
case 15:
|
|
|
|
|
case 16:
|
2026-06-14 00:34:11 -05:00
|
|
|
kprintf("CPU Error: %s", err_messages[args->int_no]);
|
2026-06-11 23:23:39 -05:00
|
|
|
break;
|
2026-06-16 18:44:31 -05:00
|
|
|
case 14:{
|
|
|
|
|
uint32_t faulting_address;
|
|
|
|
|
asm volatile("mov %%cr2, %0" : "=r"(faulting_address));
|
|
|
|
|
kprintf("\n--- PAGE FAULT ---\n");
|
2026-06-16 21:53:38 -04:00
|
|
|
kprintf("Faulted Virtual Address: 0x%x\n", faulting_address);
|
2026-06-16 18:44:31 -05:00
|
|
|
kprintf("Error Code: 0x%x\n", args->error_code);
|
2026-06-16 21:53:38 -04:00
|
|
|
kprintf("Instruction Pointer: 0x%x\n", args->eip);
|
2026-06-16 18:44:31 -05:00
|
|
|
kprintf("Caused by: %s, %s, running in %s mode\n",
|
2026-06-16 21:53:38 -04:00
|
|
|
(args->error_code & 0x01) ? "Protection Violation" : "Page Not Present",
|
|
|
|
|
(args->error_code & 0x02) ? "Write" : "Read",
|
|
|
|
|
(args->error_code & 0x04) ? "User" : "Supervisor");
|
2026-06-16 18:44:31 -05:00
|
|
|
while(1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-06-13 19:53:57 -04:00
|
|
|
case 33: {
|
|
|
|
|
uint8_t data = ps2_read_data();
|
2026-06-13 13:27:39 -05:00
|
|
|
send_eoi_master();
|
2026-06-11 23:23:39 -05:00
|
|
|
break;
|
2026-06-13 19:53:57 -04:00
|
|
|
}
|
2026-06-23 23:19:49 -05:00
|
|
|
case 46: { // IDE PRIMARY
|
|
|
|
|
send_eoi_slave();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 47: { // IDE SLAVE
|
|
|
|
|
send_eoi_slave();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-06-11 23:23:39 -05:00
|
|
|
default:
|
2026-06-14 00:34:11 -05:00
|
|
|
kprintf("Received unhandled interrupt: %d\n", args->int_no);
|
2026-06-11 23:23:39 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|