1 | /* |
---|
2 | * vmm.c - virtual memory manager related operations interface. |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011, 2012) |
---|
5 | * Mohamed Lamine Karaoui (2015) |
---|
6 | * Alain Greiner (2016) |
---|
7 | * |
---|
8 | * Copyright (c) UPMC Sorbonne Universites |
---|
9 | * |
---|
10 | * This file is part of ALMOS-MKH. |
---|
11 | * |
---|
12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
13 | * under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; version 2.0 of the License. |
---|
15 | * |
---|
16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | * General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <kernel_config.h> |
---|
27 | #include <hal_types.h> |
---|
28 | #include <hal_special.h> |
---|
29 | #include <hal_gpt.h> |
---|
30 | #include <printk.h> |
---|
31 | #include <memcpy.h> |
---|
32 | #include <rwlock.h> |
---|
33 | #include <list.h> |
---|
34 | #include <bits.h> |
---|
35 | #include <process.h> |
---|
36 | #include <thread.h> |
---|
37 | #include <vseg.h> |
---|
38 | #include <cluster.h> |
---|
39 | #include <scheduler.h> |
---|
40 | #include <vfs.h> |
---|
41 | #include <mapper.h> |
---|
42 | #include <page.h> |
---|
43 | #include <kmem.h> |
---|
44 | #include <vmm.h> |
---|
45 | |
---|
46 | ////////////////////////////////////////////////////////////////////////////////// |
---|
47 | // Extern global variables |
---|
48 | ////////////////////////////////////////////////////////////////////////////////// |
---|
49 | |
---|
50 | extern process_t process_zero; // defined in cluster.c file |
---|
51 | |
---|
52 | |
---|
53 | //////////////////////////////////// |
---|
54 | void vmm_init( process_t * process ) |
---|
55 | { |
---|
56 | error_t error; |
---|
57 | vseg_t * vseg_kentry; |
---|
58 | vseg_t * vseg_args; |
---|
59 | vseg_t * vseg_envs; |
---|
60 | vseg_t * vseg_heap; |
---|
61 | intptr_t base; |
---|
62 | intptr_t size; |
---|
63 | |
---|
64 | vmm_dmsg("\n[INFO] %s : enter for process %x\n", __FUNCTION__ , process->pid ); |
---|
65 | |
---|
66 | // get pointer on VMM |
---|
67 | vmm_t * vmm = &process->vmm; |
---|
68 | |
---|
69 | assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) |
---|
70 | <= CONFIG_VMM_ELF_BASE) , __FUNCTION__ , "UTILS zone too small\n" ); |
---|
71 | |
---|
72 | assert( (CONFIG_THREAD_MAX_PER_CLUSTER <= 32) , __FUNCTION__ , |
---|
73 | "no more than 32 threads per cluster for a single process\n"); |
---|
74 | |
---|
75 | assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) <= |
---|
76 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , __FUNCTION__ , |
---|
77 | "STACK zone too small\n"); |
---|
78 | |
---|
79 | // initialize the rwlock protecting the vsegs list |
---|
80 | rwlock_init( &vmm->vsegs_lock ); |
---|
81 | |
---|
82 | // initialize local list of vsegs and radix-tree |
---|
83 | vmm->vsegs_nr = 0; |
---|
84 | list_root_init( &vmm->vsegs_root ); |
---|
85 | error = grdxt_init( &vmm->grdxt, |
---|
86 | CONFIG_VMM_GRDXT_W1, |
---|
87 | CONFIG_VMM_GRDXT_W2, |
---|
88 | CONFIG_VMM_GRDXT_W3 ); |
---|
89 | |
---|
90 | assert( (error == 0) , __FUNCTION__ , "cannot initialize radix tree\n" ); |
---|
91 | |
---|
92 | // register kentry vseg in VMM |
---|
93 | base = 1 << CONFIG_PPM_PAGE_SHIFT; |
---|
94 | size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
95 | vseg_kentry = vmm_create_vseg( process , base , size , VSEG_TYPE_CODE ); |
---|
96 | |
---|
97 | assert( (vseg_kentry != NULL) , __FUNCTION__ , "cannot register kentry vseg\n" ); |
---|
98 | |
---|
99 | vmm->kent_vpn_base = 1; |
---|
100 | |
---|
101 | // register the args vseg in VMM |
---|
102 | base = (CONFIG_VMM_KENTRY_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; |
---|
103 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
104 | vseg_args = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); |
---|
105 | |
---|
106 | assert( (vseg_args != NULL) , __FUNCTION__ , "cannot register args vseg\n" ); |
---|
107 | |
---|
108 | vmm->args_vpn_base = CONFIG_VMM_KENTRY_SIZE + 1; |
---|
109 | |
---|
110 | // register the envs vseg in VMM |
---|
111 | base = (CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; |
---|
112 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
113 | vseg_envs = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); |
---|
114 | |
---|
115 | assert( (vseg_envs != NULL) , __FUNCTION__ , "cannot register envs vseg\n" ); |
---|
116 | |
---|
117 | vmm->envs_vpn_base = CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1; |
---|
118 | |
---|
119 | // register the heap vseg in VMM |
---|
120 | base = CONFIG_VMM_HEAP_BASE << CONFIG_PPM_PAGE_SHIFT; |
---|
121 | size = (CONFIG_VMM_MMAP_BASE-CONFIG_VMM_HEAP_BASE) << CONFIG_PPM_PAGE_SHIFT; |
---|
122 | vseg_heap = vmm_create_vseg( process , base , size , VSEG_TYPE_HEAP ); |
---|
123 | |
---|
124 | assert( (vseg_heap != NULL) , __FUNCTION__ , "cannot register heap vseg\n" ); |
---|
125 | |
---|
126 | vmm->heap_vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
127 | |
---|
128 | // initialize generic page table |
---|
129 | error = hal_gpt_create( &vmm->gpt ); |
---|
130 | |
---|
131 | assert( (error == 0) , __FUNCTION__ , "cannot initialize page table\n"); |
---|
132 | |
---|
133 | // initialize STACK allocator |
---|
134 | vmm->stack_mgr.bitmap = 0; |
---|
135 | vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
136 | |
---|
137 | // initialize MMAP allocator |
---|
138 | vmm->mmap_mgr.vpn_base = CONFIG_VMM_MMAP_BASE; |
---|
139 | vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_MMAP_BASE; |
---|
140 | vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_MMAP_BASE; |
---|
141 | uint32_t i; |
---|
142 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] ); |
---|
143 | |
---|
144 | // initialize instrumentation counters |
---|
145 | vmm->pgfault_nr = 0; |
---|
146 | vmm->u_err_nr = 0; |
---|
147 | vmm->m_err_nr = 0; |
---|
148 | |
---|
149 | hal_fence(); |
---|
150 | |
---|
151 | vmm_dmsg("\n[INFO] %s : exit for process %x\n", __FUNCTION__ , process->pid ); |
---|
152 | |
---|
153 | } // end vmm_init() |
---|
154 | |
---|
155 | ////////////////////////////////////////// |
---|
156 | error_t vmm_copy( process_t * dst_process, |
---|
157 | process_t * src_process ) |
---|
158 | { |
---|
159 | error_t error; |
---|
160 | |
---|
161 | vmm_t * src_vmm = &src_process->vmm; |
---|
162 | vmm_t * dst_vmm = &dst_process->vmm; |
---|
163 | |
---|
164 | // take the src_vmm vsegs_lock |
---|
165 | rwlock_wr_lock( &src_vmm->vsegs_lock ); |
---|
166 | |
---|
167 | // initialize dst_vmm vsegs_lock |
---|
168 | rwlock_init( &dst_vmm->vsegs_lock ); |
---|
169 | |
---|
170 | // initialize the dst_vmm vsegs list and the radix tree |
---|
171 | dst_vmm->vsegs_nr = 0; |
---|
172 | list_root_init( &dst_vmm->vsegs_root ); |
---|
173 | error = grdxt_init( &dst_vmm->grdxt, |
---|
174 | CONFIG_VMM_GRDXT_W1, |
---|
175 | CONFIG_VMM_GRDXT_W2, |
---|
176 | CONFIG_VMM_GRDXT_W3 ); |
---|
177 | if( error ) |
---|
178 | { |
---|
179 | printk("\n[ERROR] in %s : cannot initialize radix tree for process %x\n", |
---|
180 | __FUNCTION__ , dst_process->pid ); |
---|
181 | return ENOMEM; |
---|
182 | } |
---|
183 | |
---|
184 | // loop on src_vmm list of vsegs to create |
---|
185 | // and register vsegs copies in dst_vmm |
---|
186 | list_entry_t * iter; |
---|
187 | vseg_t * src_vseg; |
---|
188 | vseg_t * dst_vseg; |
---|
189 | LIST_FOREACH( &src_vmm->vsegs_root , iter ) |
---|
190 | { |
---|
191 | // get pointer on current src_vseg |
---|
192 | src_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
193 | |
---|
194 | // allocate memory for a new dst_vseg |
---|
195 | dst_vseg = vseg_alloc(); |
---|
196 | |
---|
197 | if( dst_vseg == NULL ) |
---|
198 | { |
---|
199 | // release all allocated vsegs |
---|
200 | LIST_FOREACH( &dst_vmm->vsegs_root , iter ) |
---|
201 | { |
---|
202 | dst_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
203 | vseg_free( dst_vseg ); |
---|
204 | } |
---|
205 | return ENOMEM; |
---|
206 | } |
---|
207 | |
---|
208 | // copy src_vseg to dst_vseg |
---|
209 | vseg_init_from_ref( dst_vseg , XPTR( local_cxy , src_vseg ) ); |
---|
210 | |
---|
211 | // register dst_vseg in dst_vmm |
---|
212 | vseg_attach( dst_vmm , dst_vseg ); |
---|
213 | } |
---|
214 | |
---|
215 | // release the src_vmm vsegs_lock |
---|
216 | rwlock_wr_unlock( &src_vmm->vsegs_lock ); |
---|
217 | |
---|
218 | // initialize generic page table |
---|
219 | error = hal_gpt_create( &dst_vmm->gpt ); |
---|
220 | |
---|
221 | if( error ) |
---|
222 | { |
---|
223 | printk("\n[ERROR] in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
224 | return ENOMEM; |
---|
225 | } |
---|
226 | |
---|
227 | // initialize STACK allocator |
---|
228 | dst_vmm->stack_mgr.bitmap = 0; |
---|
229 | dst_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
230 | |
---|
231 | // initialize MMAP allocator |
---|
232 | dst_vmm->mmap_mgr.vpn_base = CONFIG_VMM_MMAP_BASE; |
---|
233 | dst_vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_MMAP_BASE; |
---|
234 | dst_vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_MMAP_BASE; |
---|
235 | uint32_t i; |
---|
236 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &dst_vmm->mmap_mgr.zombi_list[i] ); |
---|
237 | |
---|
238 | // initialize instrumentation counters |
---|
239 | dst_vmm->pgfault_nr = 0; |
---|
240 | dst_vmm->u_err_nr = 0; |
---|
241 | dst_vmm->m_err_nr = 0; |
---|
242 | |
---|
243 | // copy base addresses |
---|
244 | dst_vmm->kent_vpn_base = src_vmm->kent_vpn_base; |
---|
245 | dst_vmm->args_vpn_base = src_vmm->args_vpn_base; |
---|
246 | dst_vmm->envs_vpn_base = src_vmm->envs_vpn_base; |
---|
247 | dst_vmm->heap_vpn_base = src_vmm->heap_vpn_base; |
---|
248 | dst_vmm->code_vpn_base = src_vmm->code_vpn_base; |
---|
249 | dst_vmm->data_vpn_base = src_vmm->data_vpn_base; |
---|
250 | |
---|
251 | dst_vmm->entry_point = src_vmm->entry_point; |
---|
252 | |
---|
253 | // HEAP TODO : new heap for child ??? |
---|
254 | dst_vmm->heap_vseg = src_vmm->heap_vseg; |
---|
255 | |
---|
256 | // initialize generic page table |
---|
257 | error = hal_gpt_create( &dst_vmm->gpt ); |
---|
258 | |
---|
259 | if( error ) |
---|
260 | { |
---|
261 | printk("\n[ERROR] in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
262 | return ENOMEM; |
---|
263 | } |
---|
264 | |
---|
265 | // copy GPT content from src_vmm to dst_vmm, activating "Copy-On-Write" |
---|
266 | // TODO register Copy-On_Write in page descriptors |
---|
267 | bool_t cow = true; |
---|
268 | hal_gpt_copy( &dst_vmm->gpt , &src_vmm->gpt , cow ); |
---|
269 | |
---|
270 | hal_fence(); |
---|
271 | |
---|
272 | return 0; |
---|
273 | |
---|
274 | } // vmm_copy() |
---|
275 | |
---|
276 | /////////////////////////////////////// |
---|
277 | void vmm_destroy( process_t * process ) |
---|
278 | { |
---|
279 | vseg_t * vseg; |
---|
280 | |
---|
281 | // get pointer on VMM |
---|
282 | vmm_t * vmm = &process->vmm; |
---|
283 | |
---|
284 | // get lock protecting vseg list |
---|
285 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
286 | |
---|
287 | // remove all vsegs registered in vmm |
---|
288 | while( !list_is_empty( &vmm->vsegs_root ) ) |
---|
289 | { |
---|
290 | vseg = LIST_FIRST( &vmm->vsegs_root , vseg_t , list ); |
---|
291 | vseg_detach( vmm , vseg ); |
---|
292 | vseg_free( vseg ); |
---|
293 | } |
---|
294 | |
---|
295 | // delete vsegs radix_tree |
---|
296 | grdxt_destroy( &vmm->grdxt ); |
---|
297 | |
---|
298 | // release lock |
---|
299 | rwlock_wr_unlock(&vmm->vsegs_lock); |
---|
300 | |
---|
301 | // remove all vsegs from zombi_lists in MMAP allocator |
---|
302 | uint32_t i; |
---|
303 | for( i = 0 ; i<32 ; i++ ) |
---|
304 | { |
---|
305 | while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) ) |
---|
306 | { |
---|
307 | vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , list ); |
---|
308 | vseg_detach( vmm , vseg ); |
---|
309 | vseg_free( vseg ); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | // release memory allocated to the local page table |
---|
314 | hal_gpt_destroy( &vmm->gpt ); |
---|
315 | |
---|
316 | } // end vmm_destroy() |
---|
317 | |
---|
318 | ///////////////////////////////////////////////// |
---|
319 | vseg_t * vmm_check_conflict( process_t * process, |
---|
320 | vpn_t vpn_base, |
---|
321 | vpn_t vpn_size ) |
---|
322 | { |
---|
323 | vmm_t * vmm = &process->vmm; |
---|
324 | vseg_t * vseg; |
---|
325 | list_entry_t * iter; |
---|
326 | |
---|
327 | // scan the list of registered vsegs |
---|
328 | LIST_FOREACH( &vmm->vsegs_root , iter ) |
---|
329 | { |
---|
330 | vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
331 | |
---|
332 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && |
---|
333 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; |
---|
334 | } |
---|
335 | return NULL; |
---|
336 | |
---|
337 | } // end vmm_check_conflict() |
---|
338 | |
---|
339 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
340 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
341 | // the VMM stack_vseg specific allocator. |
---|
342 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
343 | // @ vmm : pointer on VMM. |
---|
344 | // @ vpn_base : (return value) first allocated page |
---|
345 | // @ vpn_size : (return value) number of allocated pages |
---|
346 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
347 | static error_t vmm_stack_alloc( vmm_t * vmm, |
---|
348 | vpn_t * vpn_base, |
---|
349 | vpn_t * vpn_size ) |
---|
350 | { |
---|
351 | // get stack allocator pointer |
---|
352 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
353 | |
---|
354 | // get lock on stack allocator |
---|
355 | spinlock_lock( &mgr->lock ); |
---|
356 | |
---|
357 | // get first free slot index in bitmap |
---|
358 | int32_t index = bitmap_ffc( &mgr->bitmap , 4 ); |
---|
359 | if( (index < 0) || (index > 31) ) |
---|
360 | { |
---|
361 | spinlock_unlock( &mgr->lock ); |
---|
362 | return ENOMEM; |
---|
363 | } |
---|
364 | |
---|
365 | // update bitmap |
---|
366 | bitmap_set( &mgr->bitmap , index ); |
---|
367 | |
---|
368 | // release lock on stack allocator |
---|
369 | spinlock_unlock( &mgr->lock ); |
---|
370 | |
---|
371 | // returns vpn_base, vpn_size (one page non allocated) |
---|
372 | *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1; |
---|
373 | *vpn_size = CONFIG_VMM_STACK_SIZE - 1; |
---|
374 | return 0; |
---|
375 | |
---|
376 | } // end vmm_stack_alloc() |
---|
377 | |
---|
378 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
379 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
380 | // the VMM MMAP specific allocator. |
---|
381 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
382 | // @ vmm : [in] pointer on VMM. |
---|
383 | // @ npages : [in] requested number of pages. |
---|
384 | // @ vpn_base : [out] first allocated page. |
---|
385 | // @ vpn_size : [out] actual number of allocated pages. |
---|
386 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
387 | static error_t vmm_mmap_alloc( vmm_t * vmm, |
---|
388 | vpn_t npages, |
---|
389 | vpn_t * vpn_base, |
---|
390 | vpn_t * vpn_size ) |
---|
391 | { |
---|
392 | uint32_t index; |
---|
393 | vseg_t * vseg; |
---|
394 | vpn_t base; |
---|
395 | vpn_t size; |
---|
396 | vpn_t free; |
---|
397 | |
---|
398 | // mmap vseg size must be power of 2 |
---|
399 | // compute actual size and index in zombi_list array |
---|
400 | size = POW2_ROUNDUP( npages ); |
---|
401 | index = bits_log2( size ); |
---|
402 | |
---|
403 | // get mmap allocator pointer |
---|
404 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
405 | |
---|
406 | // get lock on mmap allocator |
---|
407 | spinlock_lock( &mgr->lock ); |
---|
408 | |
---|
409 | // get vseg from zombi_list or from mmap zone |
---|
410 | if( list_is_empty( &mgr->zombi_list[index] ) ) // from mmap zone |
---|
411 | { |
---|
412 | // check overflow |
---|
413 | free = mgr->first_free_vpn; |
---|
414 | if( (free + size) > mgr->vpn_size ) return ENOMEM; |
---|
415 | |
---|
416 | // update STACK allocator |
---|
417 | mgr->first_free_vpn += size; |
---|
418 | |
---|
419 | // compute base |
---|
420 | base = free; |
---|
421 | } |
---|
422 | else // from zombi_list |
---|
423 | { |
---|
424 | // get pointer on zombi vseg from zombi_list |
---|
425 | vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , list ); |
---|
426 | |
---|
427 | // remove vseg from free-list |
---|
428 | list_unlink( &vseg->list ); |
---|
429 | |
---|
430 | // compute base |
---|
431 | base = vseg->vpn_base; |
---|
432 | } |
---|
433 | |
---|
434 | // release lock on mmap allocator |
---|
435 | spinlock_unlock( &mgr->lock ); |
---|
436 | |
---|
437 | // returns vpn_base, vpn_size |
---|
438 | *vpn_base = base; |
---|
439 | *vpn_size = size; |
---|
440 | return 0; |
---|
441 | |
---|
442 | } // end vmm_mmap_alloc() |
---|
443 | |
---|
444 | ////////////////////////////////////////////// |
---|
445 | vseg_t * vmm_create_vseg( process_t * process, |
---|
446 | intptr_t base, |
---|
447 | intptr_t size, |
---|
448 | uint32_t type ) |
---|
449 | { |
---|
450 | vseg_t * vseg; // created vseg pointer |
---|
451 | vpn_t vpn_base; // first page index |
---|
452 | vpn_t vpn_size; // number of pages |
---|
453 | error_t error; |
---|
454 | |
---|
455 | // get pointer on VMM |
---|
456 | vmm_t * vmm = &process->vmm; |
---|
457 | |
---|
458 | vmm_dmsg("\n[INFO] %s : enter for process %x / base = %x / size = %x / type = %s\n", |
---|
459 | __FUNCTION__ , process->pid , base , size , vseg_type_str(type) ); |
---|
460 | |
---|
461 | // compute base, size, vpn_base, vpn_size, depending on vseg type |
---|
462 | // we use the VMM specific allocators for STACK and MMAP vsegs |
---|
463 | if( type == VSEG_TYPE_STACK ) |
---|
464 | { |
---|
465 | // get vpn_base and vpn_size from STACK allocator |
---|
466 | error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size ); |
---|
467 | if( error ) |
---|
468 | { |
---|
469 | printk("\n[ERROR] in %s : no vspace for stack vseg / process %x in cluster %x\n", |
---|
470 | __FUNCTION__ , process->pid , local_cxy ); |
---|
471 | return NULL; |
---|
472 | } |
---|
473 | |
---|
474 | // compute vseg base and size from vpn_base and vpn_size |
---|
475 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
476 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
477 | } |
---|
478 | else if( (type == VSEG_TYPE_ANON) || |
---|
479 | (type == VSEG_TYPE_FILE) || |
---|
480 | (type == VSEG_TYPE_REMOTE) ) |
---|
481 | { |
---|
482 | // get vpn_base and vpn_size from MMAP allocator |
---|
483 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
484 | error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size ); |
---|
485 | if( error ) |
---|
486 | { |
---|
487 | printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n", |
---|
488 | __FUNCTION__ , process->pid , local_cxy ); |
---|
489 | return NULL; |
---|
490 | } |
---|
491 | |
---|
492 | // compute vseg base and size from vpn_base and vpn_size |
---|
493 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
494 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
495 | } |
---|
496 | else |
---|
497 | { |
---|
498 | uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
499 | uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
500 | |
---|
501 | vpn_base = vpn_min; |
---|
502 | vpn_size = vpn_max - vpn_min + 1; |
---|
503 | } |
---|
504 | |
---|
505 | // check collisions |
---|
506 | vseg = vmm_check_conflict( process , vpn_base , vpn_size ); |
---|
507 | if( vseg != NULL ) |
---|
508 | { |
---|
509 | printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n" |
---|
510 | " overlap existing vseg [vpn_base = %x / vpn_size = %x]\n", |
---|
511 | __FUNCTION__ , process->pid, vpn_base, vpn_size, |
---|
512 | vseg->vpn_base, vseg->vpn_size ); |
---|
513 | return NULL; |
---|
514 | } |
---|
515 | |
---|
516 | // allocate physical memory for vseg descriptor |
---|
517 | vseg = vseg_alloc(); |
---|
518 | if( vseg == NULL ) |
---|
519 | { |
---|
520 | printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n", |
---|
521 | __FUNCTION__ , process->pid ); |
---|
522 | return NULL; |
---|
523 | } |
---|
524 | |
---|
525 | // initialize vseg descriptor |
---|
526 | vseg_init( vseg , base, size , vpn_base , vpn_size , type , local_cxy ); |
---|
527 | |
---|
528 | // update "heap_vseg" in VMM |
---|
529 | process->vmm.heap_vseg = vseg; |
---|
530 | |
---|
531 | // attach vseg to vmm |
---|
532 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
533 | vseg_attach( vmm , vseg ); |
---|
534 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
535 | |
---|
536 | vmm_dmsg("\n[INFO] %s : exit for process %x / vseg [%x, %x] has been mapped\n", |
---|
537 | __FUNCTION__ , process->pid , vseg->min , vseg->max ); |
---|
538 | |
---|
539 | return vseg; |
---|
540 | } |
---|
541 | |
---|
542 | ///////////////////////////////////// |
---|
543 | void vmm_remove_vseg( vseg_t * vseg ) |
---|
544 | { |
---|
545 | // get pointers on calling process and VMM |
---|
546 | thread_t * this = CURRENT_THREAD; |
---|
547 | process_t * process = this->process; |
---|
548 | vmm_t * vmm = &this->process->vmm; |
---|
549 | uint32_t type = vseg->type; |
---|
550 | |
---|
551 | // detach vseg from VMM |
---|
552 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
553 | vseg_detach( &process->vmm , vseg ); |
---|
554 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
555 | |
---|
556 | // release the stack slot to VMM stack allocator if STACK type |
---|
557 | if( type == VSEG_TYPE_STACK ) |
---|
558 | { |
---|
559 | // get pointer on stack allocator |
---|
560 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
561 | |
---|
562 | // compute slot index |
---|
563 | uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE); |
---|
564 | |
---|
565 | // update stacks_bitmap |
---|
566 | spinlock_lock( &mgr->lock ); |
---|
567 | bitmap_clear( &mgr->bitmap , index ); |
---|
568 | spinlock_unlock( &mgr->lock ); |
---|
569 | } |
---|
570 | |
---|
571 | // release the vseg to VMM mmap allocator if MMAP type |
---|
572 | if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) ) |
---|
573 | { |
---|
574 | // get pointer on mmap allocator |
---|
575 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
576 | |
---|
577 | // compute zombi_list index |
---|
578 | uint32_t index = bits_log2( vseg->vpn_size ); |
---|
579 | |
---|
580 | // update zombi_list |
---|
581 | spinlock_lock( &mgr->lock ); |
---|
582 | list_add_first( &mgr->zombi_list[index] , &vseg->list ); |
---|
583 | spinlock_unlock( &mgr->lock ); |
---|
584 | } |
---|
585 | |
---|
586 | // release physical memory allocated for vseg descriptor if no MMAP type |
---|
587 | if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) ) |
---|
588 | { |
---|
589 | vseg_free( vseg ); |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | ////////////////////////////////////////////// |
---|
594 | error_t vmm_map_kernel_vseg( vseg_t * vseg, |
---|
595 | uint32_t attr ) |
---|
596 | { |
---|
597 | vpn_t vpn; // VPN of PTE to be set |
---|
598 | vpn_t vpn_min; // VPN of first PTE to be set |
---|
599 | vpn_t vpn_max; // VPN of last PTE to be set (excluded) |
---|
600 | ppn_t ppn; // PPN of allocated physical page |
---|
601 | uint32_t order; // ln( number of small pages for one single PTE ) |
---|
602 | page_t * page; |
---|
603 | error_t error; |
---|
604 | |
---|
605 | // check vseg type : must be a kernel vseg |
---|
606 | uint32_t type = vseg->type; |
---|
607 | assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)), |
---|
608 | __FUNCTION__ , "not a kernel vseg\n" ); |
---|
609 | |
---|
610 | // get pointer on page table |
---|
611 | gpt_t * gpt = &process_zero.vmm.gpt; |
---|
612 | |
---|
613 | // define number of small pages per PTE |
---|
614 | if( attr & GPT_SMALL ) order = 0; // 1 small page |
---|
615 | else order = 9; // 512 small pages |
---|
616 | |
---|
617 | // loop on pages in vseg |
---|
618 | vpn_min = vseg->vpn_base; |
---|
619 | vpn_max = vpn_min + vseg->vpn_size; |
---|
620 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
621 | { |
---|
622 | // allocate a physical page from local PPM |
---|
623 | kmem_req_t req; |
---|
624 | req.type = KMEM_PAGE; |
---|
625 | req.size = order; |
---|
626 | req.flags = AF_KERNEL | AF_ZERO; |
---|
627 | page = (page_t *)kmem_alloc( &req ); |
---|
628 | if( page == NULL ) |
---|
629 | { |
---|
630 | printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ ); |
---|
631 | return ENOMEM; |
---|
632 | } |
---|
633 | |
---|
634 | // set page table entry |
---|
635 | ppn = ppm_page2ppn( XPTR( local_cxy , page ) ); |
---|
636 | error = hal_gpt_set_pte( gpt , vpn , ppn , attr ); |
---|
637 | if( error ) |
---|
638 | { |
---|
639 | printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ ); |
---|
640 | return ENOMEM; |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | return 0; |
---|
645 | } |
---|
646 | |
---|
647 | ///////////////////////////////////////// |
---|
648 | void vmm_unmap_vseg( process_t * process, |
---|
649 | vseg_t * vseg ) |
---|
650 | { |
---|
651 | vpn_t vpn; // VPN of current PTE |
---|
652 | vpn_t vpn_min; // VPN of first PTE |
---|
653 | vpn_t vpn_max; // VPN of last PTE (excluded) |
---|
654 | |
---|
655 | // get pointer on process page table |
---|
656 | gpt_t * gpt = &process->vmm.gpt; |
---|
657 | |
---|
658 | // loop on pages in vseg |
---|
659 | vpn_min = vseg->vpn_base; |
---|
660 | vpn_max = vpn_min + vseg->vpn_size; |
---|
661 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
662 | { |
---|
663 | hal_gpt_reset_pte( gpt , vpn ); |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | ///////////////////////////////////////////// |
---|
668 | error_t vmm_resize_vseg( process_t * process, |
---|
669 | intptr_t base, |
---|
670 | intptr_t size ) |
---|
671 | { |
---|
672 | error_t error; |
---|
673 | |
---|
674 | // get pointer on process VMM |
---|
675 | vmm_t * vmm = &process->vmm; |
---|
676 | |
---|
677 | intptr_t addr_min = base; |
---|
678 | intptr_t addr_max = base + size; |
---|
679 | uint32_t shift = CONFIG_PPM_PAGE_SHIFT; |
---|
680 | |
---|
681 | // get pointer on vseg |
---|
682 | vseg_t * vseg = grdxt_lookup( &vmm->grdxt , (uint32_t)(base >> shift) ); |
---|
683 | |
---|
684 | if( vseg == NULL) return EINVAL; |
---|
685 | |
---|
686 | // get VMM lock protecting vsegs list |
---|
687 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
688 | |
---|
689 | if( (vseg->min > addr_min) || (vseg->max < addr_max) ) // region not included in vseg |
---|
690 | { |
---|
691 | error = EINVAL; |
---|
692 | } |
---|
693 | else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed |
---|
694 | { |
---|
695 | vmm_remove_vseg( vseg ); |
---|
696 | error = 0; |
---|
697 | } |
---|
698 | else if( vseg->min == addr_min ) // vseg must be resized |
---|
699 | { |
---|
700 | panic("resize not implemented yet"); |
---|
701 | error = 0; |
---|
702 | } |
---|
703 | else if( vseg->max == addr_max ) // vseg must be resized |
---|
704 | { |
---|
705 | panic("resize not implemented yet"); |
---|
706 | error = 0; |
---|
707 | } |
---|
708 | else // vseg cut in three regions => vseg must be resized & new vseg created |
---|
709 | { |
---|
710 | panic("resize not implemented yet"); |
---|
711 | error = 0; |
---|
712 | } |
---|
713 | |
---|
714 | // release VMM lock |
---|
715 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
716 | |
---|
717 | return error; |
---|
718 | } |
---|
719 | |
---|
720 | /////////////////////////////////////////// |
---|
721 | error_t vmm_get_vseg( process_t * process, |
---|
722 | intptr_t vaddr, |
---|
723 | vseg_t ** found_vseg ) |
---|
724 | { |
---|
725 | vmm_t * vmm; |
---|
726 | vseg_t * vseg; |
---|
727 | |
---|
728 | // get pointer on process VMM |
---|
729 | vmm = &process->vmm; |
---|
730 | |
---|
731 | // get lock protecting the vseg list |
---|
732 | rwlock_rd_lock( &vmm->vsegs_lock ); |
---|
733 | |
---|
734 | // get pointer on vseg from local radix tree |
---|
735 | vseg = grdxt_lookup( &vmm->grdxt, (uint32_t)(vaddr >> CONFIG_PPM_PAGE_SHIFT) ); |
---|
736 | |
---|
737 | // release the lock |
---|
738 | rwlock_rd_unlock( &vmm->vsegs_lock ); |
---|
739 | |
---|
740 | if( vseg == NULL ) // vseg not found in local cluster => try to get it from ref |
---|
741 | { |
---|
742 | // get extended pointer on reference process |
---|
743 | xptr_t ref_xp = process->ref_xp; |
---|
744 | |
---|
745 | // get cluster and local pointer on reference process |
---|
746 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
747 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
748 | |
---|
749 | if( local_cxy == ref_cxy ) return -1; // local cluster is the reference |
---|
750 | |
---|
751 | // get extended pointer on reference vseg |
---|
752 | xptr_t vseg_xp; |
---|
753 | error_t error; |
---|
754 | rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error ); |
---|
755 | |
---|
756 | if( error ) return -1; // vseg not found => illegal user vaddr |
---|
757 | |
---|
758 | // allocate a vseg in local cluster |
---|
759 | vseg = vseg_alloc(); |
---|
760 | |
---|
761 | if( vseg == NULL ) panic("no memory for vseg copy in cluster %x", local_cxy ); |
---|
762 | |
---|
763 | // initialise local vseg from reference |
---|
764 | vseg_init_from_ref( vseg , vseg_xp ); |
---|
765 | |
---|
766 | // register local vseg in local VMM |
---|
767 | error = vseg_attach( &process->vmm , vseg ); |
---|
768 | |
---|
769 | if( error ) panic("no memory for vseg registration in cluster %x", local_cxy ); |
---|
770 | } |
---|
771 | |
---|
772 | // success |
---|
773 | *found_vseg = vseg; |
---|
774 | return 0; |
---|
775 | |
---|
776 | } // end vmm_get_vseg() |
---|
777 | |
---|
778 | //////////////////////////////////////// |
---|
779 | error_t vmm_get_one_ppn( vseg_t * vseg, |
---|
780 | vpn_t vpn, |
---|
781 | ppn_t * ppn ) |
---|
782 | { |
---|
783 | error_t error; |
---|
784 | cxy_t page_cxy; // physical page cluster |
---|
785 | page_t * page_ptr; // local pointer on physical page descriptor |
---|
786 | |
---|
787 | uint32_t type = vseg->type; |
---|
788 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
789 | uint32_t flags = vseg->flags; |
---|
790 | |
---|
791 | // get mapper cluster and local pointer |
---|
792 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
793 | mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); |
---|
794 | |
---|
795 | // FILE type : simply get the physical page from the file mapper |
---|
796 | if( type == VSEG_TYPE_FILE ) |
---|
797 | { |
---|
798 | // compute index in file mapper |
---|
799 | uint32_t index = vpn - vseg->vpn_base; |
---|
800 | |
---|
801 | // get page descriptor from mapper |
---|
802 | if( mapper_cxy == local_cxy ) // mapper is local |
---|
803 | { |
---|
804 | page_ptr = mapper_get_page( mapper_ptr , index ); |
---|
805 | } |
---|
806 | else // mapper is remote |
---|
807 | { |
---|
808 | rpc_mapper_get_page_client( mapper_cxy , mapper_ptr , index , &page_ptr ); |
---|
809 | } |
---|
810 | |
---|
811 | if ( page_ptr == NULL ) return EINVAL; |
---|
812 | |
---|
813 | page_cxy = mapper_cxy; |
---|
814 | } |
---|
815 | |
---|
816 | // all other types : allocate a physical page from target cluster, |
---|
817 | else |
---|
818 | { |
---|
819 | // get target cluster for physical page |
---|
820 | if( flags & VSEG_DISTRIB ) // depends on VPN LSB |
---|
821 | { |
---|
822 | uint32_t x_width = LOCAL_CLUSTER->x_width; |
---|
823 | uint32_t y_width = LOCAL_CLUSTER->y_width; |
---|
824 | page_cxy = vpn & ((1<<(x_width + y_width)) - 1); |
---|
825 | } |
---|
826 | else // defined in vseg descriptor |
---|
827 | { |
---|
828 | page_cxy = vseg->cxy; |
---|
829 | } |
---|
830 | |
---|
831 | // allocate a physical page in target cluster |
---|
832 | kmem_req_t req; |
---|
833 | if( page_cxy == local_cxy ) // target cluster is the local cluster |
---|
834 | { |
---|
835 | req.type = KMEM_PAGE; |
---|
836 | req.size = 0; |
---|
837 | req.flags = AF_NONE; |
---|
838 | page_ptr = (page_t *)kmem_alloc( &req ); |
---|
839 | } |
---|
840 | else // target cluster is not the local cluster |
---|
841 | { |
---|
842 | rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr ); |
---|
843 | } |
---|
844 | |
---|
845 | if( page_ptr == NULL ) return ENOMEM; |
---|
846 | |
---|
847 | // initialise page from .elf file mapper for DATA and CODE types |
---|
848 | if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) |
---|
849 | { |
---|
850 | // compute missing page index in vseg |
---|
851 | vpn_t page_index = vpn - vseg->vpn_base; |
---|
852 | |
---|
853 | // compute missing page offset in .elf file |
---|
854 | intptr_t page_offset = vseg->file_offset + |
---|
855 | (page_index << CONFIG_PPM_PAGE_SHIFT); |
---|
856 | |
---|
857 | // compute extended pointer on page first byte |
---|
858 | xptr_t base_xp = ppm_page2base( XPTR( page_cxy , page_ptr ) ); |
---|
859 | |
---|
860 | // file_size can be smaller than vseg_size for BSS |
---|
861 | intptr_t file_size = vseg->file_size; |
---|
862 | |
---|
863 | if( file_size < page_offset ) // fully in BSS |
---|
864 | { |
---|
865 | if( page_cxy == local_cxy ) |
---|
866 | { |
---|
867 | memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
868 | } |
---|
869 | else |
---|
870 | { |
---|
871 | hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
872 | } |
---|
873 | } |
---|
874 | else if( file_size >= (page_offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper |
---|
875 | { |
---|
876 | if( mapper_cxy == local_cxy ) |
---|
877 | { |
---|
878 | error = mapper_move_kernel( mapper_ptr, |
---|
879 | true, // to_buffer |
---|
880 | page_offset, |
---|
881 | base_xp, |
---|
882 | CONFIG_PPM_PAGE_SIZE ); |
---|
883 | } |
---|
884 | else |
---|
885 | { |
---|
886 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
887 | mapper_ptr, |
---|
888 | true, // to buffer |
---|
889 | false, // kernel buffer |
---|
890 | page_offset, |
---|
891 | (uint64_t)base_xp, |
---|
892 | CONFIG_PPM_PAGE_SIZE, |
---|
893 | &error ); |
---|
894 | } |
---|
895 | if( error ) return EINVAL; |
---|
896 | } |
---|
897 | else // in mapper : from page_offset -> (file_size - page_offset) |
---|
898 | // in BSS : from file_size -> (page_offset + page_size) |
---|
899 | { |
---|
900 | // initialize mapper part |
---|
901 | if( mapper_cxy == local_cxy ) |
---|
902 | { |
---|
903 | error = mapper_move_kernel( mapper_ptr, |
---|
904 | true, // to buffer |
---|
905 | page_offset, |
---|
906 | base_xp, |
---|
907 | file_size - page_offset ); |
---|
908 | } |
---|
909 | else |
---|
910 | { |
---|
911 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
912 | mapper_ptr, |
---|
913 | true, // to buffer |
---|
914 | false, // kernel buffer |
---|
915 | page_offset, |
---|
916 | (uint64_t)base_xp, |
---|
917 | file_size - page_offset, |
---|
918 | &error ); |
---|
919 | } |
---|
920 | if( error ) return EINVAL; |
---|
921 | |
---|
922 | // initialize BSS part |
---|
923 | if( page_cxy == local_cxy ) |
---|
924 | { |
---|
925 | memset( GET_PTR( base_xp ) + file_size - page_offset , 0 , |
---|
926 | page_offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
927 | } |
---|
928 | else |
---|
929 | { |
---|
930 | hal_remote_memset( base_xp + file_size - page_offset , 0 , |
---|
931 | page_offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
932 | } |
---|
933 | } |
---|
934 | } // end initialisation for CODE or DATA types |
---|
935 | } |
---|
936 | |
---|
937 | // return ppn |
---|
938 | *ppn = ppm_page2ppn( XPTR( page_cxy , page_ptr ) ); |
---|
939 | return 0; |
---|
940 | |
---|
941 | } // end vmm_get_one_ppn() |
---|
942 | |
---|
943 | ///////////////////////////////////////// |
---|
944 | error_t vmm_get_pte( process_t * process, |
---|
945 | vpn_t vpn, |
---|
946 | uint32_t * ret_attr, |
---|
947 | ppn_t * ret_ppn ) |
---|
948 | { |
---|
949 | vseg_t * vseg; // pointer on vseg containing VPN |
---|
950 | ppn_t ppn; // physical page number |
---|
951 | uint32_t attr; // attributes from GPT entry |
---|
952 | error_t error; |
---|
953 | |
---|
954 | // this function must be called by a thread running in the reference cluster |
---|
955 | assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , |
---|
956 | " not called in the reference cluster\n" ); |
---|
957 | |
---|
958 | // get VMM pointer |
---|
959 | vmm_t * vmm = &process->vmm; |
---|
960 | |
---|
961 | // access GPT to get PTE attributes and PPN |
---|
962 | hal_gpt_get_pte( &vmm->gpt , vpn , &attr , &ppn ); |
---|
963 | |
---|
964 | // if PTE is unmapped |
---|
965 | // 1) get VSEG containing the missing VPN |
---|
966 | // 2) get & initialize physical page (depending on vseg type), |
---|
967 | // 3) register the PTE in reference GPT |
---|
968 | if( (attr & GPT_MAPPED) == 0 ) |
---|
969 | { |
---|
970 | // 1. get vseg pointer |
---|
971 | error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg ); |
---|
972 | |
---|
973 | if( error ) |
---|
974 | { |
---|
975 | printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n", |
---|
976 | __FUNCTION__ , process->pid , vpn ); |
---|
977 | return error; |
---|
978 | } |
---|
979 | |
---|
980 | // 2. get physical page number, depending on vseg type |
---|
981 | error = vmm_get_one_ppn( vseg , vpn , &ppn ); |
---|
982 | |
---|
983 | if( error ) |
---|
984 | { |
---|
985 | printk("\n[ERROR] in %s : cannot allocate memory / process = %x / vpn = %x\n", |
---|
986 | __FUNCTION__ , process->pid , vpn ); |
---|
987 | return error; |
---|
988 | } |
---|
989 | |
---|
990 | // 3. define attributes from vseg flags and register in GPT |
---|
991 | attr = GPT_MAPPED | GPT_SMALL; |
---|
992 | if( vseg->flags & VSEG_USER ) attr |= GPT_USER; |
---|
993 | if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE; |
---|
994 | if( vseg->flags & VSEG_EXEC ) attr |= GPT_EXECUTABLE; |
---|
995 | if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE; |
---|
996 | |
---|
997 | error = hal_gpt_set_pte( &vmm->gpt , vpn , ppn , attr ); |
---|
998 | |
---|
999 | if( error ) |
---|
1000 | { |
---|
1001 | printk("\n[ERROR] in %s : cannot register PTE / process = %x / vpn = %x\n", |
---|
1002 | __FUNCTION__ , process->pid , vpn ); |
---|
1003 | return error; |
---|
1004 | } |
---|
1005 | } // end new PTE |
---|
1006 | |
---|
1007 | *ret_ppn = ppn; |
---|
1008 | *ret_attr = attr; |
---|
1009 | return 0; |
---|
1010 | |
---|
1011 | } // end vmm_get_pte() |
---|
1012 | |
---|
1013 | /////////////////////////////////////////////////// |
---|
1014 | error_t vmm_handle_page_fault( process_t * process, |
---|
1015 | vseg_t * vseg, |
---|
1016 | vpn_t vpn ) |
---|
1017 | { |
---|
1018 | uint32_t attr; // missing page attributes |
---|
1019 | ppn_t ppn; // missing page PPN |
---|
1020 | error_t error; // return value |
---|
1021 | |
---|
1022 | // get local VMM pointer |
---|
1023 | vmm_t * vmm = &process->vmm; |
---|
1024 | |
---|
1025 | // get reference process cluster and local pointer |
---|
1026 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
1027 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
1028 | |
---|
1029 | // get missing PTE attributes and PPN from reference cluster |
---|
1030 | if( local_cxy != ref_cxy ) // local cluster is not the reference cluster |
---|
1031 | { |
---|
1032 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , &attr , &ppn , &error ); |
---|
1033 | } |
---|
1034 | else // local cluster is the reference cluster |
---|
1035 | { |
---|
1036 | error = vmm_get_pte( process , vpn , &attr , &ppn ); |
---|
1037 | } |
---|
1038 | |
---|
1039 | return error; |
---|
1040 | |
---|
1041 | } // end vmm_handle_page_fault() |
---|
1042 | |
---|
1043 | |
---|
1044 | /////////////////////////////////////////// |
---|
1045 | error_t vmm_v2p_translate( bool_t ident, |
---|
1046 | void * ptr, |
---|
1047 | paddr_t * paddr ) |
---|
1048 | { |
---|
1049 | process_t * process = CURRENT_THREAD->process; |
---|
1050 | |
---|
1051 | if( ident ) // identity mapping |
---|
1052 | { |
---|
1053 | *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr ); |
---|
1054 | return 0; |
---|
1055 | } |
---|
1056 | |
---|
1057 | // access page table |
---|
1058 | error_t error; |
---|
1059 | vpn_t vpn; |
---|
1060 | uint32_t attr; |
---|
1061 | ppn_t ppn; |
---|
1062 | uint32_t offset; |
---|
1063 | |
---|
1064 | vpn = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT ); |
---|
1065 | offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK ); |
---|
1066 | |
---|
1067 | if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process |
---|
1068 | { |
---|
1069 | error = vmm_get_pte( process, vpn , &attr , &ppn ); |
---|
1070 | } |
---|
1071 | else // calling process is not reference process |
---|
1072 | { |
---|
1073 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
1074 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
1075 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , &attr , &ppn , &error ); |
---|
1076 | } |
---|
1077 | |
---|
1078 | // set paddr |
---|
1079 | *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset; |
---|
1080 | |
---|
1081 | return error; |
---|
1082 | |
---|
1083 | } // end vmm_v2p_translate() |
---|
1084 | |
---|
1085 | |
---|