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