| 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 |  | 
|---|
| 43 | extern 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 |  | 
|---|
| 49 | typedef enum | 
|---|
| 50 | { | 
|---|
| 51 | XCODE_ADEL = 4,        // Illegal address for data load | 
|---|
| 52 | XCODE_ADES = 5,        // Illegal address for data store | 
|---|
| 53 | XCODE_IBE  = 6,        // Instruction MMU exception       (can be NON-FATAL) | 
|---|
| 54 | XCODE_DBE  = 7,        // Data MMU exception              (can be NON-FATAL) | 
|---|
| 55 | XCODE_RI   = 10,       // Reserved instruction exception | 
|---|
| 56 | XCODE_CPU  = 11,       // Coprocessor unusable exception  (can be NON-FATAl) | 
|---|
| 57 | XCODE_OVR  = 12        // Arithmetic Overflow exception | 
|---|
| 58 | } | 
|---|
| 59 | xcode_values_t; | 
|---|
| 60 |  | 
|---|
| 61 | /////////////////////////////////////// | 
|---|
| 62 | void 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 / arithmetic overflow\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\n", | 
|---|
| 126 | __FUNCTION__ , this->trdid ); | 
|---|
| 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 | ///////////////////////////////////////// | 
|---|
| 145 | void 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 | // dump core registers values | 
|---|
| 152 |  | 
|---|
| 153 | printk("\n====================================================================\n"); | 
|---|
| 154 | printk("  thread %x / process %x / core %x / cycle %d\n", | 
|---|
| 155 | this->trdid , this->process->pid , this->core->gid , hal_get_cycles() ); | 
|---|
| 156 |  | 
|---|
| 157 | printk("  - Processor State:\n"); | 
|---|
| 158 |  | 
|---|
| 159 | printk("    CR:   %x\tEPC:  %x\tSR:   %x\tSP:    %x\n", | 
|---|
| 160 | regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]); | 
|---|
| 161 |  | 
|---|
| 162 | printk("    at_1  %x\tv0_2  %x\t\tv1_3  %x\ta0_4   %x\ta1_5   %x\n", | 
|---|
| 163 | regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]); | 
|---|
| 164 |  | 
|---|
| 165 | printk("    a2_6  %x\t\ta3_7  %x\tt0_8  %x\tt1_9   %x\tt2_10  %x\n", | 
|---|
| 166 | regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]); | 
|---|
| 167 |  | 
|---|
| 168 | printk("    t3_11 %x\tt4_12 %x\t\tt5_13 %x\tt6_14  %x\tt7_15  %x\n", | 
|---|
| 169 | regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]); | 
|---|
| 170 |  | 
|---|
| 171 | printk("    t8_24 %x\t\tt9_25 %x\tgp_28 %x\tc0_hi  %x\tc0_lo  %x\n", | 
|---|
| 172 | regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]); | 
|---|
| 173 |  | 
|---|
| 174 | printk("    s0_16 %x\ts1_17 %x\ts2_18 %x\ts3_19  %x\ts4_20  %x\n", | 
|---|
| 175 | regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]); | 
|---|
| 176 |  | 
|---|
| 177 | printk("    s5_21 %x\ts6_22 %x\t\ts7_23 %x\ts8_30  %x\tra_31  %x\n\n", | 
|---|
| 178 | regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]); | 
|---|
| 179 |  | 
|---|
| 180 | printk("  - Thread State: %x\n" | 
|---|
| 181 | "    type = %s / local_locks = %d / remote_locks = %d / blocked = %x\n", | 
|---|
| 182 | thread_type_str( this->type ), this->local_locks, this->remote_locks, | 
|---|
| 183 | this->blocked ); | 
|---|
| 184 |  | 
|---|
| 185 | // release the exception_lock | 
|---|
| 186 | remote_spinlock_unlock( XPTR( LOCAL_CLUSTER->io_cxy , &exception_lock ) ); | 
|---|
| 187 |  | 
|---|
| 188 | }  // end hal_exception_dump() | 
|---|
| 189 |  | 
|---|