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

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

euh...

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    // get relevant values from MMU
87        hal_get_mmu_excp( &mmu_ins_excp_code,
88                          &mmu_ins_bad_vaddr,
89                          &mmu_dat_excp_code, 
90                          &mmu_dat_bad_vaddr );
91
92    // get exception code and faulty vaddr
93    if( mmu_ins_excp_code )
94    {
95        excp_code = mmu_ins_excp_code;
96        bad_vaddr = mmu_ins_bad_vaddr;
97    }
98    else if( mmu_dat_excp_code )
99    {
100        excp_code = mmu_dat_excp_code;
101        bad_vaddr = mmu_dat_bad_vaddr;
102    }
103    else
104    {
105         return EXCP_NON_FATAL;
106    }
107
108        vmm_dmsg("\n[INFO] %s : enters for thread %x / process %x"
109             " / bad_vaddr = %x / excep_code = %x\n", 
110                     __FUNCTION__, this->trdid , process->pid , bad_vaddr , excp_code );
111
112    // a kernel thread should not rise an MMU exception
113        if( this->type != THREAD_USER )
114        {
115                printk("\n[PANIC] in %s : thread %x is a kernel thread / vaddr = %x\n",
116                       __FUNCTION__ , this->trdid , bad_vaddr );
117                return EXCP_KERNEL_PANIC;
118        }
119 
120    // enable IRQs
121        hal_enable_irq( NULL );
122
123    // vaddr must be contained in a registered vseg
124    vseg = vmm_get_vseg( process , bad_vaddr );
125
126    if( vseg == NULL )   // vseg not found in local cluster
127        {
128        // get extended pointer on reference process
129        xptr_t ref_xp = process->ref_xp;
130
131        // get cluster and local pointer on reference process
132        cxy_t       ref_cxy = GET_CXY( ref_xp );
133        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
134
135        if( local_cxy != ref_cxy )   // reference process is remote
136        {
137            // get extended pointer on reference vseg
138            xptr_t vseg_xp;
139            rpc_vmm_get_ref_vseg_client( ref_cxy , ref_ptr , bad_vaddr , &vseg_xp );
140           
141       
142            if( vseg == NULL )          // vseg not found => illegal user vaddr
143            {
144                printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
145                       __FUNCTION__ , this->trdid , bad_vaddr );
146
147                hal_disable_irq( NULL );
148                        return EXCP_USER_ERROR;
149            }
150            else                        // vseg found => make a local copy
151            {
152                // allocate a vseg in local cluster
153                vseg = vseg_alloc();
154
155                if( vseg == NULL )
156                {
157                            printk("\n[PANIC] in %s : no memory for vseg / thread = %x\n",
158                                   __FUNCTION__ , this->trdid );
159                    hal_disable_irq( NULL );
160                            return EXCP_KERNEL_PANIC;
161                }
162
163                // initialise local vseg from reference
164                vseg_init_from_ref( vseg , ref_xp );
165
166                // register local vseg in local VMM
167                error = vseg_attach( &process->vmm , vseg );
168            }
169        }
170        else                   // reference is local => illegal user vaddr
171        {
172            printk("\n[ERROR] in %s for thread %x : illegal vaddr = %x\n",
173                   __FUNCTION__ , this->trdid , bad_vaddr );
174
175            hal_disable_irq( NULL );
176                    return EXCP_USER_ERROR;
177        }
178    }
179
180        vmm_dmsg("\n[INFO] %s : found vseg for thread %x / vseg_min = %x / vseg_max = %x\n", 
181                         __FUNCTION__ , this->trdid , vseg->min , vseg->max );
182
183    // analyse exception code
184    if( excp_code & MMU_EXCP_PAGE_UNMAPPED )
185    {
186        // try to map the unmapped PTE
187        error = vmm_handle_page_fault( process, 
188                                       vseg,
189                                       bad_vaddr >> CONFIG_PPM_PAGE_SHIFT );  // vpn
190
191        if( error )
192        {
193            printk("\n[PANIC] in %s for thread %x : cannot map legal vaddr = %x\n",
194               __FUNCTION__ , this->trdid , bad_vaddr );
195
196            hal_disable_irq( NULL );
197                    return EXCP_KERNEL_PANIC;
198        }
199        else
200        {
201            vmm_dmsg("\n[INFO] %s : page fault handled for vaddr = %x in thread %x\n",
202                             __FUNCTION__ , bad_vaddr , this->trdid );
203 
204            // page fault successfully handled
205            hal_disable_irq( NULL );
206
207            // TODO Pourquoi ce yield ? [AG]
208            // sched_yield(); 
209
210            return EXCP_NON_FATAL;
211        }
212    }
213    else if( excp_code & MMU_EXCP_USER_PRIVILEGE )
214    {
215        printk("\n[ERROR] in %s for thread %x : user access to kernel vseg at vaddr = %x\n",
216               __FUNCTION__ , this->trdid , bad_vaddr );
217
218        hal_disable_irq( NULL );
219        return EXCP_USER_ERROR;
220    }
221    else if( excp_code & MMU_EXCP_USER_EXEC )
222    {
223        printk("\n[ERROR] in %s for thread %x : access to non-exec vseg at vaddr = %x\n",
224               __FUNCTION__ , this->trdid , bad_vaddr );
225
226        hal_disable_irq( NULL );
227        return EXCP_USER_ERROR;
228    }
229    else if( excp_code & MMU_EXCP_USER_WRITE )
230    {
231        printk("\n[ERROR] in %s for thread %x : write to non-writable vseg at vaddr = %x\n",
232               __FUNCTION__ , this->trdid , bad_vaddr );
233
234        hal_disable_irq( NULL );
235        return EXCP_USER_ERROR;
236    }
237
238    else  // this is a kernel error => panic   
239    {
240        printk("\n[PANIC] in %s for thread %x : kernel exception = %x / vaddr = %x\n",
241               __FUNCTION__ , this->trdid , excp_code , bad_vaddr );
242
243        hal_disable_irq( NULL );
244        return EXCP_KERNEL_PANIC;
245    }
246 
247} // end mmu_exception_handler()
248
249//////////////////////////////////////////////////////////////////////////////////////////
250// This static function is called by the generic do_exception() handler when a
251// FPU Coprocessor Unavailable exception has been detected by the calling thread.
252// It enables the FPU, It saves the current FPU context in the current owner thread
253// descriptor if required, and restore the FPU context from the calling thread descriptor.
254//////////////////////////////////////////////////////////////////////////////////////////
255// @ thread     : pointer on faulty thread descriptor.
256// @ return always EXCP_NON_FATAL
257//////////////////////////////////////////////////////////////////////////////////////////
258static error_t fpu_exception( thread_t * this )
259{
260        core_t   * core = this->core; 
261
262    // enable FPU 
263        hal_fpu_enable();
264
265    // save FPU context in current owner thread if required
266        if( core->fpu_owner != NULL )
267    {
268        if( core->fpu_owner != this )
269            {
270                    hal_fpu_context_save ( core->fpu_owner->fpu_context );
271        }
272        }
273
274    // attach the FPU to the requesting thread
275        hal_fpu_context_restore( this->fpu_context );
276        core->fpu_owner = this;
277
278        return EXCP_NON_FATAL;
279}
280
281
282//////////////////////////////////////
283error_t do_exception( thread_t * this,
284                      bool_t     is_mmu )
285{
286    error_t error;
287
288        // update user time     
289        thread_user_time_update( this );
290
291    // try to handle non fatal exception
292    if( is_mmu ) error = mmu_exception( this );
293    else         error = fpu_exception( this );
294           
295    // handle pending signals for interrupted thread
296    thread_signals_handle( this );
297
298        // update kernel time
299        thread_kernel_time_update( this );
300
301    return error;
302}
Note: See TracBrowser for help on using the repository browser.