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

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

Add missing defines in the TSAR exception handler.

File size: 15.1 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 <chdev.h>
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
39
40//////////////////////////////////////////////////////////////////////////////////////////
41//  Extern global variables
42//////////////////////////////////////////////////////////////////////////////////////////
43
44extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
45
46//////////////////////////////////////////////////////////////////////////////////////////
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//////////////////////////////////////////////////////////////////////////////////////////
72// This enum defines the relevant values for XCODE field in mips32 CP0_CR register.
73//////////////////////////////////////////////////////////////////////////////////////////
74
75typedef enum
76{
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
84}
85xcode_values_t;
86
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        vseg_t         * vseg;        // vseg containing the bad_vaddr
135        process_t      * process;     // local process descriptor
136    error_t          error;       // return value
137
138    reg_t            mmu_ins_excp_code;
139    reg_t            mmu_ins_bad_vaddr;
140    reg_t            mmu_dat_excp_code;
141    reg_t            mmu_dat_bad_vaddr;
142
143    intptr_t         bad_vaddr;
144    uint32_t         excp_code;
145       
146    process     = this->process;
147
148    // get relevant values from MMU
149        hal_get_mmu_excp( &mmu_ins_excp_code,
150                          &mmu_ins_bad_vaddr,
151                          &mmu_dat_excp_code, 
152                          &mmu_dat_bad_vaddr );
153
154    // get exception code and faulty vaddr
155    if( mmu_ins_excp_code )
156    {
157        excp_code = mmu_ins_excp_code;
158        bad_vaddr = mmu_ins_bad_vaddr;
159    }
160    else if( mmu_dat_excp_code )
161    {
162        excp_code = mmu_dat_excp_code;
163        bad_vaddr = mmu_dat_bad_vaddr;
164    }
165    else
166    {
167         return EXCP_NON_FATAL;
168    }
169
170        vmm_dmsg("\n[INFO] %s : enters for thread %x / process %x"
171             " / bad_vaddr = %x / excep_code = %x\n", 
172                     __FUNCTION__, this->trdid , process->pid , bad_vaddr , excp_code );
173
174    // a kernel thread should not rise an MMU exception
175        if( this->type != THREAD_USER )
176        {
177                printk("\n[PANIC] in %s : thread %x is a kernel thread / vaddr = %x\n",
178                       __FUNCTION__ , this->trdid , bad_vaddr );
179                return EXCP_KERNEL_PANIC;
180        }
181 
182    // enable IRQs
183        hal_enable_irq( NULL );
184
185    // vaddr must be contained in a registered vseg
186    vseg = vmm_get_vseg( process , bad_vaddr );
187
188    if( vseg == NULL )   // vseg not found in local cluster
189        {
190        // get extended pointer on reference process
191        xptr_t ref_xp = process->ref_xp;
192
193        // get cluster and local pointer on reference process
194        cxy_t       ref_cxy = GET_CXY( ref_xp );
195        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
196
197        if( local_cxy != ref_cxy )   // reference process is remote
198        {
199            // get extended pointer on reference vseg
200            xptr_t vseg_xp;
201            rpc_vmm_get_ref_vseg_client( ref_cxy , ref_ptr , bad_vaddr , &vseg_xp );
202           
203       
204            if( vseg == NULL )          // vseg not found => illegal user vaddr
205            {
206                printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
207                       __FUNCTION__ , this->trdid , bad_vaddr );
208
209                hal_disable_irq( NULL );
210                        return EXCP_USER_ERROR;
211            }
212            else                        // vseg found => make a local copy
213            {
214                // allocate a vseg in local cluster
215                vseg = vseg_alloc();
216
217                if( vseg == NULL )
218                {
219                            printk("\n[PANIC] in %s : no memory for vseg / thread = %x\n",
220                                   __FUNCTION__ , this->trdid );
221                    hal_disable_irq( NULL );
222                            return EXCP_KERNEL_PANIC;
223                }
224
225                // initialise local vseg from reference
226                vseg_init_from_ref( vseg , ref_xp );
227
228                // register local vseg in local VMM
229                error = vseg_attach( &process->vmm , vseg );
230            }
231        }
232        else                   // reference is local => illegal user vaddr
233        {
234            printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
235                   __FUNCTION__ , this->trdid , bad_vaddr );
236
237            hal_disable_irq( NULL );
238                    return EXCP_USER_ERROR;
239        }
240    }
241
242        vmm_dmsg("\n[INFO] %s : found vseg for thread %x / vseg_min = %x / vseg_max = %x\n", 
243                         __FUNCTION__ , this->trdid , vseg->min , vseg->max );
244
245    // analyse exception code
246    if( excp_code & MMU_EXCP_PAGE_UNMAPPED )
247    {
248        // try to map the unmapped PTE
249        error = vmm_handle_page_fault( process, 
250                                       vseg,
251                                       bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
252
253        if( error )
254        {
255            printk("\n[PANIC] in %s for thread %x : cannot map legal vaddr = %x\n",
256               __FUNCTION__ , this->trdid , bad_vaddr );
257
258            hal_disable_irq( NULL );
259                    return EXCP_KERNEL_PANIC;
260        }
261        else
262        {
263            vmm_dmsg("\n[INFO] %s : page fault handled for vaddr = %x in thread %x\n",
264                             __FUNCTION__ , bad_vaddr , this->trdid );
265 
266            // page fault successfully handled
267            hal_disable_irq( NULL );
268            return EXCP_NON_FATAL;
269        }
270    }
271    else if( excp_code & MMU_EXCP_USER_PRIVILEGE )
272    {
273        printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n",
274               __FUNCTION__ , this->trdid , bad_vaddr );
275
276        hal_disable_irq( NULL );
277        return EXCP_USER_ERROR;
278    }
279    else if( excp_code & MMU_EXCP_USER_EXEC )
280    {
281        printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n",
282               __FUNCTION__ , this->trdid , bad_vaddr );
283
284        hal_disable_irq( NULL );
285        return EXCP_USER_ERROR;
286    }
287    else if( excp_code & MMU_EXCP_USER_WRITE )
288    {
289        printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n",
290               __FUNCTION__ , this->trdid , bad_vaddr );
291
292        hal_disable_irq( NULL );
293        return EXCP_USER_ERROR;
294    }
295
296    else  // this is a kernel error => panic   
297    {
298        printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n",
299               __FUNCTION__ , this->trdid , excp_code , bad_vaddr );
300
301        hal_disable_irq( NULL );
302        return EXCP_KERNEL_PANIC;
303    }
304 
305} // end hal_mmu_exception()
306
307//////////////////////////////////////////////////////////////////////////////////////////
308// This static function prints on the kernel terminal the saved context (core registers)
309// and the thread state of a faulty thread.
310//////////////////////////////////////////////////////////////////////////////////////////
311// @ this     : pointer on faulty thread descriptor.
312// @ regs_tbl : pointer on register array.
313// @ return always EXCP_NON_FATAL
314//////////////////////////////////////////////////////////////////////////////////////////
315static void hal_exception_dump( thread_t * this,
316                                reg_t    * regs_tbl )
317{
318    uint32_t  save_sr;
319
320    // get pointers on TXT0 chdev
321    xptr_t    txt0_xp  = chdev_dir.txt[0];
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
331    if( this->type == THREAD_USER )
332    nolock_printk("\n================= USER ERROR / cycle %d ====================\n",
333           hal_time_stamp() );
334    else
335    nolock_printk("\n================= KERNEL PANIC / cycle %d ==================\n",
336           hal_time_stamp() );
337
338        nolock_printk("  thread type = %s / trdid = %x / pid %x / core[%x,%d]\n"
339           "  local locks = %d / remote locks = %d / blocked_vector = %X\n\n",
340           thread_type_str(this->type), this->trdid, this->process->pid, local_cxy,
341           this->core->lid, this->local_locks, this->remote_locks, this->blocked );
342
343        nolock_printk("CR    %X  EPC   %X  SR    %X  SP     %X\n",
344                   regs_tbl[UZ_CR], regs_tbl[UZ_EPC], regs_tbl[UZ_SR], regs_tbl[UZ_SP]);
345
346    nolock_printk("at_1  %X  v0_2  %X  v1_3  %X  a0_4   %X  a1_5   %X\n",
347               regs_tbl[UZ_AT], regs_tbl[UZ_V0], regs_tbl[UZ_V1], regs_tbl[UZ_A0], regs_tbl[UZ_A1]);
348
349    nolock_printk("a2_6  %X  a3_7  %X  t0_8  %X  t1_9   %X  t2_10  %X\n",
350                   regs_tbl[UZ_A2],regs_tbl[UZ_A3],regs_tbl[UZ_T0],regs_tbl[UZ_T1],regs_tbl[UZ_T2]);
351 
352    nolock_printk("t3_11 %X  t4_12 %X  t5_13 %X  t6_14  %X  t7_15  %X\n",
353                   regs_tbl[UZ_T3],regs_tbl[UZ_T4],regs_tbl[UZ_T5],regs_tbl[UZ_T6],regs_tbl[UZ_T7]);
354
355    nolock_printk("t8_24 %X  t9_25 %X  gp_28 %X  c0_hi  %X  c0_lo  %X\n",
356                   regs_tbl[UZ_T8],regs_tbl[UZ_T9],regs_tbl[UZ_GP],regs_tbl[UZ_HI],regs_tbl[UZ_LO]);
357
358    nolock_printk("s0_16 %X  s1_17 %X  s2_18 %X  s3_19  %X  s4_20  %X\n",
359                   regs_tbl[UZ_S0],regs_tbl[UZ_S1],regs_tbl[UZ_S2],regs_tbl[UZ_S3],regs_tbl[UZ_S4]);
360 
361    nolock_printk("s5_21 %X  s6_22 %X  s7_23 %X  s8_30  %X  ra_31  %X\n",
362               regs_tbl[UZ_S5],regs_tbl[UZ_S6],regs_tbl[UZ_S7],regs_tbl[UZ_S8],regs_tbl[UZ_RA]);
363
364    // release the lock
365    remote_spinlock_unlock_busy( lock_xp , save_sr );
366
367}  // end hal_exception_dump()
368
369
370///////////////////////////////////////////////////////////////////////////////
371// TODO replace the hal_core_sleep() by the generic panic() function.
372///////////////////////////////////////////////////////////////////////////////
373void hal_do_exception( thread_t * this, 
374                       reg_t    * regs_tbl )
375{
376        error_t             error;
377        uint32_t            excCode;                  // 4 bits XCODE from CP0_CR
378
379    // get 4 bits XCODE from CP0_CR register
380        excCode        = (regs_tbl[UZ_CR] >> 2) & 0xF;
381
382        switch(excCode)
383        {
384        case XCODE_DBE:     // can be non fatal
385            case XCODE_IBE:     // can be non fatal
386        {
387                    error = hal_mmu_exception( this );
388        }
389                break;
390
391            case XCODE_CPU:    // can be non fatal
392        {
393            if( ((regs_tbl[UZ_CR] >> 28) & 0x3) == 1 )  // unavailable FPU
394            {
395                error = hal_fpu_exception( this );
396            }
397            else
398            {
399                        error = EXCP_USER_ERROR;
400            }
401        }
402                break;
403
404        case XCODE_OVR:    // user fatal error
405        case XCODE_RI:     // user fatal error
406        case XCODE_ADEL:   // user fatal error
407        case XCODE_ADES:   // user fatal error
408        {
409                    error = EXCP_USER_ERROR;
410        }
411            break;
412
413        default:
414        {
415                    error = EXCP_KERNEL_PANIC;
416        }
417        }
418   
419    // analyse error code
420        if( error == EXCP_USER_ERROR )          //  user error => kill user process
421        {
422        hal_exception_dump( this , regs_tbl );
423        sys_kill( this->process->pid , SIGKILL );
424        }
425    else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
426    {
427        hal_exception_dump( this , regs_tbl );
428        hal_core_sleep();
429    }
430}  // end hal_do_exception()
431
432
Note: See TracBrowser for help on using the repository browser.