001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018 #include "idt.h"
019 #include "irq.h"
020
021 #include <sos/assert.h>
022 #include <drivers/bochs.h>
023 #include <sos/thread.h>
024
025 #include "exception.h"
026
027
028 extern sos_vaddr_t sos_exception_wrapper_array[SOS_EXCEPT_NUM];
029
030
031 sos_exception_handler_t sos_exception_handler_array[SOS_EXCEPT_NUM] =
032 { NULL, };
033
034
035 static const char * sos_x86_exnames[] = {
036 [SOS_EXCEPT_DIVIDE_ERROR] = "Division by zero",
037 [SOS_EXCEPT_DEBUG] = "Debug",
038 [SOS_EXCEPT_NMI_INTERRUPT] = "Non Maskable Interrupt",
039 [SOS_EXCEPT_BREAKPOINT] = "Breakpoint",
040 [SOS_EXCEPT_OVERFLOW] = "Overflow",
041 [SOS_EXCEPT_BOUND_RANGE_EXCEDEED] = "Bound Range Exceeded",
042 [SOS_EXCEPT_INVALID_OPCODE] = "Invalid Opcode",
043 [SOS_EXCEPT_DEVICE_NOT_AVAILABLE] = "Device Unavailable",
044 [SOS_EXCEPT_DOUBLE_FAULT] = "Double Fault",
045 [SOS_EXCEPT_COPROCESSOR_SEGMENT_OVERRUN] = "Coprocessor Segment Overrun",
046 [SOS_EXCEPT_INVALID_TSS] = "Invalid TSS",
047 [SOS_EXCEPT_SEGMENT_NOT_PRESENT] = "Segment Not Present",
048 [SOS_EXCEPT_STACK_SEGMENT_FAULT] = "Stack Segfault",
049 [SOS_EXCEPT_GENERAL_PROTECTION] = "General Protection",
050 [SOS_EXCEPT_PAGE_FAULT] = "Page Fault",
051 [SOS_EXCEPT_INTEL_RESERVED_1] = "INTEL1",
052 [SOS_EXCEPT_FLOATING_POINT_ERROR] = "FP Error",
053 [SOS_EXCEPT_ALIGNEMENT_CHECK] = "Alignment Check",
054 [SOS_EXCEPT_MACHINE_CHECK] = "Machine Check",
055 [SOS_EXCEPT_INTEL_RESERVED_2] = "INTEL2",
056 [SOS_EXCEPT_INTEL_RESERVED_3] = "INTEL3",
057 [SOS_EXCEPT_INTEL_RESERVED_4] = "INTEL4",
058 [SOS_EXCEPT_INTEL_RESERVED_5] = "INTEL5",
059 [SOS_EXCEPT_INTEL_RESERVED_6] = "INTEL6",
060 [SOS_EXCEPT_INTEL_RESERVED_7] = "INTEL7",
061 [SOS_EXCEPT_INTEL_RESERVED_8] = "INTEL8",
062 [SOS_EXCEPT_INTEL_RESERVED_9] = "INTEL9",
063 [SOS_EXCEPT_INTEL_RESERVED_10] = "INTEL10",
064 [SOS_EXCEPT_INTEL_RESERVED_11] = "INTEL11",
065 [SOS_EXCEPT_INTEL_RESERVED_12] = "INTEL12",
066 [SOS_EXCEPT_INTEL_RESERVED_13] = "INTEL13",
067 [SOS_EXCEPT_INTEL_RESERVED_14] = "INTEL14"
068 };
069
070
071
072 static void sos_generic_ex(int exid, struct sos_cpu_state *ctxt)
073 __attribute__((noreturn));
074 static void sos_generic_ex(int exid, struct sos_cpu_state *ctxt)
075 {
076 const char *exname = sos_exception_get_name(exid);
077
078 if (sos_cpu_context_is_in_user_mode(ctxt))
079 {
080
081 sos_bochs_printf("Exception %s in User mode at instruction 0x%x (info=%x)!\n",
082 exname,
083 sos_cpu_context_get_PC(ctxt),
084 (unsigned)sos_cpu_context_get_EX_info(ctxt));
085 sos_bochs_printf("Terminating User thread\n");
086 sos_thread_exit();
087 }
088 else
089 sos_display_fatal_error("Exception %s in Kernel at instruction 0x%x (info=%x)!\n",
090 exname,
091 sos_cpu_context_get_PC(ctxt),
092 (unsigned)sos_cpu_context_get_EX_info(ctxt));
093 }
094
095
096 sos_ret_t sos_exception_subsystem_setup(void)
097 {
098 sos_ret_t retval;
099 int exid;
100
101
102
103 for (exid = 0 ; exid < SOS_EXCEPT_NUM ; exid ++)
104 {
105
106 if (exid == SOS_EXCEPT_DOUBLE_FAULT)
107 continue;
108
109 retval = sos_exception_set_routine(exid, sos_generic_ex);
110 if (SOS_OK != retval)
111 return retval;
112 }
113
114
115
116
117
118
119 return sos_idt_set_handler(SOS_EXCEPT_BASE + SOS_EXCEPT_DOUBLE_FAULT,
120 (sos_vaddr_t) sos_exception_wrapper_array[SOS_EXCEPT_DOUBLE_FAULT],
121 0 );
122 }
123
124
125 sos_ret_t sos_exception_set_routine(int exception_number,
126 sos_exception_handler_t routine)
127 {
128 sos_ret_t retval;
129 sos_ui32_t flags;
130
131 if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM))
132 return -SOS_EINVAL;
133
134
135 if (exception_number == SOS_EXCEPT_DOUBLE_FAULT)
136 return -SOS_ENOSUP;
137
138 sos_disable_IRQs(flags);
139
140 retval = SOS_OK;
141
142
143 sos_exception_handler_array[exception_number] = routine;
144
145
146
147 if (routine != NULL)
148 retval
149 = sos_idt_set_handler(SOS_EXCEPT_BASE + exception_number,
150 (sos_vaddr_t) sos_exception_wrapper_array[exception_number],
151 0 );
152 else
153 retval
154 = sos_idt_set_handler(SOS_EXCEPT_BASE + exception_number,
155 (sos_vaddr_t)NULL ,
156 0 );
157
158 sos_restore_IRQs(flags);
159 return retval;
160 }
161
162
163 sos_exception_handler_t sos_exception_get_routine(int exception_number)
164 {
165 if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM))
166 return NULL;
167
168
169 if (exception_number == SOS_EXCEPT_DOUBLE_FAULT)
170 return NULL;
171
172
173 return sos_exception_handler_array[exception_number];
174 }
175
176
177 const char * sos_exception_get_name(int exception_number)
178 {
179 if ((exception_number < 0) || (exception_number >= SOS_EXCEPT_NUM))
180 return NULL;
181
182 return sos_x86_exnames[exception_number];
183 }