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

Last change on this file since 406 was 406, checked in by alain, 7 years ago

This version executed successfully the user "init" process on a mono-processor TSAR architecture.

File size: 13.3 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 <signal.h>
36#include <syscalls.h>
37#include <remote_spinlock.h>
[406]38#include <hal_kentry.h>
[16]39
[380]40
[16]41//////////////////////////////////////////////////////////////////////////////////////////
42//  Extern global variables
43//////////////////////////////////////////////////////////////////////////////////////////
44
[380]45extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
[16]46
47//////////////////////////////////////////////////////////////////////////////////////////
[381]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//////////////////////////////////////////////////////////////////////////////////////////
[406]60// This enum defines the mask valuesi for an MMU exception code reported by the mips32.
[381]61//////////////////////////////////////////////////////////////////////////////////////////
62
63typedef enum
64{
[406]65    MMU_EXCP_PAGE_UNMAPPED   = 0x0003,
66    MMU_EXCP_USER_PRIVILEGE  = 0x0004,
67    MMU_EXCP_USER_WRITE      = 0x0008,
68    MMU_EXCP_USER_EXEC       = 0x1010,
[381]69}
70mmu_exception_subtype_t;
71
72//////////////////////////////////////////////////////////////////////////////////////////
[16]73// This enum defines the relevant values for XCODE field in mips32 CP0_CR register.
74//////////////////////////////////////////////////////////////////////////////////////////
75
76typedef enum
77{
[296]78    XCODE_ADEL = 0x4,        // Illegal address for data load
79    XCODE_ADES = 0x5,        // Illegal address for data store
80    XCODE_IBE  = 0x6,        // Instruction MMU exception       (can be NON-FATAL)
81    XCODE_DBE  = 0x7,        // Data MMU exception              (can be NON-FATAL)
82    XCODE_RI   = 0xA,        // Reserved instruction exception
83    XCODE_CPU  = 0xB,        // Coprocessor unusable exception  (can be NON-FATAl)
84    XCODE_OVR  = 0xC,        // Arithmetic Overflow exception
[16]85}
86xcode_values_t;
87
[380]88//////////////////////////////////////////////////////////////////////////////////////////
[406]89// This function is called when a FPU Coprocessor Unavailable exception has been
[380]90// detected for the calling thread.
91// It enables the FPU, It saves the current FPU context in the current owner thread
92// descriptor if required, and restore the FPU context from the calling thread descriptor.
93//////////////////////////////////////////////////////////////////////////////////////////
94// @ this     : pointer on faulty thread descriptor.
95// @ return always EXCP_NON_FATAL
96//////////////////////////////////////////////////////////////////////////////////////////
[406]97error_t hal_fpu_exception( thread_t * this )
[380]98{
99        core_t   * core = this->core; 
100
101    // enable FPU 
102        hal_fpu_enable();
103
104    // save FPU context in current owner thread if required
105        if( core->fpu_owner != NULL )
106    {
107        if( core->fpu_owner != this )
108            {
109                    hal_fpu_context_save ( core->fpu_owner->fpu_context );
110        }
111        }
112
113    // attach the FPU to the requesting thread
114        hal_fpu_context_restore( this->fpu_context );
115        core->fpu_owner = this;
116
117        return EXCP_NON_FATAL;
118
119}  // end hal_fpu_exception()
120
121//////////////////////////////////////////////////////////////////////////////////////////
[406]122// This function is called when an MMU exception has been detected.
[380]123// It get the relevant exception arguments from the MMU.
124// It signal a fatal error in case of illegal access. In case of page unmapped
125// it checks that the faulty address belongs to a registered vseg. It update the local
126// vseg list from the reference cluster if required, and signal a fatal user error
127// in case of illegal virtual address. Finally, it updates the local page table from the
128// reference cluster.
129//////////////////////////////////////////////////////////////////////////////////////////
130// @ this     : pointer on faulty thread descriptor.
[406]131// @ is_ins   : IBE if true / DBE if false.
[380]132// @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC
133//////////////////////////////////////////////////////////////////////////////////////////
[406]134error_t hal_mmu_exception( thread_t * this,
135                           bool_t     is_ins ) 
[380]136{
[406]137        process_t      * process;
138    error_t          error;
[380]139
[406]140    uint32_t         mmu_ins_excp_code;
141    uint32_t         mmu_ins_bad_vaddr;
142    uint32_t         mmu_dat_excp_code;
143    uint32_t         mmu_dat_bad_vaddr;
[380]144
[406]145    uint32_t         bad_vaddr;
[380]146    uint32_t         excp_code;
147       
[406]148    process = this->process;
[380]149
[406]150    excp_dmsg("\n[DMSG] %s : enter for thread %x in process %x / is_ins = %d\n",
151    __FUNCTION__ , this->trdid , process->pid , is_ins );
152
[380]153    // get relevant values from MMU
154        hal_get_mmu_excp( &mmu_ins_excp_code,
155                          &mmu_ins_bad_vaddr,
156                          &mmu_dat_excp_code, 
157                          &mmu_dat_bad_vaddr );
158
[406]159    excp_dmsg("\n[DMSG] %s : icode = %x / ivaddr = %x / dcode = %x / dvaddr = %x\n",
160    __FUNCTION__ , mmu_ins_excp_code , mmu_ins_bad_vaddr ,
161                   mmu_dat_excp_code , mmu_dat_bad_vaddr );
162
163    // get exception code and faulty vaddr, depending on IBE/DBE
164    if( is_ins )
[380]165    {
166        excp_code = mmu_ins_excp_code;
167        bad_vaddr = mmu_ins_bad_vaddr;
168    }
[406]169    else 
[380]170    {
171        excp_code = mmu_dat_excp_code;
172        bad_vaddr = mmu_dat_bad_vaddr;
173    }
174
[406]175    excp_dmsg("\n[DMSG] %s : excp_code = %x / bad_vaddr = %x\n",
176    __FUNCTION__ , excp_code , bad_vaddr );
[380]177
178    // analyse exception code
179    if( excp_code & MMU_EXCP_PAGE_UNMAPPED )
180    {
[406]181        excp_dmsg("\n[DMSG] %s : type PAGE_UNMAPPED\n", __FUNCTION__ );
182
[391]183        // enable IRQs before handling page fault
[406]184        // hal_enable_irq( NULL );
[391]185
[380]186        // try to map the unmapped PTE
187        error = vmm_handle_page_fault( process, 
188                                       bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
[391]189        // disable IRQs
[406]190        // hal_disable_irq( NULL );
[380]191
[391]192        if( error )     // not enough memory
[380]193        {
[406]194            printk("\n[ERROR] in %s for thread %x : cannot map vaddr = %x\n",
[380]195               __FUNCTION__ , this->trdid , bad_vaddr );
196
[391]197                    return EXCP_USER_ERROR;
198        } 
199        else            // page fault successfully handled
[380]200        {
[406]201            excp_dmsg("\n[DMSG] %s : page fault handled / bad_vaddr = %x / excp_code = %x\n",
202                             __FUNCTION__ , bad_vaddr , excp_code );
[380]203 
204            return EXCP_NON_FATAL;
205        }
206    }
207    else if( excp_code & MMU_EXCP_USER_PRIVILEGE )
208    {
209        printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n",
210               __FUNCTION__ , this->trdid , bad_vaddr );
211
212        return EXCP_USER_ERROR;
213    }
214    else if( excp_code & MMU_EXCP_USER_EXEC )
215    {
216        printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n",
217               __FUNCTION__ , this->trdid , bad_vaddr );
218
219        return EXCP_USER_ERROR;
220    }
221    else if( excp_code & MMU_EXCP_USER_WRITE )
222    {
223        printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n",
224               __FUNCTION__ , this->trdid , bad_vaddr );
225
226        return EXCP_USER_ERROR;
227    }
228    else  // this is a kernel error => panic   
229    {
230        printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n",
231               __FUNCTION__ , this->trdid , excp_code , bad_vaddr );
232
233        return EXCP_KERNEL_PANIC;
234    }
235 
236} // end hal_mmu_exception()
237
238//////////////////////////////////////////////////////////////////////////////////////////
239// This static function prints on the kernel terminal the saved context (core registers)
240// and the thread state of a faulty thread.
241//////////////////////////////////////////////////////////////////////////////////////////
242// @ this     : pointer on faulty thread descriptor.
243// @ regs_tbl : pointer on register array.
244// @ return always EXCP_NON_FATAL
245//////////////////////////////////////////////////////////////////////////////////////////
246static void hal_exception_dump( thread_t * this,
247                                reg_t    * regs_tbl )
248{
249    uint32_t  save_sr;
250
251    // get pointers on TXT0 chdev
252    xptr_t    txt0_xp  = chdev_dir.txt[0];
253    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
254    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
255
256    // get extended pointer on remote TXT0 chdev lock
257    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
258
259    // get TXT0 lock in busy waiting mode
260    remote_spinlock_lock_busy( lock_xp , &save_sr );
261
262    if( this->type == THREAD_USER )
263    nolock_printk("\n================= USER ERROR / cycle %d ====================\n",
264           hal_time_stamp() );
265    else
266    nolock_printk("\n================= KERNEL PANIC / cycle %d ==================\n",
267           hal_time_stamp() );
268
269        nolock_printk("  thread type = %s / trdid = %x / pid %x / core[%x,%d]\n"
270           "  local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
271           thread_type_str(this->type), this->trdid, this->process->pid, local_cxy,
272           this->core->lid, this->local_locks, this->remote_locks, this->blocked );
273
274        nolock_printk("CR    %X  EPC   %X  SR    %X  SP     %X\n",
275                   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]);
276
277    nolock_printk("at_1  %X  v0_2  %X  v1_3  %X  a0_4   %X  a1_5   %X\n",
278               regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]);
279
280    nolock_printk("a2_6  %X  a3_7  %X  t0_8  %X  t1_9   %X  t2_10  %X\n",
281                   regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]);
282 
283    nolock_printk("t3_11 %X  t4_12 %X  t5_13 %X  t6_14  %X  t7_15  %X\n",
284                   regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]);
285
286    nolock_printk("t8_24 %X  t9_25 %X  gp_28 %X  c0_hi  %X  c0_lo  %X\n",
287                   regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]);
288
289    nolock_printk("s0_16 %X  s1_17 %X  s2_18 %X  s3_19  %X  s4_20  %X\n",
290                   regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]);
291 
292    nolock_printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_30  %X  ra_31  %X\n",
293               regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
294
295    // release the lock
296    remote_spinlock_unlock_busy( lock_xp , save_sr );
297
298}  // end hal_exception_dump()
299
300
301///////////////////////////////////////////////////////////////////////////////
302// TODO replace the hal_core_sleep() by the generic panic() function.
303///////////////////////////////////////////////////////////////////////////////
[16]304void hal_do_exception( thread_t * this, 
305                       reg_t    * regs_tbl )
306{
307        error_t             error;
308        uint32_t            excCode;                  // 4 bits XCODE from CP0_CR
309
310    // get 4 bits XCODE from CP0_CR register
311        excCode        = (regs_tbl[UZ_CR] >> 2) & 0xF;
312
[406]313    excp_dmsg("\n[DMSG] %s : enter for thread %x in process %x / xcode = %x / cycle %d\n",
314    __FUNCTION__ , this->trdid , this->process->pid , excCode , hal_time_stamp() );
315
[16]316        switch(excCode)
317        {
318        case XCODE_DBE:     // can be non fatal
[406]319        {
320                    error = hal_mmu_exception( this , false );  // data MMU exception
321            break;
322        }
[380]323            case XCODE_IBE:     // can be non fatal
[16]324        {
[406]325                    error = hal_mmu_exception( this , true );   // ins MMU exception
326                    break;
[16]327        }
328            case XCODE_CPU:    // can be non fatal
329        {
330            if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 )  // unavailable FPU
331            {
[380]332                error = hal_fpu_exception( this );
[16]333            }
334            else
335            {
336                        error = EXCP_USER_ERROR;
337            }
[406]338                    break;
[16]339        }
[380]340        case XCODE_OVR:    // user fatal error
341        case XCODE_RI:     // user fatal error
342        case XCODE_ADEL:   // user fatal error
343        case XCODE_ADES:   // user fatal error
[16]344        {
345                    error = EXCP_USER_ERROR;
[406]346                break;
[16]347        }
348        default:
349        {
[317]350                    error = EXCP_KERNEL_PANIC;
[16]351        }
352        }
353   
354    // analyse error code
[317]355        if( error == EXCP_USER_ERROR )          //  user error => kill user process
[16]356        {
357        hal_exception_dump( this , regs_tbl );
358        sys_kill( this->process->pid , SIGKILL );
359        }
360    else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
361    {
362        hal_exception_dump( this , regs_tbl );
363        hal_core_sleep();
364    }
[406]365
366    excp_dmsg("\n[DMSG] %s : exit for thread %x in process %x / cycle %d\n",
367    __FUNCTION__ , this->trdid , this->process->pid , hal_time_stamp() );
368
[16]369}  // end hal_do_exception()
370
371
Note: See TracBrowser for help on using the repository browser.