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

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

Few bugs in VMM

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