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