| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /* 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 <modules/tty.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_sys_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(const char *s, const __uint64_t *rsp, char nr) {
- /* should we use schk here? */
- printk(red, "int #%b: %s. \n", nr, s);
- printk(red, "rsp now: %q\n", rsp);
- printk(red, "rax\t\t\trcx\t\t\trdx\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_rax], rsp[off_rcx], rsp[off_rdx]);
- printk(red, "rbx\t\t\trbp\t\t\trsp\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_rbx], rsp[off_rbp], rsp[off_rsp]);
- printk(red, "rsi\t\t\trdi\t\t\tr8\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_rsi], rsp[off_rdi], rsp[off_r8]);
- printk(red, "r9\t\t\tr10\t\t\tr11\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_r9], rsp[off_r10], rsp[off_r11]);
-
- printk(red, "r12\t\t\tr13\t\t\tr14\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_r12], rsp[off_r13], rsp[off_r14]);
- printk(red, "r15\t\t\trip\t\t\trflags\n");
- printk(red, "%q\t%q\t%q\n", rsp[off_r15], rsp[off_rip], rsp[off_rflags]);
- printk(red, "ds\t\t\tss\n");
- printk(red, "%w\t%w\n", rsp[off_ds] & 0xffff, rsp[off_ss] & 0xffff);
- printk(red, "instruction: %q\n", *(__uint64_t *) rsp[off_rip]);
- trace_mm();
- for (;;) hlt();
- return;
- }
- void do_divide_error(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("divide error", rsp, 0);
- schk_leave();
- return;
- }
- void do_debug(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("debug", rsp, 1);
- schk_leave();
- return;
- }
- void do_nmi(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("nmi", rsp, 2);
- schk_leave();
- return;
- }
- void do_int3(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("int3", rsp, 3);
- schk_leave();
- return;
- }
- void do_overflow(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("overflow", rsp, 4);
- schk_leave();
- return;
- }
- void do_bound(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("bound", rsp, 5);
- schk_leave();
- return;
- }
- void do_ud(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("undefined instruction", rsp, 6);
- schk_leave();
- return;
- }
- void do_no_fpu(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu not present", rsp, 7);
- schk_leave();
- return;
- }
- void do_double_fault(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("double fault", rsp, 8);
- schk_leave();
- return;
- }
- void do_fpu_overrun(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu overrun", rsp, 9);
- schk_leave();
- return;
- }
- void do_invalid_tss(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("invalid tss", rsp, 10);
- schk_leave();
- return;
- }
- void do_no_segment(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("no such segment", rsp, 11);
- schk_leave();
- return;
- }
- void do_stack(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("stack error", rsp, 12);
- schk_leave();
- return;
- }
- void do_gp(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("general protection", rsp, 13);
- schk_leave();
- return;
- }
- void do_page_fault(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- printk(red, "error code: %q\n", errcode);
- if (errcode & 1)
- print(red, "page protection. \n");
- else
- print(red, "page not exists. \n");
- if (errcode & 2)
- print(red, "wr ");
- else
- print(red, "rd ");
- if (errcode & 4)
- print(red, "u ");
- else
- print(red, "k ");
- if (errcode & 8)
- print(red, "rsvd set ");
- if (errcode & 16)
- print(red, "instruction");
- putc('\n', red);
- die("page fault", rsp, 14);
- schk_leave();
- return;
- }
- void do_reserved(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("reserved", rsp, 15);
- schk_leave();
- return;
- }
- void do_fpu_error(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("fpu error", rsp, 16);
- schk_leave();
- return;
- }
- void do_alignment(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("alignment error", rsp, 17);
- schk_leave();
- return;
- }
- void do_machine(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("machine check", rsp, 18);
- schk_leave();
- return;
- }
- void do_simd(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("simd error", rsp, 19);
- schk_leave();
- return;
- }
- void do_virtualization(const __uint64_t *rsp, __uint64_t errcode) {
- schk_init();
- die("virtualization error", rsp, 20);
- schk_leave();
- return;
- }
|