source: trunk/hal/tsar_mips32/core/hal_exception.c @ 494

Last change on this file since 494 was 492, checked in by viala@…, 6 years ago

Refactoring assert calling to conform with new assert macro.

Made with this command for the general case.
find ./kernel/ hal/ -name "*.c" | xargs sed -i -e '/assert(/ s/,[ ]*FUNCTION[ ]*,/,/'

And some done by hand.

File size: 19.6 KB
Line 
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_kernel_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 <syscalls.h>
36#include <shared_syscalls.h>
37#include <remote_spinlock.h>
38#include <hal_kentry.h>
39
40
41//////////////////////////////////////////////////////////////////////////////////////////
42//  Extern global variables
43//////////////////////////////////////////////////////////////////////////////////////////
44
45extern   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
51typedef enum
52{
53    EXCP_NON_FATAL,
54    EXCP_USER_ERROR,
55    EXCP_KERNEL_PANIC,
56}
57exception_handling_type_t;
58
59//////////////////////////////////////////////////////////////////////////////////////////
60// This enum defines the mask values for an MMU exception code reported by the mips32.
61//////////////////////////////////////////////////////////////////////////////////////////
62
63typedef 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}
83mmu_exception_subtype_t;
84
85//////////////////////////////////////////////////////////////////////////////////////////
86// This enum defines the relevant values for XCODE field in mips32 CP0_CR register.
87//////////////////////////////////////////////////////////////////////////////////////////
88
89typedef 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}
99xcode_values_t;
100
101/////////////////////////////////////////////
102char * hal_mmu_exception_str( uint32_t code )
103{
104    if     ( code == MMU_WRITE_PT1_UNMAPPED        ) return "WRITE_PT1_UNMAPPED";
105    else if( code == MMU_WRITE_PT2_UNMAPPED        ) return "WRITE_PT2_UNMAPPED";
106    else if( code == MMU_WRITE_PRIVILEGE_VIOLATION ) return "WRITE_PRIVILEGE_VIOLATION";
107    else if( code == MMU_WRITE_ACCESS_VIOLATION    ) return "WRITE_ACCESS_VIOLATION";
108    else if( code == MMU_WRITE_UNDEFINED_XTN       ) return "WRITE_UNDEFINED_XTN";
109    else if( code == MMU_WRITE_PT1_ILLEGAL_ACCESS  ) return "WRITE_PT1_ILLEGAL_ACCESS";
110    else if( code == MMU_WRITE_PT2_ILLEGAL_ACCESS  ) return "WRITE_PT2_ILLEGAL_ACCESS";
111    else if( code == MMU_WRITE_DATA_ILLEGAL_ACCESS ) return "WRITE_DATA_ILLEGAL_ACCESS";
112    else if( code == MMU_READ_PT1_UNMAPPED         ) return "READ_PT1_UNMAPPED";
113    else if( code == MMU_READ_PT2_UNMAPPED         ) return "READ_PT2_UNMAPPED";
114    else if( code == MMU_READ_PRIVILEGE_VIOLATION  ) return "READ_PRIVILEGE_VIOLATION";
115    else if( code == MMU_READ_EXEC_VIOLATION       ) return "READ_EXEC_VIOLATION";
116    else if( code == MMU_READ_UNDEFINED_XTN        ) return "READ_UNDEFINED_XTN";
117    else if( code == MMU_READ_PT1_ILLEGAL_ACCESS   ) return "READ_PT1_ILLEGAL_ACCESS";
118    else if( code == MMU_READ_PT2_ILLEGAL_ACCESS   ) return "READ_PT2_ILLEGAL_ACCESS";
119    else if( code == MMU_READ_DATA_ILLEGAL_ACCESS  ) return "READ_DATA_ILLEGAL_ACCESS";
120    else                                             return "undefined";
121}
122
123//////////////////////////////////////////////////////////////////////////////////////////
124// This function is called when a FPU Coprocessor Unavailable exception has been
125// detected for the calling thread.
126// It enables the FPU, It saves the current FPU context in the current owner thread
127// descriptor if required, and restore the FPU context from the calling thread descriptor.
128//////////////////////////////////////////////////////////////////////////////////////////
129// @ this     : pointer on faulty thread descriptor.
130// @ return always EXCP_NON_FATAL
131//////////////////////////////////////////////////////////////////////////////////////////
132error_t hal_fpu_exception( thread_t * this )
133{
134        core_t   * core = this->core; 
135
136    // enable FPU (in core SR) 
137        hal_fpu_enable();
138
139    // save FPU register values in current owner thread if required
140        if( core->fpu_owner != NULL )
141    {
142        if( core->fpu_owner != this )
143            {
144            // save the FPU registers to current owner thread context
145                    hal_fpu_context_save( XPTR( local_cxy , core->fpu_owner ) );
146
147            // restore FPU registers from requesting thread context
148                hal_fpu_context_restore( this );
149
150            // attach the FPU to the requesting thread
151                core->fpu_owner = this;
152        }
153        }
154    else
155    {
156        // restore FPU registers from requesting thread context
157            hal_fpu_context_restore( this );
158
159        // attach the FPU to the requesting thread
160            core->fpu_owner = this;
161    }
162
163        return EXCP_NON_FATAL;
164
165}  // end hal_fpu_exception()
166
167//////////////////////////////////////////////////////////////////////////////////////////
168// This function is called when an MMU exception has been detected (IBE / DBE).
169// It get the relevant exception arguments from the MMU.
170// It signal a fatal error in case of illegal access. In case of page unmapped
171// it checks that the faulty address belongs to a registered vseg. It update the local
172// vseg list from the reference cluster if required, and signal a fatal user error
173// in case of illegal virtual address. Finally, it updates the local page table from the
174// reference cluster.
175//////////////////////////////////////////////////////////////////////////////////////////
176// @ this     : pointer on faulty thread descriptor.
177// @ excPC    :
178// @ is_ins   : IBE if true / DBE if false.
179// @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC
180//////////////////////////////////////////////////////////////////////////////////////////
181error_t hal_mmu_exception( thread_t * this,
182                           uint32_t   excPC,
183                           bool_t     is_ins ) 
184{
185        process_t      * process;
186    error_t          error;
187
188    uint32_t         mmu_ins_excp_code;
189    uint32_t         mmu_ins_bad_vaddr;
190    uint32_t         mmu_dat_excp_code;
191    uint32_t         mmu_dat_bad_vaddr;
192
193    uint32_t         bad_vaddr;
194    uint32_t         excp_code;
195       
196    process = this->process;
197
198    // get relevant values from MMU
199        hal_get_mmu_excp( &mmu_ins_excp_code,
200                          &mmu_ins_bad_vaddr,
201                          &mmu_dat_excp_code, 
202                          &mmu_dat_bad_vaddr );
203
204    // get exception code and faulty vaddr, depending on IBE/DBE
205    if( is_ins )
206    {
207        excp_code = mmu_ins_excp_code;
208        bad_vaddr = mmu_ins_bad_vaddr;
209    }
210    else 
211    {
212        excp_code = mmu_dat_excp_code;
213        bad_vaddr = mmu_dat_bad_vaddr;
214    }
215
216#if DEBUG_HAL_EXCEPTIONS
217uint32_t cycle = (uint32_t)hal_get_cycles();
218if( DEBUG_HAL_EXCEPTIONS < cycle )
219printk("\n[DBG] %s : thread %x in process %x enter / is_ins %d / %s / vaddr %x / cycle %d\n",
220__FUNCTION__, this->trdid, process->pid,
221is_ins, hal_mmu_exception_str(excp_code), bad_vaddr, cycle);
222#endif
223
224   // analyse exception code
225    switch( excp_code )
226    {
227        case MMU_WRITE_PT1_UNMAPPED:      // non fatal
228        case MMU_WRITE_PT2_UNMAPPED:
229        case MMU_READ_PT1_UNMAPPED:
230        case MMU_READ_PT2_UNMAPPED:
231        {
232            // try to map the unmapped PTE
233            error = vmm_handle_page_fault( process, 
234                                           bad_vaddr >> CONFIG_PPM_PAGE_SHIFT,  // vpn
235                                           false );                             // not a COW
236            if( error )   
237            {
238                printk("\n[USER ERROR] in %s for thread %x in process %x\n"
239                "   cannot map vaddr = %x / is_ins %d / epc %x\n",
240                __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
241
242                        return EXCP_USER_ERROR;
243            } 
244            else            // page fault successfull
245            {
246
247#if DEBUG_HAL_EXCEPTIONS
248cycle = (uint32_t)hal_get_cycles();
249if( DEBUG_HAL_EXCEPTIONS < cycle )
250printk("\n[DBG] %s : thread %x in process %x exit / page-fault handled for vaddr = %x\n",
251__FUNCTION__, this->trdid, process->pid, bad_vaddr );
252#endif
253 
254                return EXCP_NON_FATAL;
255            }
256        }
257        case MMU_WRITE_PRIVILEGE_VIOLATION:  // illegal access user error
258        case MMU_READ_PRIVILEGE_VIOLATION:
259        {
260            printk("\n[USER ERROR] in %s for thread %x in process %x\n"
261            "   illegal user access to vaddr = %x / is_ins %d / epc %x\n",
262            __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
263
264            return EXCP_USER_ERROR;
265        }
266        case MMU_WRITE_ACCESS_VIOLATION:     // user error, or Copy-on-Write
267        {
268            // access page table to get GPT_COW flag
269            bool_t cow = hal_gpt_pte_is_cow( &(process->vmm.gpt),
270                                             bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); 
271
272            if( cow )                        // Copy-on-Write
273            {
274                // try to allocate and copy the page
275                error = vmm_handle_page_fault( process,
276                                               bad_vaddr >> CONFIG_PPM_PAGE_SHIFT,  // vpn
277                                               true );                              // COW
278                if( error )
279                {
280                    printk("\n[USER ERROR] in %s for thread %x in process %x\n"
281                    "   cannot cow vaddr = %x / is_ins %d / epc %x\n",
282                    __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
283
284                            return EXCP_USER_ERROR;
285                }
286                else         // Copy on write successfull
287                {
288
289#if DEBUG_HAL_EXCEPTIONS
290cycle = (uint32_t)hal_get_cycles();
291if( DEBUG_HAL_EXCEPTIONS < cycle )
292printk("\n[DBG] %s : thread %x in process %x exit / copy-on-write handled for vaddr = %x\n",
293__FUNCTION__, this->trdid, process->pid, bad_vaddr );
294#endif
295
296                    return EXCP_NON_FATAL;
297                } 
298            }
299            else                             // non writable user error
300            {
301                printk("\n[USER ERROR] in %s for thread %x in process %x\n"
302                "   non-writable vaddr = %x / is_ins %d / epc %x\n",
303                __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
304
305                return EXCP_USER_ERROR;
306            }
307        }
308        case MMU_READ_EXEC_VIOLATION:        // user error
309        {
310            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
311            "   non-executable vaddr = %x / is_ins %d / epc %x\n",
312            __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
313
314            return EXCP_USER_ERROR;
315        }
316        default:                             // this is a kernel error   
317        {
318            printk("\n[KERNEL ERROR] in %s for thread %x in process %x\n"
319            "  epc %x / badvaddr %x / is_ins %d\n",
320            __FUNCTION__, this->trdid, this->process->pid, excPC, bad_vaddr, is_ins );
321
322            return EXCP_KERNEL_PANIC;
323        }
324    } 
325} // end hal_mmu_exception()
326
327//////////////////////////////////////////////////////////////////////////////////////////
328// This static function prints on the kernel terminal the saved context (core registers)
329// and the thread state of a faulty thread.
330//////////////////////////////////////////////////////////////////////////////////////////
331// @ this     : pointer on faulty thread descriptor.
332// @ uzone    : pointer on register array.
333// @ error    : EXCP_USER_ERROR or EXCP_KERNEL_PANIC
334//////////////////////////////////////////////////////////////////////////////////////////
335static void hal_exception_dump( thread_t * this,
336                                reg_t    * uzone,
337                                error_t    error )
338{
339    uint32_t    save_sr;
340    core_t    * core    = this->core;
341    process_t * process = this->process;
342
343    // get pointers on TXT0 chdev
344    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
345    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
346    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
347
348    // get extended pointer on remote TXT0 chdev lock
349    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
350
351    // get TXT0 lock in busy waiting mode
352    remote_spinlock_lock_busy( lock_xp , &save_sr );
353
354    if( error == EXCP_USER_ERROR )
355    {
356        nolock_printk("\n=== USER ERROR / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
357        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
358    }
359    else
360    {
361        nolock_printk("\n=== KERNEL ERROR / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
362        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
363    }
364
365        nolock_printk("local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
366    this->local_locks, this->remote_locks, this->blocked );
367
368    nolock_printk("c0_cr   %X  c0_epc  %X  c0_sr  %X  c0_th  %X\n",
369    uzone[UZ_CR], uzone[UZ_EPC], uzone[UZ_SR], uzone[UZ_TH] );
370
371    nolock_printk("c2_mode %X  c2_ptpr %X\n",
372    uzone[UZ_MODE], uzone[UZ_PTPR] );
373
374    nolock_printk("at_01   %X  v0_2    %X  v1_3   %X  a0_4   %X  a1_5   %X\n",
375        uzone[UZ_AT], uzone[UZ_V0], uzone[UZ_V1], uzone[UZ_A0], uzone[UZ_A1] );
376
377    nolock_printk("a2_6    %X  a3_7    %X  t0_8   %X  t1_9   %X  t2_10  %X\n",
378        uzone[UZ_A2], uzone[UZ_A3], uzone[UZ_T0], uzone[UZ_T1], uzone[UZ_T2] );
379 
380    nolock_printk("t3_11   %X  t4_12   %X  t5_13  %X  t6_14  %X  t7_15  %X\n",
381        uzone[UZ_T3], uzone[UZ_T4], uzone[UZ_T5], uzone[UZ_T6], uzone[UZ_T7] );
382
383    nolock_printk("s0_16   %X  s1_17   %X  s2_18  %X  s3_19  %X  s4_20  %X\n",
384        uzone[UZ_S0], uzone[UZ_S1], uzone[UZ_S2], uzone[UZ_S3], uzone[UZ_S4] );
385 
386    nolock_printk("s5_21   %X  s6_22   %X  s7_23  %X  s8_24  %X  ra_25  %X\n",
387        uzone[UZ_S5], uzone[UZ_S6], uzone[UZ_S7], uzone[UZ_T8], uzone[UZ_T9] );
388
389    nolock_printk("gp_28   %X  sp_29   %X  S8_30  %X  ra_31  %X\n",
390        uzone[UZ_GP], uzone[UZ_SP], uzone[UZ_S8], uzone[UZ_RA] );
391
392    // release the lock
393    remote_spinlock_unlock_busy( lock_xp , save_sr );
394
395}  // end hal_exception_dump()
396
397///////////////////////
398void hal_do_exception( void )
399{
400    uint32_t   * uzone;
401    thread_t   * this;
402        error_t      error;
403        uint32_t     excCode;                  // 4 bits XCODE from CP0_CR
404        uint32_t     excPC;                    // fauty instruction address
405
406    // get pointer on faulty thread uzone
407    this  = CURRENT_THREAD;
408    uzone = (uint32_t *)CURRENT_THREAD->uzone_current;
409
410    // get XCODE and EPC from UZONE
411        excCode        = (uzone[UZ_CR] >> 2) & 0xF;
412    excPC          = uzone[UZ_EPC];
413
414#if DEBUG_HAL_EXCEPTIONS
415uint32_t cycle = (uint32_t)hal_get_cycles();
416if( DEBUG_HAL_EXCEPTIONS < cycle )
417printk("\n[DBG] %s : thread %x in process %x enter / core[%x,%d] / epc %x / xcode %x / cycle %d\n",
418__FUNCTION__, this->trdid, this->process->pid, local_cxy, this->core->lid, excPC, excCode, cycle );
419#endif
420
421        switch(excCode)
422        {
423        case XCODE_DBE:     // Data Bus Error : can be non fatal if page fault
424        {
425                    error = hal_mmu_exception( this , excPC , false );  // data MMU exception
426            break;
427        }
428            case XCODE_IBE:     // Instruction Bus Error : can be non fatal if page fault
429        {
430                    error = hal_mmu_exception( this , excPC , true );   // ins MMU exception
431                    break;
432        }
433            case XCODE_CPU:    // Coprocessor unavailable : can be non fatal if FPU
434        {
435            if( ((uzone[UZ_CR] >> 28) & 0x3) == 1 )             // FPU
436            {
437                error = hal_fpu_exception( this );
438            }
439            else                                                // undefined coprocessor
440            {
441                printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
442                "   undefined coprocessor / epc %x\n",
443                __FUNCTION__, this->trdid, this->process->pid, excPC );
444
445                        error = EXCP_USER_ERROR;
446            }
447                    break;
448        }
449        case XCODE_OVR:    // Arithmetic Overflow : user fatal error
450        {
451            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
452            "   arithmetic overflow / epc %x\n",
453            __FUNCTION__, this->trdid, this->process->pid, excPC );
454
455                    error = EXCP_USER_ERROR;
456                break;
457        }
458        case XCODE_RI:     // Reserved Instruction : user fatal error
459        {
460            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
461            "   reserved instruction / epc %x\n",
462            __FUNCTION__, this->trdid, this->process->pid, excPC );
463
464                    error = EXCP_USER_ERROR;
465                break;
466        }
467        case XCODE_ADEL:   // user fatal error
468        {
469            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
470            "   illegal data load address / epc %x\n",
471            __FUNCTION__, this->trdid, this->process->pid, excPC );
472
473                    error = EXCP_USER_ERROR;
474                break;
475        }
476        case XCODE_ADES:   //   user fatal error
477        {
478            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
479            "   illegal data store address / epc %x\n",
480            __FUNCTION__, this->trdid, this->process->pid, excPC );
481
482                    error = EXCP_USER_ERROR;
483                break;
484        }
485        default:
486        {
487                    error = EXCP_KERNEL_PANIC;
488        }
489        }
490   
491    // analyse error code
492        if( error == EXCP_USER_ERROR )          //  user error => kill user process
493        {
494        hal_exception_dump( this , uzone , error );
495
496        sys_exit( EXIT_FAILURE );
497        }
498    else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
499    {
500        hal_exception_dump( this , uzone , error );
501
502        assert( false , "Exception raised kernel panic see information below.\n" );
503    }
504
505#if DEBUG_HAL_EXCEPTIONS
506cycle = (uint32_t)hal_get_cycles();
507if( DEBUG_HAL_EXCEPTIONS < cycle )
508printk("\n[DBG] %s : thread %x in process %x exit / core[%x,%d] / epc %x / xcode %x / cycle %d\n",
509__FUNCTION__, this->trdid, this->process->pid, local_cxy, this->core->lid, excPC, excCode, cycle );
510#endif
511
512}  // end hal_do_exception()
513
514
Note: See TracBrowser for help on using the repository browser.