2021-07-14 19:32:15 +00:00
|
|
|
#include "idt.h"
|
2021-07-16 01:24:01 +00:00
|
|
|
#include "io.h"
|
|
|
|
#include "pic/keyboard.h"
|
2021-07-17 09:57:21 +00:00
|
|
|
#include "pic/pic.h"
|
|
|
|
#include "pic/pit.h"
|
|
|
|
#include "stdio.h"
|
2021-07-14 19:32:15 +00:00
|
|
|
|
|
|
|
static struct idt idt = { 0 };
|
|
|
|
|
2021-07-15 13:22:41 +00:00
|
|
|
static struct idt_entry_info infos[] = {
|
2021-07-15 14:53:58 +00:00
|
|
|
// Intel-defined (0-20)
|
2021-07-15 02:42:11 +00:00
|
|
|
{int_de, INTERRUPT_TYPE}, {int_db, INTERRUPT_TYPE},
|
|
|
|
{int_nmi, INTERRUPT_TYPE}, {int_bp, TRAP_TYPE},
|
|
|
|
{int_of, TRAP_TYPE}, {int_br, INTERRUPT_TYPE},
|
|
|
|
{int_ud, INTERRUPT_TYPE}, {int_nm, INTERRUPT_TYPE},
|
|
|
|
{int_df, INTERRUPT_TYPE}, {0, INTERRUPT_TYPE},
|
|
|
|
{int_ts, INTERRUPT_TYPE}, {int_np, INTERRUPT_TYPE},
|
|
|
|
{int_ss, INTERRUPT_TYPE}, {int_gp, INTERRUPT_TYPE},
|
|
|
|
{int_pf, INTERRUPT_TYPE}, {0, INTERRUPT_TYPE},
|
|
|
|
{int_mf, INTERRUPT_TYPE}, {int_ac, INTERRUPT_TYPE},
|
|
|
|
{int_mc, INTERRUPT_TYPE}, {int_xm, INTERRUPT_TYPE},
|
2021-07-15 14:53:58 +00:00
|
|
|
{int_ve, INTERRUPT_TYPE},
|
|
|
|
|
|
|
|
// Intel-reserved (21-31)
|
|
|
|
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
|
|
|
|
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
|
|
|
|
|
|
|
|
// user-defined (32-255)
|
2021-07-17 09:57:21 +00:00
|
|
|
{pic_pit, INTERRUPT_TYPE}, {pic_keyboard, INTERRUPT_TYPE}
|
2021-07-15 02:42:11 +00:00
|
|
|
};
|
|
|
|
|
2021-07-15 13:22:41 +00:00
|
|
|
void handle_interrupt(struct int_args *args)
|
2021-07-14 20:57:38 +00:00
|
|
|
{
|
2021-07-17 09:57:21 +00:00
|
|
|
switch (args->int_code)
|
2021-07-16 01:24:01 +00:00
|
|
|
{
|
2021-07-17 09:57:21 +00:00
|
|
|
case 32: // PIT
|
|
|
|
pit_handler();
|
|
|
|
outb(MASTER_PORT_A, OCW2_EOI);
|
|
|
|
return;
|
|
|
|
case 33: // keyboard
|
|
|
|
keyboard_handler();
|
|
|
|
outb(MASTER_PORT_A, OCW2_EOI);
|
|
|
|
return;
|
2021-07-16 01:24:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 13:22:41 +00:00
|
|
|
printf("INTERRUPT: %d, error code: %d\r\n", args->int_code, args->err_code);
|
2021-07-16 01:24:01 +00:00
|
|
|
|
|
|
|
asm volatile("cli; hlt");
|
2021-07-14 20:57:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 02:42:11 +00:00
|
|
|
static struct idt_entry create_idt_entry(uint32_t offset,
|
|
|
|
struct segment_selector selector,
|
|
|
|
uint32_t type)
|
2021-07-14 20:57:38 +00:00
|
|
|
{
|
|
|
|
struct idt_entry res = { 0 };
|
|
|
|
|
|
|
|
res.selector = selector;
|
|
|
|
res.offset_1 = offset & 0xffff;
|
|
|
|
res.offset_2 = (offset >> 16) & 0xffff;
|
|
|
|
|
2021-07-15 02:42:11 +00:00
|
|
|
res.type = type;
|
2021-07-14 20:57:38 +00:00
|
|
|
res.size = 1;
|
|
|
|
res.desc_priv = 0;
|
|
|
|
res.present = 1;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void create_idt(void)
|
|
|
|
{
|
|
|
|
struct segment_selector selector;
|
|
|
|
selector.rpl = 0;
|
|
|
|
selector.table_indicator = 0;
|
|
|
|
selector.index = KERNEL_CS_INDEX;
|
|
|
|
for (int i = 0; i < IDT_NB_ENTRIES; ++i)
|
|
|
|
{
|
2021-07-15 02:42:11 +00:00
|
|
|
struct idt_entry entry = create_idt_entry((uint32_t)infos[i].isr,
|
|
|
|
selector,
|
|
|
|
infos[i].type);
|
2021-07-14 20:57:38 +00:00
|
|
|
idt.entries[i] = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_idt(void)
|
2021-07-14 19:32:15 +00:00
|
|
|
{
|
|
|
|
struct idt_r idtr;
|
|
|
|
idtr.addr = (uint32_t)&idt;
|
|
|
|
idtr.limit = IDT_SIZE - 1;
|
|
|
|
|
|
|
|
asm volatile("lidt %0\n"
|
|
|
|
:
|
|
|
|
: "m" (idtr)
|
|
|
|
: "memory");
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:57:38 +00:00
|
|
|
void init_idt(void)
|
2021-07-14 19:32:15 +00:00
|
|
|
{
|
2021-07-14 20:57:38 +00:00
|
|
|
create_idt();
|
2021-07-14 19:32:15 +00:00
|
|
|
load_idt();
|
|
|
|
}
|