1 | /* |
---|
2 | * hal_exception.c - implementation of exception handler for TSAR-MIPS32. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016, 2017) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_types.h> |
---|
25 | #include <hal_irqmask.h> |
---|
26 | #include <hal_exception.h> |
---|
27 | #include <thread.h> |
---|
28 | #include <printk.h> |
---|
29 | #include <chdev.h> |
---|
30 | #include <vmm.h> |
---|
31 | #include <errno.h> |
---|
32 | #include <scheduler.h> |
---|
33 | #include <core.h> |
---|
34 | #include <signal.h> |
---|
35 | #include <syscalls.h> |
---|
36 | #include <remote_spinlock.h> |
---|
37 | #include <mips32_uzone.h> |
---|
38 | |
---|
39 | |
---|
40 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
41 | // Extern global variables |
---|
42 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
43 | |
---|
44 | extern chdev_directory_t chdev_dir; // allocated in the kernel_init.c file. |
---|
45 | |
---|
46 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
47 | // This enum defines the global exception types after analysis by the exception handler. |
---|
48 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
49 | |
---|
50 | typedef enum |
---|
51 | { |
---|
52 | EXCP_NON_FATAL, |
---|
53 | EXCP_USER_ERROR, |
---|
54 | EXCP_KERNEL_PANIC, |
---|
55 | } |
---|
56 | exception_handling_type_t; |
---|
57 | |
---|
58 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
59 | // This enum defines the relevant subtypes for a MMU exception reported by the mips32. |
---|
60 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
61 | |
---|
62 | typedef enum |
---|
63 | { |
---|
64 | MMU_EXCP_PAGE_UNMAPPED, |
---|
65 | MMU_EXCP_USER_PRIVILEGE, |
---|
66 | MMU_EXCP_USER_EXEC, |
---|
67 | MMU_EXCP_USER_WRITE, |
---|
68 | } |
---|
69 | mmu_exception_subtype_t; |
---|
70 | |
---|
71 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
72 | // This enum defines the relevant values for XCODE field in mips32 CP0_CR register. |
---|
73 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
74 | |
---|
75 | typedef enum |
---|
76 | { |
---|
77 | XCODE_ADEL = 0x4, // Illegal address for data load |
---|
78 | XCODE_ADES = 0x5, // Illegal address for data store |
---|
79 | XCODE_IBE = 0x6, // Instruction MMU exception (can be NON-FATAL) |
---|
80 | XCODE_DBE = 0x7, // Data MMU exception (can be NON-FATAL) |
---|
81 | XCODE_RI = 0xA, // Reserved instruction exception |
---|
82 | XCODE_CPU = 0xB, // Coprocessor unusable exception (can be NON-FATAl) |
---|
83 | XCODE_OVR = 0xC, // Arithmetic Overflow exception |
---|
84 | } |
---|
85 | xcode_values_t; |
---|
86 | |
---|
87 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
88 | // This static function is called when a FPU Coprocessor Unavailable exception has been |
---|
89 | // detected for the calling thread. |
---|
90 | // It enables the FPU, It saves the current FPU context in the current owner thread |
---|
91 | // descriptor if required, and restore the FPU context from the calling thread descriptor. |
---|
92 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
93 | // @ this : pointer on faulty thread descriptor. |
---|
94 | // @ return always EXCP_NON_FATAL |
---|
95 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
96 | static error_t hal_fpu_exception( thread_t * this ) |
---|
97 | { |
---|
98 | core_t * core = this->core; |
---|
99 | |
---|
100 | // enable FPU |
---|
101 | hal_fpu_enable(); |
---|
102 | |
---|
103 | // save FPU context in current owner thread if required |
---|
104 | if( core->fpu_owner != NULL ) |
---|
105 | { |
---|
106 | if( core->fpu_owner != this ) |
---|
107 | { |
---|
108 | hal_fpu_context_save ( core->fpu_owner->fpu_context ); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // attach the FPU to the requesting thread |
---|
113 | hal_fpu_context_restore( this->fpu_context ); |
---|
114 | core->fpu_owner = this; |
---|
115 | |
---|
116 | return EXCP_NON_FATAL; |
---|
117 | |
---|
118 | } // end hal_fpu_exception() |
---|
119 | |
---|
120 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
121 | // This static function is called when an MMU exception has been detected. |
---|
122 | // It get the relevant exception arguments from the MMU. |
---|
123 | // It signal a fatal error in case of illegal access. In case of page unmapped |
---|
124 | // it checks that the faulty address belongs to a registered vseg. It update the local |
---|
125 | // vseg list from the reference cluster if required, and signal a fatal user error |
---|
126 | // in case of illegal virtual address. Finally, it updates the local page table from the |
---|
127 | // reference cluster. |
---|
128 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
129 | // @ this : pointer on faulty thread descriptor. |
---|
130 | // @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC |
---|
131 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
132 | static error_t hal_mmu_exception( thread_t * this ) |
---|
133 | { |
---|
134 | vseg_t * vseg; // vseg containing the bad_vaddr |
---|
135 | process_t * process; // local process descriptor |
---|
136 | error_t error; // return value |
---|
137 | |
---|
138 | reg_t mmu_ins_excp_code; |
---|
139 | reg_t mmu_ins_bad_vaddr; |
---|
140 | reg_t mmu_dat_excp_code; |
---|
141 | reg_t mmu_dat_bad_vaddr; |
---|
142 | |
---|
143 | intptr_t bad_vaddr; |
---|
144 | uint32_t excp_code; |
---|
145 | |
---|
146 | process = this->process; |
---|
147 | |
---|
148 | // get relevant values from MMU |
---|
149 | hal_get_mmu_excp( &mmu_ins_excp_code, |
---|
150 | &mmu_ins_bad_vaddr, |
---|
151 | &mmu_dat_excp_code, |
---|
152 | &mmu_dat_bad_vaddr ); |
---|
153 | |
---|
154 | // get exception code and faulty vaddr |
---|
155 | if( mmu_ins_excp_code ) |
---|
156 | { |
---|
157 | excp_code = mmu_ins_excp_code; |
---|
158 | bad_vaddr = mmu_ins_bad_vaddr; |
---|
159 | } |
---|
160 | else if( mmu_dat_excp_code ) |
---|
161 | { |
---|
162 | excp_code = mmu_dat_excp_code; |
---|
163 | bad_vaddr = mmu_dat_bad_vaddr; |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | return EXCP_NON_FATAL; |
---|
168 | } |
---|
169 | |
---|
170 | vmm_dmsg("\n[INFO] %s : enters for thread %x / process %x" |
---|
171 | " / bad_vaddr = %x / excep_code = %x\n", |
---|
172 | __FUNCTION__, this->trdid , process->pid , bad_vaddr , excp_code ); |
---|
173 | |
---|
174 | // on TSAR, a kernel thread should not rise an MMU exception |
---|
175 | assert( (this->type != THREAD_USER) , __FUNCTION__ , |
---|
176 | "thread %x is a kernel thread / vaddr = %x\n", this->trdid , bad_vaddr ); |
---|
177 | |
---|
178 | // vaddr must be contained in a registered vseg |
---|
179 | error = vmm_get_vseg( process , bad_vaddr , &vseg ); |
---|
180 | |
---|
181 | vmm_dmsg("\n[INFO] %s : found vseg for thread %x / vseg_min = %x / vseg_max = %x\n", |
---|
182 | __FUNCTION__ , this->trdid , vseg->min , vseg->max ); |
---|
183 | |
---|
184 | // analyse exception code |
---|
185 | if( excp_code & MMU_EXCP_PAGE_UNMAPPED ) |
---|
186 | { |
---|
187 | // enable IRQs before handling page fault |
---|
188 | hal_enable_irq( NULL ); |
---|
189 | |
---|
190 | // try to map the unmapped PTE |
---|
191 | error = vmm_handle_page_fault( process, |
---|
192 | vseg, |
---|
193 | bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); // vpn |
---|
194 | // disable IRQs |
---|
195 | hal_disable_irq( NULL ); |
---|
196 | |
---|
197 | if( error ) // not enough memory |
---|
198 | { |
---|
199 | printk("\n[ERROR] in %s for thread %x : cannot map legal vaddr = %x\n", |
---|
200 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
201 | |
---|
202 | return EXCP_USER_ERROR; |
---|
203 | } |
---|
204 | else // page fault successfully handled |
---|
205 | { |
---|
206 | vmm_dmsg("\n[INFO] %s : page fault handled for vaddr = %x in thread %x\n", |
---|
207 | __FUNCTION__ , bad_vaddr , this->trdid ); |
---|
208 | |
---|
209 | return EXCP_NON_FATAL; |
---|
210 | } |
---|
211 | } |
---|
212 | else if( excp_code & MMU_EXCP_USER_PRIVILEGE ) |
---|
213 | { |
---|
214 | printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n", |
---|
215 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
216 | |
---|
217 | return EXCP_USER_ERROR; |
---|
218 | } |
---|
219 | else if( excp_code & MMU_EXCP_USER_EXEC ) |
---|
220 | { |
---|
221 | printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n", |
---|
222 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
223 | |
---|
224 | return EXCP_USER_ERROR; |
---|
225 | } |
---|
226 | else if( excp_code & MMU_EXCP_USER_WRITE ) |
---|
227 | { |
---|
228 | printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n", |
---|
229 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
230 | |
---|
231 | return EXCP_USER_ERROR; |
---|
232 | } |
---|
233 | |
---|
234 | else // this is a kernel error => panic |
---|
235 | { |
---|
236 | printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n", |
---|
237 | __FUNCTION__ , this->trdid , excp_code , bad_vaddr ); |
---|
238 | |
---|
239 | return EXCP_KERNEL_PANIC; |
---|
240 | } |
---|
241 | |
---|
242 | } // end hal_mmu_exception() |
---|
243 | |
---|
244 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
245 | // This static function prints on the kernel terminal the saved context (core registers) |
---|
246 | // and the thread state of a faulty thread. |
---|
247 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
248 | // @ this : pointer on faulty thread descriptor. |
---|
249 | // @ regs_tbl : pointer on register array. |
---|
250 | // @ return always EXCP_NON_FATAL |
---|
251 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
252 | static void hal_exception_dump( thread_t * this, |
---|
253 | reg_t * regs_tbl ) |
---|
254 | { |
---|
255 | uint32_t save_sr; |
---|
256 | |
---|
257 | // get pointers on TXT0 chdev |
---|
258 | xptr_t txt0_xp = chdev_dir.txt[0]; |
---|
259 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
260 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
261 | |
---|
262 | // get extended pointer on remote TXT0 chdev lock |
---|
263 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
264 | |
---|
265 | // get TXT0 lock in busy waiting mode |
---|
266 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
267 | |
---|
268 | if( this->type == THREAD_USER ) |
---|
269 | nolock_printk("\n================= USER ERROR / cycle %d ====================\n", |
---|
270 | hal_time_stamp() ); |
---|
271 | else |
---|
272 | nolock_printk("\n================= KERNEL PANIC / cycle %d ==================\n", |
---|
273 | hal_time_stamp() ); |
---|
274 | |
---|
275 | nolock_printk(" thread type = %s / trdid = %x / pid %x / core[%x,%d]\n" |
---|
276 | " local locks = %d / remote locks = %d / blocked_vector = %X\n\n", |
---|
277 | thread_type_str(this->type), this->trdid, this->process->pid, local_cxy, |
---|
278 | this->core->lid, this->local_locks, this->remote_locks, this->blocked ); |
---|
279 | |
---|
280 | nolock_printk("CR %X EPC %X SR %X SP %X\n", |
---|
281 | regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]); |
---|
282 | |
---|
283 | nolock_printk("at_1 %X v0_2 %X v1_3 %X a0_4 %X a1_5 %X\n", |
---|
284 | regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]); |
---|
285 | |
---|
286 | nolock_printk("a2_6 %X a3_7 %X t0_8 %X t1_9 %X t2_10 %X\n", |
---|
287 | regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]); |
---|
288 | |
---|
289 | nolock_printk("t3_11 %X t4_12 %X t5_13 %X t6_14 %X t7_15 %X\n", |
---|
290 | regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]); |
---|
291 | |
---|
292 | nolock_printk("t8_24 %X t9_25 %X gp_28 %X c0_hi %X c0_lo %X\n", |
---|
293 | regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]); |
---|
294 | |
---|
295 | nolock_printk("s0_16 %X s1_17 %X s2_18 %X s3_19 %X s4_20 %X\n", |
---|
296 | regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]); |
---|
297 | |
---|
298 | nolock_printk("s5_21 %X s6_22 %X s7_23 %X s8_30 %X ra_31 %X\n", |
---|
299 | regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]); |
---|
300 | |
---|
301 | // release the lock |
---|
302 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
303 | |
---|
304 | } // end hal_exception_dump() |
---|
305 | |
---|
306 | |
---|
307 | /////////////////////////////////////////////////////////////////////////////// |
---|
308 | // TODO replace the hal_core_sleep() by the generic panic() function. |
---|
309 | /////////////////////////////////////////////////////////////////////////////// |
---|
310 | void hal_do_exception( thread_t * this, |
---|
311 | reg_t * regs_tbl ) |
---|
312 | { |
---|
313 | error_t error; |
---|
314 | uint32_t excCode; // 4 bits XCODE from CP0_CR |
---|
315 | |
---|
316 | // get 4 bits XCODE from CP0_CR register |
---|
317 | excCode = (regs_tbl[UZ_CR] >> 2) & 0xF; |
---|
318 | |
---|
319 | switch(excCode) |
---|
320 | { |
---|
321 | case XCODE_DBE: // can be non fatal |
---|
322 | case XCODE_IBE: // can be non fatal |
---|
323 | { |
---|
324 | error = hal_mmu_exception( this ); |
---|
325 | } |
---|
326 | break; |
---|
327 | |
---|
328 | case XCODE_CPU: // can be non fatal |
---|
329 | { |
---|
330 | if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 ) // unavailable FPU |
---|
331 | { |
---|
332 | error = hal_fpu_exception( this ); |
---|
333 | } |
---|
334 | else |
---|
335 | { |
---|
336 | error = EXCP_USER_ERROR; |
---|
337 | } |
---|
338 | } |
---|
339 | break; |
---|
340 | |
---|
341 | case XCODE_OVR: // user fatal error |
---|
342 | case XCODE_RI: // user fatal error |
---|
343 | case XCODE_ADEL: // user fatal error |
---|
344 | case XCODE_ADES: // user fatal error |
---|
345 | { |
---|
346 | error = EXCP_USER_ERROR; |
---|
347 | } |
---|
348 | break; |
---|
349 | |
---|
350 | default: |
---|
351 | { |
---|
352 | error = EXCP_KERNEL_PANIC; |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | // analyse error code |
---|
357 | if( error == EXCP_USER_ERROR ) // user error => kill user process |
---|
358 | { |
---|
359 | hal_exception_dump( this , regs_tbl ); |
---|
360 | sys_kill( this->process->pid , SIGKILL ); |
---|
361 | } |
---|
362 | else if( error == EXCP_KERNEL_PANIC ) // kernel error => kernel panic |
---|
363 | { |
---|
364 | hal_exception_dump( this , regs_tbl ); |
---|
365 | hal_core_sleep(); |
---|
366 | } |
---|
367 | } // end hal_do_exception() |
---|
368 | |
---|
369 | |
---|