source: trunk/kernel/kern/do_exception.c @ 76

Last change on this file since 76 was 16, checked in by alain, 8 years ago

mprove the HAL for interrupt, exception, syscall handling.

File size: 10.4 KB
Line 
1/*
2 * do_exception.c - architecture independant exception handler implementation.
3 *
4 * Author        Alain Greiner (2016)
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 <kernel_config.h>
27#include <thread.h>
28#include <printk.h>
29#include <do_exception.h>
30
31//////////////////////////////////////////////////////////////////////////////////////////
32// This file containss the architecture independant exception handler.
33//
34// It is called by the architecture specific hal_do_exception() function.
35// The fatal errors are mostly handled by the architecture specific handler,
36// and this generic exception handler handles only two non fatal exceptions types:
37// - MMU page fault
38// - FPU unavailable
39//
40// As some MMU exceptions can be fatal, the returned error code can take three values:
41// - Non Fatal Exception
42// - Fatal User Error
43// - Kernel Panic Error
44//////////////////////////////////////////////////////////////////////////////////////////
45
46/////////////////////////////////////////////////////////////////////////////////////////
47// This remote_spinlock is a kernel global variable defined in all clusters.
48// It must be used by the architecture-specific exception handlers to display
49// error messages on the kernel terminal. Only the spinlock in the I/O cluster is used.
50/////////////////////////////////////////////////////////////////////////////////////////
51
52remote_spinlock_t  exception_lock;
53
54
55//////////////////////////////////////////////////////////////////////////////////////////
56// This static function is called by the generic do_exception() handler when an MMU
57// exception has been detected.
58// It get the relevant exception arguments from the MMU.
59// It signal a fatal error in case of illegal access. In case of page fault,
60// it checks that the faulty address belongs to a registered vseg. It update the local
61// vseg list from the reference cluster if required, and signal a fatal user error
62// in case of illegal virtual address. Finally, it updates the local page table from the
63// reference cluster.
64//////////////////////////////////////////////////////////////////////////////////////////
65// @ this     : pointer on faulty thread descriptor.
66// @ return EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC
67//////////////////////////////////////////////////////////////////////////////////////////
68static error_t mmu_exception( thread_t * this ) 
69{
70        vseg_t         * vseg;        // vseg containing the bad_vaddr
71        process_t      * process;     // local process descriptor
72        vmm_t          * vmm;         // VMM for calling thread
73    error_t          error;       // return value
74
75    reg_t            mmu_ins_excp_code;
76    reg_t            mmu_ins_bad_vaddr;
77    reg_t            mmu_dat_excp_code;
78    reg_t            mmu_dat_bad_vaddr;
79
80    intptr_t         bad_vaddr;
81    uint32_t         excp_code;
82       
83    process     = this->process;
84        vmm         = &process->vmm;
85
86        vmm_dmsg("\n[INFO] %s : enters for thread %x / process %x"
87             " / bad_vaddr = %x / excep_code = %x\n", 
88                     __FUNCTION__, this->trdid , process->pid , bad_vaddr , excep_code );
89
90    // get relevant values from MMU
91        hal_get_mmu_excp( &mmu_ins_excp_code,
92                          &mmu_ins_bad_vaddr,
93                          &mmu_dat_excp_code, 
94                          &mmu_dat_bad_vaddr );
95
96    // get exception code and faulty vaddr
97    if( mmu_ins_excp_code )
98    {
99        excp_code = mmu_ins_excp_code;
100        bad_vaddr = mmu_ins_bad_vaddr;
101    }
102    else if( mmu_dat_excp_code )
103    {
104        excp_code = mmu_dat_excp_code;
105        bad_vaddr = mmu_dat_bad_vaddr;
106    }
107    else
108    {
109         return EXCP_NON_FATAL;
110    }
111
112
113    // a kernel thread should not rise an MMU exception
114        if( this->type != THREAD_USER )
115        {
116                printk("\n[PANIC] in %s : thread %x is a kernel thread / vaddr = %x\n",
117                       __FUNCTION__ , this->trdid , bad_vaddr );
118                return EXCP_KERNEL_PANIC;
119        }
120 
121    // enable IRQs
122        hal_enable_irq( NULL );
123
124    // vaddr must be contained in a registered vseg
125    vseg = vmm_get_vseg( process , bad_vaddr );
126
127    if( vseg == NULL )   // vseg not found in local cluster
128        {
129        // get extended pointer on reference process
130        xptr_t ref_xp = process->ref_xp;
131
132        // get cluster and local pointer on reference process
133        cxy_t       ref_cxy = GET_CXY( ref_xp );
134        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
135
136        if( local_cxy != ref_cxy )   // reference process is remote
137        {
138            // get extended pointer on reference vseg
139            xptr_t vseg_xp;
140            rpc_vmm_get_ref_vseg_client( ref_cxy , ref_ptr , bad_vaddr , &vseg_xp );
141           
142       
143            if( vseg == NULL )          // vseg not found => illegal user vaddr
144            {
145                printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
146                       __FUNCTION__ , this->trdid , bad_vaddr );
147
148                hal_disable_irq( NULL );
149                        return EXCP_USER_ERROR;
150            }
151            else                        // vseg found => make a local copy
152            {
153                // allocate a vseg in local cluster
154                vseg = vseg_alloc();
155
156                if( vseg == NULL )
157                {
158                            printk("\n[PANIC] in %s : no memory for vseg / thread = %x\n",
159                                   __FUNCTION__ , this->trdid );
160                    hal_disable_irq( NULL );
161                            return EXCP_KERNEL_PANIC;
162                }
163
164                // initialise local vseg from reference
165                vseg_init_from_ref( vseg , ref_xp );
166
167                // register local vseg in local VMM
168                error = vseg_attach( &process->vmm , vseg );
169            }
170        }
171        else                   // reference is local => illegal user vaddr
172        {
173            printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
174                   __FUNCTION__ , this->trdid , bad_vaddr );
175
176            hal_disable_irq( NULL );
177                    return EXCP_USER_ERROR;
178        }
179    }
180
181        vmm_dmsg("\n[INFO] %s : found vseg for thread %x / vseg_base = %x / vseg_size = %x\n", 
182                         __FUNCTION__ , this->trdid , vseg->begin , vseg->size );
183
184    // analyse exception code
185    if( excp_code & MMU_EXCP_PAGE_UNMAPPED )
186    {
187        // try to map the unmapped PTE
188        error = vmm_handle_page_fault( process, 
189                                       vseg,
190                                       bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
191
192        if( error )
193        {
194            printk("\n[PANIC] in %s for thread %x : cannot map legal vaddr = %x\n",
195               __FUNCTION__ , this->trdid , bad_vaddr );
196
197            hal_disable_irq( NULL );
198                    return EXCP_KERNEL_PANIC;
199        }
200        else
201        {
202            vmm_dmsg("\n[INFO] %s : page fault handled for vaddr = %x in thread %x\n",
203                             __FUNCTION__ , bad_vaddr , this->trdid );
204 
205            // page fault successfully handled
206            hal_disable_irq( NULL );
207
208            // TODO Pourquoi ce yield ? [AG]
209            // sched_yield(); 
210
211            return EXCP_NON_FATAL;
212        }
213    }
214    else if( excp_code & MMU_EXCP_USER_PRIVILEGE )
215    {
216        printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n",
217               __FUNCTION__ , this->trdid , bad_vaddr );
218
219        hal_disable_irq( NULL );
220        return EXCP_USER_ERROR;
221    }
222    else if( excp_code & MMU_EXCP_USER_EXEC )
223    {
224        printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n",
225               __FUNCTION__ , this->trdid , bad_vaddr );
226
227        hal_disable_irq( NULL );
228        return EXCP_USER_ERROR;
229    }
230    else if( excp_code & MMU_EXCP_USER_WRITE )
231    {
232        printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n",
233               __FUNCTION__ , this->trdid , bad_vaddr );
234
235        hal_disable_irq( NULL );
236        return EXCP_USER_ERROR;
237    }
238
239    else  // this is a kernel error => panic   
240    {
241        printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n",
242               __FUNCTION__ , this->trdid , excp_code , bad_vaddr );
243
244        hal_disable_irq( NULL );
245        return EXCP_KERNEL_PANIC;
246    }
247 
248} // end mmu_exception_handler()
249
250//////////////////////////////////////////////////////////////////////////////////////////
251// This static function is called by the generic do_exception() handler when a
252// FPU Coprocessor Unavailable exception has been detected by the calling thread.
253// It enables the FPU, It saves the current FPU context in the current owner thread
254// descriptor if required, and restore the FPU context from the calling thread descriptor.
255//////////////////////////////////////////////////////////////////////////////////////////
256// @ thread     : pointer on faulty thread descriptor.
257// @ return always EXCP_NON_FATAL
258//////////////////////////////////////////////////////////////////////////////////////////
259static error_t fpu_exception( thread_t * this )
260{
261        core_t   * core = this->core; 
262
263    // enable FPU 
264        hal_fpu_enable();
265
266    // save FPU context in current owner thread if required
267        if( core->fpu_owner != NULL )
268    {
269        if( core->fpu_owner != this )
270            {
271                    hal_fpu_context_save ( core->fpu_owner->fpu_context );
272        }
273        }
274
275    // attach the FPU to the requesting thread
276        hal_fpu_context_restore( this->fpu_context );
277        core->fpu_owner = this;
278
279        return EXCP_NON_FATAL;
280}
281
282
283//////////////////////////////////////
284error_t do_exception( thread_t * this,
285                      bool_t     is_mmu )
286{
287    error_t error;
288
289        // update user time     
290        thread_user_time_update( this );
291
292    // try to handle non fatal exception
293    if( is_mmu ) error = mmu_exception( this );
294    else         error = fpu_exception( this );
295           
296    // handle pending signals for interrupted thread
297    thread_signals_handle( this );
298
299        // update kernel time
300        thread_kernel_time_update( this );
301
302    return error;
303}
Note: See TracBrowser for help on using the repository browser.