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

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

Several modifs in the generic scheduler and in the hal_context to
fix the context switch mechanism.

File size: 6.5 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_types.h>
25#include <hal_irqmask.h>
26#include <hal_exception.h>
27#include <thread.h>
28#include <printk.h>
29#include <vmm.h>
30#include <errno.h>
31#include <scheduler.h>
32#include <core.h>
33#include <signal.h>
34#include <syscalls.h>
35#include <do_exception.h>
36#include <remote_spinlock.h>
37#include <mips32_uzone.h>
38
39//////////////////////////////////////////////////////////////////////////////////////////
40//  Extern global variables
41//////////////////////////////////////////////////////////////////////////////////////////
42
43extern remote_spinlock_t  exception_lock;  // allocated in the do_exception.c file.
44
45//////////////////////////////////////////////////////////////////////////////////////////
46// This enum defines the relevant values for XCODE field in mips32 CP0_CR register.
47//////////////////////////////////////////////////////////////////////////////////////////
48
49typedef enum
50{
51    XCODE_ADEL = 0x4,        // Illegal address for data load
52    XCODE_ADES = 0x5,        // Illegal address for data store
53    XCODE_IBE  = 0x6,        // Instruction MMU exception       (can be NON-FATAL)
54    XCODE_DBE  = 0x7,        // Data MMU exception              (can be NON-FATAL)
55    XCODE_RI   = 0xA,        // Reserved instruction exception
56    XCODE_CPU  = 0xB,        // Coprocessor unusable exception  (can be NON-FATAl)
57    XCODE_OVR  = 0xC,        // Arithmetic Overflow exception
58}
59xcode_values_t;
60
61///////////////////////////////////////
62void hal_do_exception( thread_t * this, 
63                       reg_t    * regs_tbl )
64{
65        error_t             error;
66        uint32_t            excCode;                  // 4 bits XCODE from CP0_CR
67
68    // get 4 bits XCODE from CP0_CR register
69        excCode        = (regs_tbl[UZ_CR] >> 2) & 0xF;
70
71        switch(excCode)
72        {
73        case XCODE_DBE:     // can be non fatal
74            case XCODE_IBE:     // can be non fatal
75        {
76            // call generic excepton handler for a MMU exception
77                    error = do_exception( this , true );
78        }
79                break;
80
81            case XCODE_CPU:    // can be non fatal
82        {
83            if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 )  // unavailable FPU
84            {
85                // call generic excepton handler for a FPU exception
86                error = do_exception( this , false );
87            }
88            else
89            {
90                        printk("\n[ERROR] in thread %x / unsupported coprocessor type\n",
91                               this->trdid );
92                        error = EXCP_USER_ERROR;
93            }
94        }
95                break;
96
97        case XCODE_OVR:   // user fatal error
98        {
99            printk("\n[ERROR] in thread %x / arithmetic overflow\n",
100                           this->trdid );
101                    error = EXCP_USER_ERROR;
102        }
103            break;
104
105        case XCODE_RI:   // user fatal error
106        {
107            printk("\n[ERROR] in thread %x / Illegal Codop\n",
108                           this->trdid );
109                    error = EXCP_USER_ERROR;
110        }
111        break;
112
113        case XCODE_ADEL: // user fatal error
114
115        case XCODE_ADES:
116        {
117            printk("\n[ERROR] in thread %x / illegal address\n",
118                           this->trdid );
119                    error = EXCP_USER_ERROR;
120        }
121                break;
122
123        default:
124        {
125            printk("\n[PANIC] in %s for thread %x / illegal XCODE value = %x\n",
126                           __FUNCTION__ , this->trdid , excCode );
127                    error = EXCP_USER_ERROR;
128        }
129        }
130   
131    // analyse error code
132        if( error == EXCP_USER_ERROR )      //  user error => kill the user process and return
133        {
134        hal_exception_dump( this , regs_tbl );
135        sys_kill( this->process->pid , SIGKILL );
136        }
137    else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
138    {
139        hal_exception_dump( this , regs_tbl );
140        hal_core_sleep();
141    }
142}  // end hal_do_exception()
143
144/////////////////////////////////////////
145void hal_exception_dump( thread_t * this,
146                         reg_t    * regs_tbl )
147{
148    // take the exception_lock located in io_cluster
149    remote_spinlock_lock( XPTR( LOCAL_CLUSTER->io_cxy , &exception_lock ) );
150
151    if( this->type == THREAD_USER )
152    printk("\n================= USER ERROR =======================================\n");
153    else
154    printk("\n================= KERNEL PANIC =====================================\n");
155
156        printk("  thread type = %s / trdid = %x / pid %x / core[%x,%d]\n"
157           "  local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
158           thread_type_str(this->type), this->trdid, this->process->pid, local_cxy,
159           this->core->lid, this->local_locks, this->remote_locks, this->blocked );
160
161        printk("CR    %X  EPC   %X  SR    %X  SP     %X\n",
162                   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]);
163
164    printk("at_1  %X  v0_2  %X  v1_3  %X  a0_4   %X  a1_5   %X\n",
165               regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]);
166
167    printk("a2_6  %X  a3_7  %X  t0_8  %X  t1_9   %X  t2_10  %X\n",
168                   regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]);
169 
170    printk("t3_11 %X  t4_12 %X  t5_13 %X  t6_14  %X  t7_15  %X\n",
171                   regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]);
172
173    printk("t8_24 %X  t9_25 %X  gp_28 %X  c0_hi  %X  c0_lo  %X\n",
174                   regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]);
175
176    printk("s0_16 %X  s1_17 %X  s2_18 %X  s3_19  %X  s4_20  %X\n",
177                   regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]);
178 
179    printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_30  %X  ra_31  %X\n",
180               regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
181
182    // release the exception_lock
183    remote_spinlock_unlock( XPTR( LOCAL_CLUSTER->io_cxy , &exception_lock ) );
184
185}  // end hal_exception_dump()
186
Note: See TracBrowser for help on using the repository browser.