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

Last change on this file since 587 was 587, checked in by alain, 5 years ago

Modify the GPT (Generic Page Table) API to support remote accesses,
in order to improve page faults and COW handling.

File size: 19.7 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_kernel_types.h>
25#include <hal_irqmask.h>
26#include <hal_special.h>
27#include <hal_exception.h>
28#include <thread.h>
29#include <printk.h>
30#include <chdev.h>
31#include <vmm.h>
32#include <errno.h>
33#include <scheduler.h>
34#include <core.h>
35#include <syscalls.h>
36#include <shared_syscalls.h>
37#include <remote_busylock.h>
38#include <hal_kentry.h>
39#include <hal_exception.h>
40
41//////////////////////////////////////////////////////////////////////////////////////////
42//  Extern global variables
43//////////////////////////////////////////////////////////////////////////////////////////
44
45extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
46
47//////////////////////////////////////////////////////////////////////////////////////////
48// This enum defines the mask values for an MMU exception code reported by the mips32.
49//////////////////////////////////////////////////////////////////////////////////////////
50
51typedef enum
52{
53    MMU_WRITE_PT1_UNMAPPED        = 0x0001,
54    MMU_WRITE_PT2_UNMAPPED        = 0x0002,
55    MMU_WRITE_PRIVILEGE_VIOLATION = 0x0004,
56    MMU_WRITE_ACCESS_VIOLATION    = 0x0008,
57    MMU_WRITE_UNDEFINED_XTN       = 0x0020,
58    MMU_WRITE_PT1_ILLEGAL_ACCESS  = 0x0040,
59    MMU_WRITE_PT2_ILLEGAL_ACCESS  = 0x0080,
60    MMU_WRITE_DATA_ILLEGAL_ACCESS = 0x0100,
61
62    MMU_READ_PT1_UNMAPPED         = 0x1001,
63    MMU_READ_PT2_UNMAPPED         = 0x1002,
64    MMU_READ_PRIVILEGE_VIOLATION  = 0x1004,
65    MMU_READ_EXEC_VIOLATION       = 0x1010,
66    MMU_READ_UNDEFINED_XTN        = 0x1020,
67    MMU_READ_PT1_ILLEGAL_ACCESS   = 0x1040,
68    MMU_READ_PT2_ILLEGAL_ACCESS   = 0x1080,
69    MMU_READ_DATA_ILLEGAL_ACCESS  = 0x1100,
70}
71mmu_exception_subtype_t;
72
73//////////////////////////////////////////////////////////////////////////////////////////
74// This enum defines the relevant values for XCODE field in mips32 CP0_CR register.
75//////////////////////////////////////////////////////////////////////////////////////////
76
77typedef enum
78{
79    XCODE_ADEL = 0x4,        // Illegal address for data load
80    XCODE_ADES = 0x5,        // Illegal address for data store
81    XCODE_IBE  = 0x6,        // Instruction MMU exception       (can be NON-FATAL)
82    XCODE_DBE  = 0x7,        // Data MMU exception              (can be NON-FATAL)
83    XCODE_RI   = 0xA,        // Reserved instruction exception
84    XCODE_CPU  = 0xB,        // Coprocessor unusable exception  (can be NON-FATAl)
85    XCODE_OVR  = 0xC,        // Arithmetic Overflow exception
86}
87xcode_values_t;
88
89/////////////////////////////////////////////
90char * hal_mmu_exception_str( uint32_t code )
91{
92  switch (code) {
93    case (MMU_WRITE_PT1_UNMAPPED):        return "WRITE_PT1_UNMAPPED";
94    case (MMU_WRITE_PT2_UNMAPPED):        return "WRITE_PT2_UNMAPPED";
95    case (MMU_WRITE_PRIVILEGE_VIOLATION): return "WRITE_PRIVILEGE_VIOLATION";
96    case (MMU_WRITE_ACCESS_VIOLATION):    return "WRITE_ACCESS_VIOLATION";
97    case (MMU_WRITE_UNDEFINED_XTN):       return "WRITE_UNDEFINED_XTN";
98    case (MMU_WRITE_PT1_ILLEGAL_ACCESS):  return "WRITE_PT1_ILLEGAL_ACCESS";
99    case (MMU_WRITE_PT2_ILLEGAL_ACCESS):  return "WRITE_PT2_ILLEGAL_ACCESS";
100    case (MMU_WRITE_DATA_ILLEGAL_ACCESS): return "WRITE_DATA_ILLEGAL_ACCESS";
101    case (MMU_READ_PT1_UNMAPPED):         return "READ_PT1_UNMAPPED";
102    case (MMU_READ_PT2_UNMAPPED):         return "READ_PT2_UNMAPPED";
103    case (MMU_READ_PRIVILEGE_VIOLATION):  return "READ_PRIVILEGE_VIOLATION";
104    case (MMU_READ_EXEC_VIOLATION):       return "READ_EXEC_VIOLATION";
105    case (MMU_READ_UNDEFINED_XTN):        return "READ_UNDEFINED_XTN";
106    case (MMU_READ_PT1_ILLEGAL_ACCESS):   return "READ_PT1_ILLEGAL_ACCESS";
107    case (MMU_READ_PT2_ILLEGAL_ACCESS):   return "READ_PT2_ILLEGAL_ACCESS";
108    case (MMU_READ_DATA_ILLEGAL_ACCESS):  return "READ_DATA_ILLEGAL_ACCESS";
109    default:                              return "undefined";
110  }
111}
112
113//////////////////////////////////////////////////////////////////////////////////////////
114// This function is called when a FPU Coprocessor Unavailable exception has been
115// detected for the calling thread.
116// It enables the FPU, It saves the current FPU context in the current owner thread
117// descriptor if required, and restore the FPU context from the calling thread descriptor.
118//////////////////////////////////////////////////////////////////////////////////////////
119// @ this     : pointer on faulty thread descriptor.
120// @ return always EXCP_NON_FATAL
121//////////////////////////////////////////////////////////////////////////////////////////
122error_t hal_fpu_exception( thread_t * this )
123{
124        core_t   * core = this->core; 
125
126    // enable FPU (in core SR) 
127        hal_fpu_enable();
128
129    // save FPU register values in current owner thread if required
130        if( core->fpu_owner != NULL )
131    {
132        if( core->fpu_owner != this )
133            {
134            // save the FPU registers to current owner thread context
135                    hal_fpu_context_save( XPTR( local_cxy , core->fpu_owner ) );
136
137            // restore FPU registers from requesting thread context
138                hal_fpu_context_restore( this );
139
140            // attach the FPU to the requesting thread
141                core->fpu_owner = this;
142        }
143        }
144    else
145    {
146        // restore FPU registers from requesting thread context
147            hal_fpu_context_restore( this );
148
149        // attach the FPU to the requesting thread
150            core->fpu_owner = this;
151    }
152
153        return EXCP_NON_FATAL;
154
155}  // end hal_fpu_exception()
156
157//////////////////////////////////////////////////////////////////////////////////////////
158// This function is called when an MMU exception has been detected (IBE / DBE).
159// It get the relevant exception arguments from the MMU.
160// It signal a fatal error in case of illegal access. In case of page unmapped
161// it checks that the faulty address belongs to a registered vseg. It update the local
162// vseg list from the reference cluster if required, and signal a fatal user error
163// in case of illegal virtual address. Finally, it updates the local page table from the
164// reference cluster.
165//////////////////////////////////////////////////////////////////////////////////////////
166// @ this     : pointer on faulty thread descriptor.
167// @ excPC    :
168// @ is_ins   : IBE if true / DBE if false.
169// @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC
170//////////////////////////////////////////////////////////////////////////////////////////
171error_t hal_mmu_exception( thread_t * this,
172                           uint32_t   excPC,
173                           bool_t     is_ins ) 
174{
175        process_t      * process;
176    error_t          error;
177
178    uint32_t         mmu_ins_excp_code;
179    uint32_t         mmu_ins_bad_vaddr;
180    uint32_t         mmu_dat_excp_code;
181    uint32_t         mmu_dat_bad_vaddr;
182
183    uint32_t         bad_vaddr;
184    uint32_t         excp_code;
185       
186    process = this->process;
187
188    // get relevant values from MMU
189        hal_get_mmu_excp( &mmu_ins_excp_code,
190                          &mmu_ins_bad_vaddr,
191                          &mmu_dat_excp_code, 
192                          &mmu_dat_bad_vaddr );
193
194    // get exception code and faulty vaddr, depending on IBE/DBE
195    if( is_ins )
196    {
197        excp_code = mmu_ins_excp_code;
198        bad_vaddr = mmu_ins_bad_vaddr;
199    }
200    else 
201    {
202        excp_code = mmu_dat_excp_code;
203        bad_vaddr = mmu_dat_bad_vaddr;
204    }
205
206#if DEBUG_HAL_EXCEPTIONS
207uint32_t cycle = (uint32_t)hal_get_cycles();
208if( DEBUG_HAL_EXCEPTIONS < cycle )
209printk("\n[DBG] %s : thread[%x,%x] enter / is_ins %d / %s / vaddr %x / cycle %d\n",
210__FUNCTION__, process->pid, this->trdid,
211is_ins, hal_mmu_exception_str(excp_code), bad_vaddr, cycle);
212#endif
213
214   // analyse exception code
215    switch( excp_code )
216    {
217        case MMU_WRITE_PT1_UNMAPPED:      // non fatal
218        case MMU_WRITE_PT2_UNMAPPED:
219        case MMU_READ_PT1_UNMAPPED:
220        case MMU_READ_PT2_UNMAPPED:
221        {
222            // try to map the unmapped PTE
223            error = vmm_handle_page_fault( process, 
224                                           bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );
225
226            if( error == EXCP_NON_FATAL )            // page-fault successfully handled
227            {
228
229#if DEBUG_HAL_EXCEPTIONS
230cycle = (uint32_t)hal_get_cycles();
231if( DEBUG_HAL_EXCEPTIONS < cycle )
232printk("\n[DBG] %s : thread[%x,%x] exit / page-fault handled for vaddr = %x\n",
233__FUNCTION__, process->pid, this->trdid, bad_vaddr );
234#endif
235 
236                return EXCP_NON_FATAL;
237            }
238            else if( error == EXCP_USER_ERROR )      // illegal vaddr
239            {
240                printk("\n[USER ERROR] in %s for thread %x in process %x\n"
241                "   illegal vaddr = %x / is_ins %d / epc %x\n",
242                __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
243
244                        return EXCP_USER_ERROR;
245            } 
246            else  // error == EXCP_KERNEL_PANIC 
247            {
248                printk("\n[KERNEL ERROR] in %s for thread %x in process %x\n"
249                "   no memory to map vaddr = %x / is_ins %d / epc %x\n",
250                __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
251
252                        return EXCP_KERNEL_PANIC;
253            } 
254        }
255        case MMU_WRITE_PRIVILEGE_VIOLATION:  // illegal access user error
256        case MMU_READ_PRIVILEGE_VIOLATION:
257        {
258            printk("\n[USER ERROR] in %s : thread %x in process %x\n"
259            "   illegal user access to vaddr = %x / is_ins %d / epc %x\n",
260            __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
261
262            return EXCP_USER_ERROR;
263        }
264        case MMU_WRITE_ACCESS_VIOLATION:     // user error, or Copy-on-Write
265        {
266            // access page table to get GPT_COW flag
267            bool_t cow = hal_gpt_pte_is_cow( &(process->vmm.gpt),
268                                             bad_vaddr >> CONFIG_PPM_PAGE_SHIFT ); 
269
270            if( cow )                        // Copy-on-Write
271            {
272                // try to allocate and copy the page
273                error = vmm_handle_cow( process,
274                                        bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );
275
276                if( error == EXCP_NON_FATAL )        // Copy on write successfull
277                {
278
279#if DEBUG_HAL_EXCEPTIONS
280cycle = (uint32_t)hal_get_cycles();
281if( DEBUG_HAL_EXCEPTIONS < cycle )
282printk("\n[DBG] %s : thread[%x,%x] exit / copy-on-write handled for vaddr = %x\n",
283__FUNCTION__, process->pid, this->trdid, bad_vaddr );
284#endif
285
286                    return EXCP_NON_FATAL;
287                } 
288                else if( error == EXCP_USER_ERROR )  // illegal user access
289                {
290                    printk("\n[USER ERROR] in %s : thread %x in process %x\n"
291                    "   cannot cow vaddr = %x / is_ins %d / epc %x\n",
292                    __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
293
294                            return EXCP_USER_ERROR;
295                }
296                else   // error == EXCP_KERNEL_PANIC
297                {
298                    printk("\n[KERNEL ERROR] in %s : thread %x in process %x\n"
299                    "   no memoty to cow vaddr = %x / is_ins %d / epc %x\n",
300                    __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
301
302                            return EXCP_USER_ERROR;
303                }
304            }
305            else                             // non writable user error
306            {
307                printk("\n[USER ERROR] in %s : thread %x in process %x\n"
308                "   non-writable vaddr = %x / is_ins %d / epc %x\n",
309                __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
310
311                return EXCP_USER_ERROR;
312            }
313        }
314        case MMU_READ_EXEC_VIOLATION:        // user error
315        {
316            printk("\n[USER_ERROR] in %s : thread %x in process %x\n"
317            "   non-executable vaddr = %x / is_ins %d / epc %x\n",
318            __FUNCTION__, this->trdid, this->process->pid, bad_vaddr, is_ins, excPC );
319
320            return EXCP_USER_ERROR;
321        }
322        default:                             // this is a kernel error   
323        {
324            printk("\n[KERNEL ERROR] in %s : thread %x in process %x\n"
325            "  epc %x / badvaddr %x / is_ins %d\n",
326            __FUNCTION__, this->trdid, this->process->pid, excPC, bad_vaddr, is_ins );
327
328            return EXCP_KERNEL_PANIC;
329        }
330    } 
331} // end hal_mmu_exception()
332
333//////////////////////////////////////////////////////////////////////////////////////////
334// This static function prints on the kernel terminal the saved context (core registers)
335// and the thread state of a faulty thread.
336//////////////////////////////////////////////////////////////////////////////////////////
337// @ this     : pointer on faulty thread descriptor.
338// @ uzone    : pointer on register array.
339// @ error    : EXCP_USER_ERROR or EXCP_KERNEL_PANIC
340//////////////////////////////////////////////////////////////////////////////////////////
341static void hal_exception_dump( thread_t * this,
342                                reg_t    * uzone,
343                                error_t    error )
344{
345    core_t    * core    = this->core;
346    process_t * process = this->process;
347
348    // get pointers on TXT0 chdev
349    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
350    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
351    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
352
353    // get extended pointer on remote TXT0 chdev lock
354    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
355
356    // get TXT0 lock in busy waiting mode
357    remote_busylock_acquire( lock_xp );
358
359    if( error == EXCP_USER_ERROR )
360    {
361        nolock_printk("\n=== USER ERROR / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
362        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
363    }
364    else
365    {
366        nolock_printk("\n=== KERNEL ERROR / trdid %x / pid %x / core[%x,%d] / cycle %d ===\n",
367        this->trdid, process->pid, local_cxy, core->lid , (uint32_t)hal_get_cycles() );
368    }
369
370        nolock_printk("busylocks = %d / blocked_vector = %X / flags = %X\n\n",
371    this->busylocks, this->blocked, this->flags );
372
373    nolock_printk("c0_cr   %X  c0_epc  %X  c0_sr  %X  c0_th  %X\n",
374    uzone[UZ_CR], uzone[UZ_EPC], uzone[UZ_SR], uzone[UZ_TH] );
375
376    nolock_printk("c2_mode %X  c2_ptpr %X\n",
377    uzone[UZ_MODE], uzone[UZ_PTPR] );
378
379    nolock_printk("at_01   %X  v0_2    %X  v1_3   %X  a0_4   %X  a1_5   %X\n",
380        uzone[UZ_AT], uzone[UZ_V0], uzone[UZ_V1], uzone[UZ_A0], uzone[UZ_A1] );
381
382    nolock_printk("a2_6    %X  a3_7    %X  t0_8   %X  t1_9   %X  t2_10  %X\n",
383        uzone[UZ_A2], uzone[UZ_A3], uzone[UZ_T0], uzone[UZ_T1], uzone[UZ_T2] );
384 
385    nolock_printk("t3_11   %X  t4_12   %X  t5_13  %X  t6_14  %X  t7_15  %X\n",
386        uzone[UZ_T3], uzone[UZ_T4], uzone[UZ_T5], uzone[UZ_T6], uzone[UZ_T7] );
387
388    nolock_printk("s0_16   %X  s1_17   %X  s2_18  %X  s3_19  %X  s4_20  %X\n",
389        uzone[UZ_S0], uzone[UZ_S1], uzone[UZ_S2], uzone[UZ_S3], uzone[UZ_S4] );
390 
391    nolock_printk("s5_21   %X  s6_22   %X  s7_23  %X  s8_24  %X  ra_25  %X\n",
392        uzone[UZ_S5], uzone[UZ_S6], uzone[UZ_S7], uzone[UZ_T8], uzone[UZ_T9] );
393
394    nolock_printk("gp_28   %X  sp_29   %X  S8_30  %X  ra_31  %X\n",
395        uzone[UZ_GP], uzone[UZ_SP], uzone[UZ_S8], uzone[UZ_RA] );
396
397    // release the lock
398    remote_busylock_release( lock_xp );
399
400}  // end hal_exception_dump()
401
402///////////////////////
403void hal_do_exception( void )
404{
405    uint32_t   * uzone;
406    thread_t   * this;
407        error_t      error;
408        uint32_t     excCode;                  // 4 bits XCODE from CP0_CR
409        uint32_t     excPC;                    // fauty instruction address
410
411    // get pointer on faulty thread uzone
412    this  = CURRENT_THREAD;
413    uzone = (uint32_t *)CURRENT_THREAD->uzone_current;
414
415    // get XCODE and EPC from UZONE
416        excCode        = (uzone[UZ_CR] >> 2) & 0xF;
417    excPC          = uzone[UZ_EPC];
418
419#if DEBUG_HAL_EXCEPTIONS
420uint32_t cycle = (uint32_t)hal_get_cycles();
421if( DEBUG_HAL_EXCEPTIONS < cycle )
422printk("\n[DBG] %s : thread %x in process %x enter / core[%x,%d] / epc %x / xcode %x / cycle %d\n",
423__FUNCTION__, this->trdid, this->process->pid, local_cxy, this->core->lid, excPC, excCode, cycle );
424#endif
425
426        switch(excCode)
427        {
428        case XCODE_DBE:     // Data Bus Error : can be non fatal if page fault
429        {
430                    error = hal_mmu_exception( this , excPC , false );  // data MMU exception
431            break;
432        }
433            case XCODE_IBE:     // Instruction Bus Error : can be non fatal if page fault
434        {
435                    error = hal_mmu_exception( this , excPC , true );   // ins MMU exception
436                    break;
437        }
438            case XCODE_CPU:    // Coprocessor unavailable : can be non fatal if FPU
439        {
440            if( ((uzone[UZ_CR] >> 28) & 0x3) == 1 )             // FPU
441            {
442                error = hal_fpu_exception( this );
443            }
444            else                                                // undefined coprocessor
445            {
446                printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
447                "   undefined coprocessor / epc %x\n",
448                __FUNCTION__, this->trdid, this->process->pid, excPC );
449
450                        error = EXCP_USER_ERROR;
451            }
452                    break;
453        }
454        case XCODE_OVR:    // Arithmetic Overflow : user fatal error
455        {
456            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
457            "   arithmetic overflow / epc %x\n",
458            __FUNCTION__, this->trdid, this->process->pid, excPC );
459
460                    error = EXCP_USER_ERROR;
461                break;
462        }
463        case XCODE_RI:     // Reserved Instruction : user fatal error
464        {
465            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
466            "   reserved instruction / epc %x\n",
467            __FUNCTION__, this->trdid, this->process->pid, excPC );
468
469                    error = EXCP_USER_ERROR;
470                break;
471        }
472        case XCODE_ADEL:   // user fatal error
473        {
474            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
475            "   illegal data load address / epc %x\n",
476            __FUNCTION__, this->trdid, this->process->pid, excPC );
477
478                    error = EXCP_USER_ERROR;
479                break;
480        }
481        case XCODE_ADES:   //   user fatal error
482        {
483            printk("\n[USER_ERROR] in %s for thread %x in process %x\n"
484            "   illegal data store address / epc %x\n",
485            __FUNCTION__, this->trdid, this->process->pid, excPC );
486
487                    error = EXCP_USER_ERROR;
488                break;
489        }
490        default:
491        {
492                    error = EXCP_KERNEL_PANIC;
493        }
494        }
495   
496    // analyse error code
497        if( error == EXCP_USER_ERROR )          //  user error => kill user process
498        {
499        hal_exception_dump( this , uzone , error );
500
501        sys_exit( EXIT_FAILURE );
502        }
503    else if( error == EXCP_KERNEL_PANIC )   // kernel error => kernel panic
504    {
505        hal_exception_dump( this , uzone , error );
506
507        assert( false , "Exception raised kernel panic see information below.\n" );
508    }
509
510#if DEBUG_HAL_EXCEPTIONS
511cycle = (uint32_t)hal_get_cycles();
512if( DEBUG_HAL_EXCEPTIONS < cycle )
513printk("\n[DBG] %s : thread %x in process %x exit / core[%x,%d] / epc %x / xcode %x / cycle %d\n",
514__FUNCTION__, this->trdid, this->process->pid, local_cxy, this->core->lid, excPC, excCode, cycle );
515#endif
516
517}  // end hal_do_exception()
518
519
Note: See TracBrowser for help on using the repository browser.