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

Last change on this file since 435 was 435, checked in by alain, 6 years ago

Fix a bad bug in scheduler...

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