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