218 lines
5.1 KiB
C
218 lines
5.1 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "intr.h"
|
|
#include "io.h"
|
|
#include "gdt.h"
|
|
#include "hal/hal.h"
|
|
#include "kprintf.h"
|
|
#include "compiler/attr.h"
|
|
#include "pic.h"
|
|
#include "pit.h"
|
|
#include "proc/proc.h"
|
|
#include "syscall/syscall.h"
|
|
#include "errors.h"
|
|
#include "drivers/ps2kb/ps2kb.h"
|
|
#include "ipc/pipe/pipe.h"
|
|
#include "proc/ps2kbproc/ps2kbproc.h"
|
|
#include "rbuf/rbuf.h"
|
|
|
|
void hal_intr_disable(void) {
|
|
asm volatile("cli");
|
|
}
|
|
|
|
void hal_intr_enable(void) {
|
|
asm volatile("sti");
|
|
}
|
|
|
|
typedef struct {
|
|
uint16_t intrlow;
|
|
uint16_t kernelcs;
|
|
uint8_t ist;
|
|
uint8_t attrs;
|
|
uint16_t intrmid;
|
|
uint32_t intrhigh;
|
|
uint32_t resv;
|
|
} PACKED IdtGate;
|
|
|
|
typedef struct {
|
|
uint16_t limit;
|
|
uint64_t base;
|
|
} PACKED Idt;
|
|
|
|
#define ENTRIES 256
|
|
ALIGNED(0x10) static IdtGate idtgates[ENTRIES] = {0};
|
|
static Idt idt = {0};
|
|
|
|
void idt_setentry(int i, uint64_t handler, uint8_t ist, uint8_t flags) {
|
|
idtgates[i].intrlow = handler & 0xffff;
|
|
idtgates[i].kernelcs = KCODE;
|
|
idtgates[i].ist = ist;
|
|
idtgates[i].attrs = flags;
|
|
idtgates[i].intrmid = (handler >> 16) & 0xFFFF;
|
|
idtgates[i].intrhigh = (handler >> 32) & 0xFFFFFFFF;
|
|
idtgates[i].resv = 0;
|
|
}
|
|
|
|
void idt_init(void) {
|
|
idt.base = (uint64_t)&idtgates;
|
|
idt.limit = ENTRIES * sizeof(IdtGate) - 1;
|
|
asm volatile("lidt %0" :: "m"(idt) : "memory");
|
|
|
|
LOG("hal", "idt init\n");
|
|
}
|
|
|
|
extern void *ISR_REDIRTABLE[];
|
|
|
|
static const char *exceptions[] = {
|
|
"#DE", "#DB", "NMI",
|
|
"#BP", "#OF", "#BR",
|
|
"#UD", "#NM", "#DF",
|
|
"CSO", "#TS", "#NP",
|
|
"#SS", "#GP", "#PF",
|
|
"RES", "#MF", "#AC",
|
|
"#MC", "#XM", "#VE",
|
|
"#CP",
|
|
};
|
|
|
|
void intr_init(void) {
|
|
#define MKINTR(N, IST) \
|
|
extern void intr_vec##N(void); \
|
|
idt_setentry(N, (uint64_t)&intr_vec##N, IST, 0x8E);
|
|
|
|
MKINTR(0, 0);
|
|
MKINTR(1, 0);
|
|
MKINTR(2, 2);
|
|
MKINTR(4, 0);
|
|
MKINTR(5, 0);
|
|
MKINTR(6, 0);
|
|
MKINTR(7, 0);
|
|
MKINTR(8, 1);
|
|
MKINTR(9, 0);
|
|
MKINTR(10, 0);
|
|
MKINTR(11, 0);
|
|
MKINTR(12, 0);
|
|
MKINTR(13, 0);
|
|
MKINTR(14, 0);
|
|
MKINTR(15, 0);
|
|
MKINTR(16, 0);
|
|
MKINTR(17, 0);
|
|
MKINTR(18, 0);
|
|
MKINTR(19, 0);
|
|
MKINTR(20, 0);
|
|
MKINTR(21, 0);
|
|
MKINTR(22, 0);
|
|
MKINTR(23, 0);
|
|
MKINTR(24, 0);
|
|
MKINTR(25, 0);
|
|
MKINTR(26, 0);
|
|
MKINTR(27, 0);
|
|
MKINTR(28, 0);
|
|
MKINTR(29, 0);
|
|
MKINTR(30, 0);
|
|
MKINTR(31, 0);
|
|
MKINTR(32, 0);
|
|
MKINTR(33, 0);
|
|
MKINTR(34, 0);
|
|
MKINTR(35, 0);
|
|
MKINTR(36, 0);
|
|
MKINTR(37, 0);
|
|
MKINTR(38, 0);
|
|
MKINTR(39, 0);
|
|
MKINTR(40, 3);
|
|
MKINTR(41, 0);
|
|
MKINTR(42, 0);
|
|
MKINTR(43, 0);
|
|
MKINTR(44, 0);
|
|
MKINTR(45, 0);
|
|
MKINTR(46, 0);
|
|
MKINTR(47, 0);
|
|
|
|
extern void intr_vec128(void);
|
|
idt_setentry(0x80, (uint64_t)&intr_vec128, 0, 0xEE);
|
|
|
|
idt_init();
|
|
}
|
|
|
|
void intr_dumpframe(IntrStackFrame *frame) {
|
|
uint64_t cr2;
|
|
asm volatile("mov %%cr2, %0" : "=r"(cr2));
|
|
uint64_t cr3;
|
|
asm volatile("mov %%cr3, %0" : "=r"(cr3));
|
|
uint64_t cr4;
|
|
asm volatile("mov %%cr4, %0" : "=r"(cr4));
|
|
kprintf("rax=%016lx rcx=%016lx rdx=%016lx\n"
|
|
"rsi=%016lx rdi=%016lx r8 =%016lx\n"
|
|
"r9 =%016lx r10=%016lx r11=%016lx\n"
|
|
"rip=%016lx rfl=%016lx rsp=%016lx\n"
|
|
"cs =%016lx ss =%016lx trp=%016lx\n"
|
|
"cr2=%016lx cr3=%016lx cr4=%016lx\n"
|
|
"\n\n",
|
|
frame->regs.rax, frame->regs.rcx, frame->regs.rdx,
|
|
frame->regs.rsi, frame->regs.rdi, frame->regs.r8,
|
|
frame->regs.r9, frame->regs.r10, frame->regs.r11,
|
|
frame->rip, frame->rflags, frame->rsp,
|
|
frame->cs, frame->ss, frame->trapnum,
|
|
cr2, cr3, cr4
|
|
);
|
|
}
|
|
|
|
void hal_syscalldispatch(IntrStackFrame *frame) {
|
|
uint64_t sysnum = frame->regs.rax;
|
|
if (sysnum < SYSCALLS_MAX) {
|
|
SyscallFn fn = SYSCALL_TABLE[sysnum];
|
|
if (fn == NULL) {
|
|
frame->regs.rax = E_BADSYSCALL;
|
|
return;
|
|
}
|
|
int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx,
|
|
frame->regs.r10, frame->regs.r8, frame->regs.r9);
|
|
|
|
if (ret == E_DOSCHEDULING) {
|
|
proc_sched((void *)frame);
|
|
}
|
|
frame->regs.rax = *(uint64_t *)&ret;
|
|
}
|
|
}
|
|
|
|
void intr_eoi(uint8_t irq) {
|
|
if (irq >= 8) {
|
|
io_out8(PIC2_CMD, PIC_EOI);
|
|
}
|
|
io_out8(PIC1_CMD, PIC_EOI);
|
|
}
|
|
|
|
void intr_handleintr(IntrStackFrame *frame) {
|
|
if (frame->trapnum <= 31) {
|
|
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
|
|
intr_dumpframe(frame);
|
|
if (frame->trapnum == 14 && frame->errnum & 0x4) {
|
|
kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name);
|
|
proc_killself();
|
|
proc_sched((void *)frame);
|
|
}
|
|
hal_hang();
|
|
} else if (frame->trapnum >= 32 && frame->trapnum <= 47) {
|
|
switch (frame->trapnum) {
|
|
case INTR_IRQBASE+0:
|
|
PIT_TICKS++;
|
|
intr_eoi(frame->trapnum - INTR_IRQBASE);
|
|
proc_sched((void *)frame);
|
|
break;
|
|
case INTR_IRQBASE+1:
|
|
int32_t c = ps2kb_intr();
|
|
intr_eoi(frame->trapnum - INTR_IRQBASE);
|
|
uint8_t *bytes = (uint8_t *)&c;
|
|
spinlock_acquire(&PS2KB_BUF.spinlock);
|
|
for (size_t i = 0; i < sizeof(c); i++) {
|
|
rbuf_push(&PS2KB_BUF.rbuf, bytes[i]);
|
|
}
|
|
spinlock_release(&PS2KB_BUF.spinlock);
|
|
break;
|
|
}
|
|
} else if (frame->trapnum == 0x80) {
|
|
hal_syscalldispatch(frame);
|
|
}
|
|
}
|
|
|