/* Copyright (C) 2021 Rain */
/* This file is part of XNIX. */
/*
XNIX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) and later version.
*/
/*
XNIX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
You should have received a copy of the GNU General Public License
along with XNIX. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
build_irq(0x20);
build_irq(0x21);
int int_init(void) {
schk_init();
/* init PIC */
/* ICW: initial control word */
outb(0x11, PIC0_ICW1);
outb(0x20, PIC0_ICW2);
outb(0x04, PIC0_ICW3);
outb(0x01, PIC0_ICW4);
outb(0x11, PIC1_ICW1);
outb(0x28, PIC1_ICW2);
outb(0x02, PIC1_ICW3);
outb(0x01, PIC1_ICW4);
/* disable all IRQs */
outb(0xff, PIC0_IMR);
outb(0xff, PIC1_IMR);
set_trap_gate(idt_table + 0x00, 0, ÷_error);
set_trap_gate(idt_table + 0x01, 0, &debug);
set_trap_gate(idt_table + 0x02, 0, &nmi);
set_trap_gate(idt_table + 0x03, 0, &int3);
set_trap_gate(idt_table + 0x04, 0, &overflow);
set_trap_gate(idt_table + 0x05, 0, &bound);
set_trap_gate(idt_table + 0x06, 0, &invalid_code);
set_trap_gate(idt_table + 0x07, 0, &device_not_avaliable);
set_trap_gate(idt_table + 0x08, 0, &double_fault);
set_trap_gate(idt_table + 0x09, 0, &fpu_segment_overrun);
set_trap_gate(idt_table + 0x0a, 0, &invalid_tss);
set_trap_gate(idt_table + 0x0b, 0, &segment_not_present);
set_trap_gate(idt_table + 0x0c, 0, &stack_error);
set_trap_gate(idt_table + 0x0d, 0, &general_protection);
set_trap_gate(idt_table + 0x0e, 0, &page_fault);
set_trap_gate(idt_table + 0x0f, 0, &reserved);
set_trap_gate(idt_table + 0x10, 0, &fpu_error);
set_trap_gate(idt_table + 0x11, 0, &alignment);
set_trap_gate(idt_table + 0x12, 0, &machine);
set_trap_gate(idt_table + 0x13, 0, &simd);
set_trap_gate(idt_table + 0x14, 0, &virtualization);
set_int_gate(idt_table + 0x80, 0, &syscall);
register_irq(0x00, irq0x20_interrupt, do_timer);
register_irq(0x01, irq0x21_interrupt, do_keyboard);
schk_leave();
return 0;
}
void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
schk_init();
#define handler ((__uint64_t) addr)
entry->selector = KERNEL_CS;
entry->ist = ist;
entry->attr = attr;
entry->off_0_16 = handler & 0xffff;
entry->off_16_32 = handler >> 16;
entry->off_32_64 = handler >> 32;
entry->zero = 0x00000000;
schk_leave();
return;
}
void syscall(void) {
schk_init();
print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
schk_leave();
return;
}
void die(char *s, __uint64_t *rsp, char nr) {
/* should we use schk here? */
printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
for (;;) hlt();
return;
}
void do_divide_error(__uint64_t *rsp) {
schk_init();
die("divide error", rsp, 0);
schk_leave();
return;
}
void do_debug(__uint64_t *rsp) {
schk_init();
die("debug", rsp, 1);
schk_leave();
return;
}
void do_nmi(__uint64_t *rsp) {
schk_init();
die("nmi", rsp, 2);
schk_leave();
return;
}
void do_int3(__uint64_t *rsp) {
schk_init();
die("int3", rsp, 3);
schk_leave();
return;
}
void do_overflow(__uint64_t *rsp) {
schk_init();
die("overflow", rsp, 4);
schk_leave();
return;
}
void do_bound(__uint64_t *rsp) {
schk_init();
die("bound", rsp, 5);
schk_leave();
return;
}
void do_ud(__uint64_t *rsp) {
schk_init();
die("undefined instruction", rsp, 6);
schk_leave();
return;
}
void do_no_fpu(__uint64_t *rsp) {
schk_init();
die("fpu not present", rsp, 7);
schk_leave();
return;
}
void do_double_fault(__uint64_t *rsp) {
schk_init();
die("double fault", rsp, 8);
schk_leave();
return;
}
void do_fpu_overrun(__uint64_t *rsp) {
schk_init();
die("fpu overrun", rsp, 9);
schk_leave();
return;
}
void do_invalid_tss(__uint64_t *rsp) {
schk_init();
die("invalid tss", rsp, 10);
schk_leave();
return;
}
void do_no_segment(__uint64_t *rsp) {
schk_init();
die("no such segment", rsp, 11);
schk_leave();
return;
}
void do_stack(__uint64_t *rsp) {
schk_init();
die("stack error", rsp, 12);
schk_leave();
return;
}
void do_gp(__uint64_t *rsp) {
schk_init();
die("general protection", rsp, 13);
schk_leave();
return;
}
void do_page_fault(__uint64_t *rsp) {
schk_init();
die("page fault", rsp, 14);
schk_leave();
return;
}
void do_reserved(__uint64_t *rsp) {
schk_init();
die("reserved", rsp, 15);
schk_leave();
return;
}
void do_fpu_error(__uint64_t *rsp) {
schk_init();
die("fpu error", rsp, 16);
schk_leave();
return;
}
void do_alignment(__uint64_t *rsp) {
schk_init();
die("alignment error", rsp, 17);
schk_leave();
return;
}
void do_machine(__uint64_t *rsp) {
schk_init();
die("machine check", rsp, 18);
schk_leave();
return;
}
void do_simd(__uint64_t *rsp) {
schk_init();
die("simd error", rsp, 19);
schk_leave();
return;
}
void do_virtualization(__uint64_t *rsp) {
schk_init();
die("virtualization error", rsp, 20);
schk_leave();
return;
}