f4bebae4159490173e9df5c693421ed503890121
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / intr_routines.c
1 #include <arch/x86/interrupts.h>
2
3 #include <lunaix/isrm.h>
4 #include <lunaix/lxconsole.h>
5 #include <lunaix/process.h>
6 #include <lunaix/sched.h>
7 #include <lunaix/spike.h>
8 #include <lunaix/syslog.h>
9 #include <lunaix/tty/tty.h>
10
11 #include <klibc/stdio.h>
12
13 #include <hal/apic.h>
14
15 LOG_MODULE("INTR")
16
17 extern void
18 intr_routine_page_fault(const isr_param* param);
19
20 extern u32_t debug_resv;
21
22 void
23 __print_panic_msg(const char* msg, const isr_param* param)
24 {
25     struct exec_param* execp = param->execp;
26     kprint_panic("  INT %u: (%x) [%p: %p] %s",
27                  execp->vector,
28                  execp->err_code,
29                  execp->cs,
30                  execp->eip,
31                  msg);
32 }
33
34 void
35 intr_routine_divide_zero(const isr_param* param)
36 {
37     console_flush();
38     __print_panic_msg("Divide by zero!", param);
39     spin();
40 }
41
42 void
43 intr_routine_general_protection(const isr_param* param)
44 {
45     kprintf(KERROR "Pid: %d\n", __current->pid);
46     kprintf(KERROR "Addr: %p\n", (&debug_resv)[0]);
47     kprintf(KERROR "Expected: %p\n", (&debug_resv)[1]);
48     console_flush();
49     __print_panic_msg("General Protection", param);
50     spin();
51 }
52
53 void
54 intr_routine_sys_panic(const isr_param* param)
55 {
56     console_flush();
57     __print_panic_msg((char*)(param->registers.edi), param);
58     spin();
59 }
60
61 void
62 intr_routine_fallback(const isr_param* param)
63 {
64     console_flush();
65     __print_panic_msg("Unknown Interrupt", param);
66     spin();
67 }
68
69 /**
70  * @brief ISR for Spurious interrupt
71  *
72  * @param isr_param passed by CPU
73  */
74 void
75 intr_routine_apic_spi(const isr_param* param)
76 {
77     // FUTURE: do nothing for now
78 }
79
80 void
81 intr_routine_apic_error(const isr_param* param)
82 {
83     u32_t error_reg = apic_read_reg(APIC_ESR);
84     char buf[32];
85     ksprintf(buf, "APIC error, ESR=0x%x", error_reg);
86     console_flush();
87     __print_panic_msg(buf, param);
88     spin();
89 }
90
91 void
92 intr_routine_sched(const isr_param* param)
93 {
94     schedule();
95 }
96
97 void
98 intr_routine_init()
99 {
100     isrm_bindiv(FAULT_DIVISION_ERROR, intr_routine_divide_zero);
101     isrm_bindiv(FAULT_GENERAL_PROTECTION, intr_routine_general_protection);
102     isrm_bindiv(FAULT_PAGE_FAULT, intr_routine_page_fault);
103     isrm_bindiv(FAULT_STACK_SEG_FAULT, intr_routine_page_fault);
104
105     isrm_bindiv(LUNAIX_SYS_PANIC, intr_routine_sys_panic);
106     isrm_bindiv(LUNAIX_SCHED, intr_routine_sched);
107
108     isrm_bindiv(APIC_SPIV_IV, intr_routine_apic_spi);
109     isrm_bindiv(APIC_ERROR_IV, intr_routine_apic_error);
110 }