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_special.h> |
---|
27 | #include <hal_exception.h> |
---|
28 | #include <thread.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <chdev.h> |
---|
31 | #include <vmm.h> |
---|
32 | #include <errno.h> |
---|
33 | #include <scheduler.h> |
---|
34 | #include <core.h> |
---|
35 | #include <signal.h> |
---|
36 | #include <syscalls.h> |
---|
37 | #include <remote_spinlock.h> |
---|
38 | #include <hal_kentry.h> |
---|
39 | |
---|
40 | |
---|
41 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
42 | // Extern global variables |
---|
43 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
44 | |
---|
45 | extern chdev_directory_t chdev_dir; // allocated in the kernel_init.c file. |
---|
46 | |
---|
47 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
48 | // This enum defines the global exception types after analysis by the exception handler. |
---|
49 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
50 | |
---|
51 | typedef enum |
---|
52 | { |
---|
53 | EXCP_NON_FATAL, |
---|
54 | EXCP_USER_ERROR, |
---|
55 | EXCP_KERNEL_PANIC, |
---|
56 | } |
---|
57 | exception_handling_type_t; |
---|
58 | |
---|
59 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
60 | // This enum defines the mask values for an MMU exception code reported by the mips32. |
---|
61 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
62 | |
---|
63 | typedef enum |
---|
64 | { |
---|
65 | MMU_WRITE_PT1_UNMAPPED = 0x0001, |
---|
66 | MMU_WRITE_PT2_UNMAPPED = 0x0002, |
---|
67 | MMU_WRITE_PRIVILEGE_VIOLATION = 0x0004, |
---|
68 | MMU_WRITE_ACCESS_VIOLATION = 0x0008, |
---|
69 | MMU_WRITE_UNDEFINED_XTN = 0x0020, |
---|
70 | MMU_WRITE_PT1_ILLEGAL_ACCESS = 0x0040, |
---|
71 | MMU_WRITE_PT2_ILLEGAL_ACCESS = 0x0080, |
---|
72 | MMU_WRITE_DATA_ILLEGAL_ACCESS = 0x0100, |
---|
73 | |
---|
74 | MMU_READ_PT1_UNMAPPED = 0x1001, |
---|
75 | MMU_READ_PT2_UNMAPPED = 0x1002, |
---|
76 | MMU_READ_PRIVILEGE_VIOLATION = 0x1004, |
---|
77 | MMU_READ_EXEC_VIOLATION = 0x1010, |
---|
78 | MMU_READ_UNDEFINED_XTN = 0x1020, |
---|
79 | MMU_READ_PT1_ILLEGAL_ACCESS = 0x1040, |
---|
80 | MMU_READ_PT2_ILLEGAL_ACCESS = 0x1080, |
---|
81 | MMU_READ_DATA_ILLEGAL_ACCESS = 0x1100, |
---|
82 | } |
---|
83 | mmu_exception_subtype_t; |
---|
84 | |
---|
85 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
86 | // This enum defines the relevant values for XCODE field in mips32 CP0_CR register. |
---|
87 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
88 | |
---|
89 | typedef enum |
---|
90 | { |
---|
91 | XCODE_ADEL = 0x4, // Illegal address for data load |
---|
92 | XCODE_ADES = 0x5, // Illegal address for data store |
---|
93 | XCODE_IBE = 0x6, // Instruction MMU exception (can be NON-FATAL) |
---|
94 | XCODE_DBE = 0x7, // Data MMU exception (can be NON-FATAL) |
---|
95 | XCODE_RI = 0xA, // Reserved instruction exception |
---|
96 | XCODE_CPU = 0xB, // Coprocessor unusable exception (can be NON-FATAl) |
---|
97 | XCODE_OVR = 0xC, // Arithmetic Overflow exception |
---|
98 | } |
---|
99 | xcode_values_t; |
---|
100 | |
---|
101 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
102 | // This function is called when a FPU Coprocessor Unavailable exception has been |
---|
103 | // detected for the calling thread. |
---|
104 | // It enables the FPU, It saves the current FPU context in the current owner thread |
---|
105 | // descriptor if required, and restore the FPU context from the calling thread descriptor. |
---|
106 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
107 | // @ this : pointer on faulty thread descriptor. |
---|
108 | // @ return always EXCP_NON_FATAL |
---|
109 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
110 | error_t hal_fpu_exception( thread_t * this ) |
---|
111 | { |
---|
112 | core_t * core = this->core; |
---|
113 | |
---|
114 | // enable FPU |
---|
115 | hal_fpu_enable(); |
---|
116 | |
---|
117 | // save FPU context in current owner thread if required |
---|
118 | if( core->fpu_owner != NULL ) |
---|
119 | { |
---|
120 | if( core->fpu_owner != this ) |
---|
121 | { |
---|
122 | hal_fpu_context_save ( core->fpu_owner->fpu_context ); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | // attach the FPU to the requesting thread |
---|
127 | hal_fpu_context_restore( this->fpu_context ); |
---|
128 | core->fpu_owner = this; |
---|
129 | |
---|
130 | return EXCP_NON_FATAL; |
---|
131 | |
---|
132 | } // end hal_fpu_exception() |
---|
133 | |
---|
134 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
135 | // This function is called when an MMU exception has been detected. |
---|
136 | // It get the relevant exception arguments from the MMU. |
---|
137 | // It signal a fatal error in case of illegal access. In case of page unmapped |
---|
138 | // it checks that the faulty address belongs to a registered vseg. It update the local |
---|
139 | // vseg list from the reference cluster if required, and signal a fatal user error |
---|
140 | // in case of illegal virtual address. Finally, it updates the local page table from the |
---|
141 | // reference cluster. |
---|
142 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
143 | // @ this : pointer on faulty thread descriptor. |
---|
144 | // @ is_ins : IBE if true / DBE if false. |
---|
145 | // @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC |
---|
146 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
147 | error_t hal_mmu_exception( thread_t * this, |
---|
148 | bool_t is_ins ) |
---|
149 | { |
---|
150 | process_t * process; |
---|
151 | error_t error; |
---|
152 | |
---|
153 | uint32_t mmu_ins_excp_code; |
---|
154 | uint32_t mmu_ins_bad_vaddr; |
---|
155 | uint32_t mmu_dat_excp_code; |
---|
156 | uint32_t mmu_dat_bad_vaddr; |
---|
157 | |
---|
158 | uint32_t bad_vaddr; |
---|
159 | uint32_t excp_code; |
---|
160 | |
---|
161 | process = this->process; |
---|
162 | |
---|
163 | // get relevant values from MMU |
---|
164 | hal_get_mmu_excp( &mmu_ins_excp_code, |
---|
165 | &mmu_ins_bad_vaddr, |
---|
166 | &mmu_dat_excp_code, |
---|
167 | &mmu_dat_bad_vaddr ); |
---|
168 | |
---|
169 | // get exception code and faulty vaddr, depending on IBE/DBE |
---|
170 | if( is_ins ) |
---|
171 | { |
---|
172 | excp_code = mmu_ins_excp_code; |
---|
173 | bad_vaddr = mmu_ins_bad_vaddr; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | excp_code = mmu_dat_excp_code; |
---|
178 | bad_vaddr = mmu_dat_bad_vaddr; |
---|
179 | } |
---|
180 | |
---|
181 | excp_dmsg("\n[DBG] %s : core[%x,%d] / is_ins %d / code %x / vaddr %x\n", |
---|
182 | __FUNCTION__ , local_cxy , this->core->lid , is_ins, excp_code, bad_vaddr ); |
---|
183 | |
---|
184 | // analyse exception code |
---|
185 | switch( excp_code ) |
---|
186 | { |
---|
187 | case MMU_WRITE_PT1_UNMAPPED: // non fatal |
---|
188 | case MMU_WRITE_PT2_UNMAPPED: |
---|
189 | case MMU_READ_PT1_UNMAPPED: |
---|
190 | case MMU_READ_PT2_UNMAPPED: |
---|
191 | { |
---|
192 | // try to map the unmapped PTE |
---|
193 | error = vmm_handle_page_fault( process, |
---|
194 | bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); // vpn |
---|
195 | if( error ) |
---|
196 | { |
---|
197 | printk("\n[ERROR] in %s for thread %x : cannot map vaddr = %x\n", |
---|
198 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
199 | |
---|
200 | return EXCP_USER_ERROR; |
---|
201 | } |
---|
202 | else // page fault successfull |
---|
203 | { |
---|
204 | |
---|
205 | excp_dmsg("\n[DBG] %s : core[%x,%d] / page-fault handled for vaddr = %x\n", |
---|
206 | __FUNCTION__ , local_cxy , this->core->lid , bad_vaddr ); |
---|
207 | |
---|
208 | return EXCP_NON_FATAL; |
---|
209 | } |
---|
210 | } |
---|
211 | case MMU_WRITE_PRIVILEGE_VIOLATION: // illegal access user error |
---|
212 | case MMU_READ_PRIVILEGE_VIOLATION: |
---|
213 | { |
---|
214 | printk("\n[ERROR] in %s for thread %x : illegal user access to vaddr = %x\n", |
---|
215 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
216 | |
---|
217 | return EXCP_USER_ERROR; |
---|
218 | } |
---|
219 | case MMU_WRITE_ACCESS_VIOLATION: // user error or Copy-on-Write |
---|
220 | { |
---|
221 | // access page table to get GPT_COW flag |
---|
222 | bool_t cow = hal_gpt_pte_is_cow( &(process->vmm.gpt), |
---|
223 | bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); // vpn |
---|
224 | |
---|
225 | if( cow ) // Copy-on-Write |
---|
226 | { |
---|
227 | // try to allocate and copy the page |
---|
228 | error = vmm_copy_on_write( process, |
---|
229 | bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); // vpn |
---|
230 | if( error ) |
---|
231 | { |
---|
232 | printk("\n[ERROR] in %s for thread %x : cannot cow vaddr = %x\n", |
---|
233 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
234 | |
---|
235 | return EXCP_USER_ERROR; |
---|
236 | } |
---|
237 | else // Copy on write successfull |
---|
238 | { |
---|
239 | |
---|
240 | excp_dmsg("\n[DBG] %s : core[%x,%d] / copy-on-write handled for vaddr = %x\n", |
---|
241 | __FUNCTION__ , local_cxy , this->core->lid , bad_vaddr ); |
---|
242 | |
---|
243 | return EXCP_NON_FATAL; |
---|
244 | } |
---|
245 | } |
---|
246 | else // non writable user error |
---|
247 | { |
---|
248 | printk("\n[ERROR] in %s for thread %x : write to non-writable vaddr = %x\n", |
---|
249 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
250 | |
---|
251 | return EXCP_USER_ERROR; |
---|
252 | } |
---|
253 | } |
---|
254 | case MMU_READ_EXEC_VIOLATION: // user error |
---|
255 | { |
---|
256 | printk("\n[ERROR] in %s for thread %x : read to non-executable vaddr = %x\n", |
---|
257 | __FUNCTION__ , this->trdid , bad_vaddr ); |
---|
258 | |
---|
259 | return EXCP_USER_ERROR; |
---|
260 | } |
---|
261 | default: // this is a kernel error => panic |
---|
262 | { |
---|
263 | printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n", |
---|
264 | __FUNCTION__ , this->trdid , excp_code , bad_vaddr ); |
---|
265 | |
---|
266 | return EXCP_KERNEL_PANIC; |
---|
267 | } |
---|
268 | } |
---|
269 | } // end hal_mmu_exception() |
---|
270 | |
---|
271 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
272 | // This static function prints on the kernel terminal the saved context (core registers) |
---|
273 | // and the thread state of a faulty thread. |
---|
274 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
275 | // @ this : pointer on faulty thread descriptor. |
---|
276 | // @ regs_tbl : pointer on register array. |
---|
277 | // @ error : EXCP_USER_ERROR or EXCP_KERNEL_PANIC |
---|
278 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
279 | static void hal_exception_dump( thread_t * this, |
---|
280 | reg_t * regs_tbl, |
---|
281 | error_t error ) |
---|
282 | { |
---|
283 | uint32_t save_sr; |
---|
284 | core_t * core = this->core; |
---|
285 | |
---|
286 | // get pointers on TXT0 chdev |
---|
287 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
288 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
289 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
290 | |
---|
291 | // get extended pointer on remote TXT0 chdev lock |
---|
292 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
293 | |
---|
294 | // get TXT0 lock in busy waiting mode |
---|
295 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
296 | |
---|
297 | if( error == EXCP_USER_ERROR ) |
---|
298 | { |
---|
299 | nolock_printk("\n========= USER ERROR / core[%x,%d] / cycle %d ==============\n", |
---|
300 | local_cxy , core->lid , (uint32_t)hal_get_cycles() ); |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | nolock_printk("\n======= KERNEL PANIC / core[%x,%d] / cycle %d ==============\n", |
---|
305 | local_cxy , core->lid , (uint32_t)hal_get_cycles() ); |
---|
306 | } |
---|
307 | |
---|
308 | nolock_printk(" thread type = %s / trdid = %x / pid %x / core[%x,%d]\n" |
---|
309 | " local locks = %d / remote locks = %d / blocked_vector = %X\n\n", |
---|
310 | thread_type_str(this->type), this->trdid, this->process->pid, local_cxy, |
---|
311 | this->core->lid, this->local_locks, this->remote_locks, this->blocked ); |
---|
312 | |
---|
313 | nolock_printk("cp0_cr %X cp0_epc %X cp0_sr %X cp2_mode %X\n", |
---|
314 | regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_MODE]); |
---|
315 | |
---|
316 | nolock_printk("at_01 %X v0_2 %X v1_3 %X a0_4 %X a1_5 %X\n", |
---|
317 | regs_tbl[UZ_AT],regs_tbl[UZ_V0],regs_tbl[UZ_V1],regs_tbl[UZ_A0],regs_tbl[UZ_A1]); |
---|
318 | |
---|
319 | nolock_printk("a2_6 %X a3_7 %X t0_8 %X t1_9 %X t2_10 %X\n", |
---|
320 | regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]); |
---|
321 | |
---|
322 | nolock_printk("t3_11 %X t4_12 %X t5_13 %X t6_14 %X t7_15 %X\n", |
---|
323 | regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]); |
---|
324 | |
---|
325 | nolock_printk("s0_16 %X s1_17 %X s2_18 %X s3_19 %X s4_20 %X\n", |
---|
326 | regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]); |
---|
327 | |
---|
328 | nolock_printk("s5_21 %X s6_22 %X s7_23 %X s8_24 %X ra_25 %X\n", |
---|
329 | regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_T8],regs_tbl[UZ_T9]); |
---|
330 | |
---|
331 | nolock_printk("gp_28 %X sp_29 %X S8_30 %X ra_31 %X\n", |
---|
332 | regs_tbl[UZ_GP],regs_tbl[UZ_SP],regs_tbl[UZ_S8],regs_tbl[UZ_RA]); |
---|
333 | |
---|
334 | // release the lock |
---|
335 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
336 | |
---|
337 | } // end hal_exception_dump() |
---|
338 | |
---|
339 | |
---|
340 | /////////////////////////////////////////////////////////////////////////////// |
---|
341 | // TODO replace the hal_core_sleep() by the generic panic() function. |
---|
342 | /////////////////////////////////////////////////////////////////////////////// |
---|
343 | void hal_do_exception( thread_t * this, |
---|
344 | reg_t * regs_tbl ) |
---|
345 | { |
---|
346 | error_t error; |
---|
347 | uint32_t excCode; // 4 bits XCODE from CP0_CR |
---|
348 | |
---|
349 | // get 4 bits XCODE from CP0_CR register |
---|
350 | excCode = (regs_tbl[UZ_CR] >> 2) & 0xF; |
---|
351 | |
---|
352 | excp_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / xcode %x / cycle %d\n", |
---|
353 | __FUNCTION__, local_cxy, this->core->lid, this->trdid, |
---|
354 | this->process->pid, excCode, (uint32_t)hal_get_cycle() ); |
---|
355 | |
---|
356 | switch(excCode) |
---|
357 | { |
---|
358 | case XCODE_DBE: // can be non fatal |
---|
359 | { |
---|
360 | error = hal_mmu_exception( this , false ); // data MMU exception |
---|
361 | break; |
---|
362 | } |
---|
363 | case XCODE_IBE: // can be non fatal |
---|
364 | { |
---|
365 | error = hal_mmu_exception( this , true ); // ins MMU exception |
---|
366 | break; |
---|
367 | } |
---|
368 | case XCODE_CPU: // can be non fatal |
---|
369 | { |
---|
370 | if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 ) // unavailable FPU |
---|
371 | { |
---|
372 | error = hal_fpu_exception( this ); |
---|
373 | } |
---|
374 | else |
---|
375 | { |
---|
376 | error = EXCP_USER_ERROR; |
---|
377 | } |
---|
378 | break; |
---|
379 | } |
---|
380 | case XCODE_OVR: // user fatal error |
---|
381 | case XCODE_RI: // user fatal error |
---|
382 | case XCODE_ADEL: // user fatal error |
---|
383 | case XCODE_ADES: // user fatal error |
---|
384 | { |
---|
385 | error = EXCP_USER_ERROR; |
---|
386 | break; |
---|
387 | } |
---|
388 | default: |
---|
389 | { |
---|
390 | error = EXCP_KERNEL_PANIC; |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | // analyse error code |
---|
395 | if( error == EXCP_USER_ERROR ) // user error => kill user process |
---|
396 | { |
---|
397 | hal_exception_dump( this , regs_tbl , error ); |
---|
398 | |
---|
399 | // FIXME : replace this loop by sys_kill() |
---|
400 | while( 1 ) asm volatile ("nop"); |
---|
401 | // sys_kill( this->process->pid , SIGKILL ); |
---|
402 | } |
---|
403 | else if( error == EXCP_KERNEL_PANIC ) // kernel error => kernel panic |
---|
404 | { |
---|
405 | hal_exception_dump( this , regs_tbl , error ); |
---|
406 | hal_core_sleep(); |
---|
407 | } |
---|
408 | |
---|
409 | excp_dmsg("\n[DBG] %s : core[%x,%d] exit / thread %x in process %x / cycle %d\n", |
---|
410 | __FUNCTION__, local_cxy, this->core->lid, this->trdid, this->process->pid, hal_time_stamp() ); |
---|
411 | |
---|
412 | } // end hal_do_exception() |
---|
413 | |
---|
414 | |
---|