| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- /* 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 <https://www.gnu.org/licenses/>.
- */
- #include <kernel/init.h>
- #include <kernel/errno.h>
- #include <kernel/types.h>
- #include <kernel/graphic.h>
- #include <kernel/int.h>
- #include <kernel/schk.h>
- #include <kernel/mm.h>
- #include <arch/x86/int.h>
- #include <arch/io.h>
- #include <arch/sys.h>
- 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);
- /* initial PIT timer */
- outb(0x34, 0x43);
- /* real time = 1.1932HZ / this */
- /* about 10ms */
- #define TIMER_HZ_LOW 0x9c
- #define TIMER_HZ_HIGH 0x2e
- outb(TIMER_HZ_LOW, 0x40);
- outb(TIMER_HZ_HIGH, 0x40);
- register_irq(0x00, irq0x20_interrupt, do_timer);
- register_irq(0x01, irq0x21_interrupt, do_keyboard);
- set_int_gate(idt_table + 0x80, 1, &syscall);
- 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 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);
- printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp + RIP);
- trace_mm();
- for (;;) hlt();
- return;
- }
- void do_divide_error(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("divide error", rsp, 0);
- schk_leave();
- return;
- }
- void do_debug(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("debug", rsp, 1);
- schk_leave();
- return;
- }
- void do_nmi(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("nmi", rsp, 2);
- schk_leave();
- return;
- }
- void do_int3(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("int3", rsp, 3);
- schk_leave();
- return;
- }
- void do_overflow(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("overflow", rsp, 4);
- schk_leave();
- return;
- }
- void do_bound(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("bound", rsp, 5);
- schk_leave();
- return;
- }
- void do_ud(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("undefined instruction", rsp, 6);
- schk_leave();
- return;
- }
- void do_no_fpu(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu not present", rsp, 7);
- schk_leave();
- return;
- }
- void do_double_fault(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("double fault", rsp, 8);
- schk_leave();
- return;
- }
- void do_fpu_overrun(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu overrun", rsp, 9);
- schk_leave();
- return;
- }
- void do_invalid_tss(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("invalid tss", rsp, 10);
- schk_leave();
- return;
- }
- void do_no_segment(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("no such segment", rsp, 11);
- schk_leave();
- return;
- }
- void do_stack(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("stack error", rsp, 12);
- schk_leave();
- return;
- }
- void do_gp(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("general protection", rsp, 13);
- schk_leave();
- return;
- }
- void do_page_fault(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("page fault", rsp, 14);
- schk_leave();
- return;
- }
- void do_reserved(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("reserved", rsp, 15);
- schk_leave();
- return;
- }
- void do_fpu_error(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu error", rsp, 16);
- schk_leave();
- return;
- }
- void do_alignment(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("alignment error", rsp, 17);
- schk_leave();
- return;
- }
- void do_machine(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("machine check", rsp, 18);
- schk_leave();
- return;
- }
- void do_simd(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("simd error", rsp, 19);
- schk_leave();
- return;
- }
- void do_virtualization(__uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("virtualization error", rsp, 20);
- schk_leave();
- return;
- }
|