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 <hal_vmm.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <memcpy.h> |
---|
33 | #include <rwlock.h> |
---|
34 | #include <list.h> |
---|
35 | #include <xlist.h> |
---|
36 | #include <bits.h> |
---|
37 | #include <process.h> |
---|
38 | #include <thread.h> |
---|
39 | #include <vseg.h> |
---|
40 | #include <cluster.h> |
---|
41 | #include <scheduler.h> |
---|
42 | #include <vfs.h> |
---|
43 | #include <mapper.h> |
---|
44 | #include <page.h> |
---|
45 | #include <kmem.h> |
---|
46 | #include <vmm.h> |
---|
47 | |
---|
48 | ////////////////////////////////////////////////////////////////////////////////// |
---|
49 | // Extern global variables |
---|
50 | ////////////////////////////////////////////////////////////////////////////////// |
---|
51 | |
---|
52 | extern process_t process_zero; // defined in cluster.c file |
---|
53 | |
---|
54 | |
---|
55 | /////////////////////////////////////// |
---|
56 | error_t vmm_init( process_t * process ) |
---|
57 | { |
---|
58 | error_t error; |
---|
59 | vseg_t * vseg_kentry; |
---|
60 | vseg_t * vseg_args; |
---|
61 | vseg_t * vseg_envs; |
---|
62 | intptr_t base; |
---|
63 | intptr_t size; |
---|
64 | |
---|
65 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enters for process %x\n", |
---|
66 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , process->pid ); |
---|
67 | |
---|
68 | // get pointer on VMM |
---|
69 | vmm_t * vmm = &process->vmm; |
---|
70 | |
---|
71 | // initialize local list of vsegs |
---|
72 | vmm->vsegs_nr = 0; |
---|
73 | xlist_root_init( XPTR( local_cxy , &vmm->vsegs_root ) ); |
---|
74 | remote_rwlock_init( XPTR( local_cxy , &vmm->vsegs_lock ) ); |
---|
75 | |
---|
76 | assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) |
---|
77 | <= CONFIG_VMM_ELF_BASE) , __FUNCTION__ , "UTILS zone too small\n" ); |
---|
78 | |
---|
79 | assert( (CONFIG_THREAD_MAX_PER_CLUSTER <= 32) , __FUNCTION__ , |
---|
80 | "no more than 32 threads per cluster for a single process\n"); |
---|
81 | |
---|
82 | assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) <= |
---|
83 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , __FUNCTION__ , |
---|
84 | "STACK zone too small\n"); |
---|
85 | |
---|
86 | // register kentry vseg in VSL |
---|
87 | base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT; |
---|
88 | size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
89 | |
---|
90 | vseg_kentry = vmm_create_vseg( process, |
---|
91 | VSEG_TYPE_CODE, |
---|
92 | base, |
---|
93 | size, |
---|
94 | 0, // file_offset unused |
---|
95 | 0, // file_size unused |
---|
96 | XPTR_NULL, // mapper_xp unused |
---|
97 | local_cxy ); |
---|
98 | |
---|
99 | if( vseg_kentry == NULL ) |
---|
100 | { |
---|
101 | printk("\n[ERROR] in %s : cannot register kentry vseg\n", __FUNCTION__ ); |
---|
102 | return -1; |
---|
103 | } |
---|
104 | |
---|
105 | vmm->kent_vpn_base = base; |
---|
106 | |
---|
107 | // register args vseg in VSL |
---|
108 | base = (CONFIG_VMM_KENTRY_BASE + |
---|
109 | CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT; |
---|
110 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
111 | |
---|
112 | vseg_args = vmm_create_vseg( process, |
---|
113 | VSEG_TYPE_DATA, |
---|
114 | base, |
---|
115 | size, |
---|
116 | 0, // file_offset unused |
---|
117 | 0, // file_size unused |
---|
118 | XPTR_NULL, // mapper_xp unused |
---|
119 | local_cxy ); |
---|
120 | |
---|
121 | if( vseg_args == NULL ) |
---|
122 | { |
---|
123 | printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ ); |
---|
124 | return -1; |
---|
125 | } |
---|
126 | |
---|
127 | vmm->args_vpn_base = base; |
---|
128 | |
---|
129 | // register the envs vseg in VSL |
---|
130 | base = (CONFIG_VMM_KENTRY_BASE + |
---|
131 | CONFIG_VMM_KENTRY_SIZE + |
---|
132 | CONFIG_VMM_ARGS_SIZE ) << CONFIG_PPM_PAGE_SHIFT; |
---|
133 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
134 | |
---|
135 | vseg_envs = vmm_create_vseg( process, |
---|
136 | VSEG_TYPE_DATA, |
---|
137 | base, |
---|
138 | size, |
---|
139 | 0, // file_offset unused |
---|
140 | 0, // file_size unused |
---|
141 | XPTR_NULL, // mapper_xp unused |
---|
142 | local_cxy ); |
---|
143 | |
---|
144 | if( vseg_envs == NULL ) |
---|
145 | { |
---|
146 | printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ ); |
---|
147 | return -1; |
---|
148 | } |
---|
149 | |
---|
150 | vmm->envs_vpn_base = base; |
---|
151 | |
---|
152 | // create GPT (empty) |
---|
153 | error = hal_gpt_create( &vmm->gpt ); |
---|
154 | |
---|
155 | if( error ) |
---|
156 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); |
---|
157 | |
---|
158 | // initialize GPT (architecture specic) |
---|
159 | // (For TSAR, identity map the kentry_vseg) |
---|
160 | error = hal_vmm_init( vmm ); |
---|
161 | |
---|
162 | if( error ) |
---|
163 | printk("\n[ERROR] in %s : cannot initialize GPT\n", __FUNCTION__ ); |
---|
164 | |
---|
165 | // initialize STACK allocator |
---|
166 | vmm->stack_mgr.bitmap = 0; |
---|
167 | vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
168 | |
---|
169 | // initialize MMAP allocator |
---|
170 | vmm->mmap_mgr.vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
171 | vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; |
---|
172 | vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_HEAP_BASE; |
---|
173 | uint32_t i; |
---|
174 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] ); |
---|
175 | |
---|
176 | // initialize instrumentation counters |
---|
177 | vmm->pgfault_nr = 0; |
---|
178 | |
---|
179 | hal_fence(); |
---|
180 | |
---|
181 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x / entry_point = %x\n", |
---|
182 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
183 | process->pid , process->vmm.entry_point ); |
---|
184 | |
---|
185 | return 0; |
---|
186 | |
---|
187 | } // end vmm_init() |
---|
188 | |
---|
189 | ////////////////////////////////////// |
---|
190 | void vmm_display( process_t * process, |
---|
191 | bool_t mapping ) |
---|
192 | { |
---|
193 | vmm_t * vmm = &process->vmm; |
---|
194 | gpt_t * gpt = &vmm->gpt; |
---|
195 | |
---|
196 | printk("\n***** VSL and GPT for process %x\n\n", |
---|
197 | process->pid ); |
---|
198 | |
---|
199 | // get lock protecting the vseg list |
---|
200 | remote_rwlock_rd_lock( XPTR( local_cxy , &vmm->vsegs_lock ) ); |
---|
201 | |
---|
202 | // scan the list of vsegs |
---|
203 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
204 | xptr_t iter_xp; |
---|
205 | xptr_t vseg_xp; |
---|
206 | vseg_t * vseg; |
---|
207 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
208 | { |
---|
209 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
210 | vseg = (vseg_t *)GET_PTR( vseg_xp ); |
---|
211 | |
---|
212 | printk(" - %s : base = %X / size = %X / npages = %d\n", |
---|
213 | vseg_type_str( vseg->type ) , vseg->min , vseg->max - vseg->min , vseg->vpn_size ); |
---|
214 | |
---|
215 | if( mapping ) |
---|
216 | { |
---|
217 | vpn_t vpn; |
---|
218 | ppn_t ppn; |
---|
219 | uint32_t attr; |
---|
220 | vpn_t base = vseg->vpn_base; |
---|
221 | vpn_t size = vseg->vpn_size; |
---|
222 | for( vpn = base ; vpn < (base+size) ; vpn++ ) |
---|
223 | { |
---|
224 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); |
---|
225 | if( attr & GPT_MAPPED ) |
---|
226 | { |
---|
227 | printk(" . vpn = %X / attr = %X / ppn = %X\n", vpn , attr , ppn ); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | // release the lock |
---|
234 | remote_rwlock_rd_unlock( XPTR( local_cxy , &vmm->vsegs_lock ) ); |
---|
235 | |
---|
236 | } // vmm_display() |
---|
237 | |
---|
238 | /////////////////////i//////////////////// |
---|
239 | void vmm_update_pte( process_t * process, |
---|
240 | vpn_t vpn, |
---|
241 | uint32_t attr, |
---|
242 | ppn_t ppn ) |
---|
243 | { |
---|
244 | |
---|
245 | xlist_entry_t * process_root_ptr; |
---|
246 | xptr_t process_root_xp; |
---|
247 | xptr_t process_iter_xp; |
---|
248 | |
---|
249 | xptr_t remote_process_xp; |
---|
250 | cxy_t remote_process_cxy; |
---|
251 | process_t * remote_process_ptr; |
---|
252 | xptr_t remote_gpt_xp; |
---|
253 | |
---|
254 | pid_t pid; |
---|
255 | cxy_t owner_cxy; |
---|
256 | lpid_t owner_lpid; |
---|
257 | |
---|
258 | // get extended pointer on root of process copies xlist in owner cluster |
---|
259 | pid = process->pid; |
---|
260 | owner_cxy = CXY_FROM_PID( pid ); |
---|
261 | owner_lpid = LPID_FROM_PID( pid ); |
---|
262 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; |
---|
263 | process_root_xp = XPTR( owner_cxy , process_root_ptr ); |
---|
264 | |
---|
265 | // loop on destination process copies |
---|
266 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
267 | { |
---|
268 | // get cluster and local pointer on remote process |
---|
269 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
270 | remote_process_ptr = (process_t *)GET_PTR( remote_process_xp ); |
---|
271 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
272 | |
---|
273 | // get extended pointer on remote gpt |
---|
274 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); |
---|
275 | |
---|
276 | hal_gpt_update_pte( remote_gpt_xp, |
---|
277 | vpn, |
---|
278 | attr, |
---|
279 | ppn ); |
---|
280 | } |
---|
281 | } // end vmm_update_pte() |
---|
282 | |
---|
283 | /////////////////////////////////////// |
---|
284 | void vmm_set_cow( process_t * process ) |
---|
285 | { |
---|
286 | vmm_t * vmm; |
---|
287 | |
---|
288 | xlist_entry_t * process_root_ptr; |
---|
289 | xptr_t process_root_xp; |
---|
290 | xptr_t process_iter_xp; |
---|
291 | |
---|
292 | xptr_t remote_process_xp; |
---|
293 | cxy_t remote_process_cxy; |
---|
294 | process_t * remote_process_ptr; |
---|
295 | xptr_t remote_gpt_xp; |
---|
296 | |
---|
297 | xptr_t vseg_root_xp; |
---|
298 | xptr_t vseg_iter_xp; |
---|
299 | |
---|
300 | xptr_t vseg_xp; |
---|
301 | vseg_t * vseg; |
---|
302 | |
---|
303 | pid_t pid; |
---|
304 | cxy_t owner_cxy; |
---|
305 | lpid_t owner_lpid; |
---|
306 | |
---|
307 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enters for process %x\n", |
---|
308 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , process->pid ); |
---|
309 | |
---|
310 | // check cluster is reference |
---|
311 | assert( (GET_CXY( process->ref_xp ) == local_cxy) , __FUNCTION__, |
---|
312 | "local cluster is not process reference cluster\n"); |
---|
313 | |
---|
314 | // get pointer on reference VMM |
---|
315 | vmm = &process->vmm; |
---|
316 | |
---|
317 | // get extended pointer on root of process copies xlist in owner cluster |
---|
318 | pid = process->pid; |
---|
319 | owner_cxy = CXY_FROM_PID( pid ); |
---|
320 | owner_lpid = LPID_FROM_PID( pid ); |
---|
321 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; |
---|
322 | process_root_xp = XPTR( owner_cxy , process_root_ptr ); |
---|
323 | |
---|
324 | // get extended pointer on root of vsegs xlist from reference VMM |
---|
325 | vseg_root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
326 | |
---|
327 | // loop on destination process copies |
---|
328 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
329 | { |
---|
330 | // get cluster and local pointer on remote process |
---|
331 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
332 | remote_process_ptr = (process_t *)GET_PTR( remote_process_xp ); |
---|
333 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
334 | |
---|
335 | vmm_dmsg("\n[DBG] %s : core[%x,%d] handling process %x in cluster %x\n", |
---|
336 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , process->pid , remote_process_cxy ); |
---|
337 | |
---|
338 | // get extended pointer on remote gpt |
---|
339 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); |
---|
340 | |
---|
341 | // loop on vsegs in (local) reference process VSL |
---|
342 | XLIST_FOREACH( vseg_root_xp , vseg_iter_xp ) |
---|
343 | { |
---|
344 | // get pointer on vseg |
---|
345 | vseg_xp = XLIST_ELEMENT( vseg_iter_xp , vseg_t , xlist ); |
---|
346 | vseg = (vseg_t *)GET_PTR( vseg_xp ); |
---|
347 | |
---|
348 | assert( (GET_CXY( vseg_xp ) == local_cxy) , __FUNCTION__, |
---|
349 | "all vsegs in reference VSL must be local\n" ); |
---|
350 | |
---|
351 | // get vseg type, base and size |
---|
352 | uint32_t type = vseg->type; |
---|
353 | vpn_t vpn_base = vseg->vpn_base; |
---|
354 | vpn_t vpn_size = vseg->vpn_size; |
---|
355 | |
---|
356 | vmm_dmsg("\n[DBG] %s : core[%x,%d] handling vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
357 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vseg_type_str(type), vpn_base, vpn_size ); |
---|
358 | |
---|
359 | // set COW flag on the remote GPT depending on vseg type |
---|
360 | if( (type == VSEG_TYPE_DATA) || |
---|
361 | (type == VSEG_TYPE_ANON) || |
---|
362 | (type == VSEG_TYPE_REMOTE) ) |
---|
363 | { |
---|
364 | hal_gpt_flip_cow( true, // set_cow |
---|
365 | remote_gpt_xp, |
---|
366 | vpn_base, |
---|
367 | vpn_size ); |
---|
368 | } |
---|
369 | } // en loop on vsegs |
---|
370 | } // end loop on process copies |
---|
371 | |
---|
372 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x\n", |
---|
373 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , process->pid ); |
---|
374 | |
---|
375 | } // end vmm_set-cow() |
---|
376 | |
---|
377 | ///////////////////////////////////////////////// |
---|
378 | error_t vmm_fork_copy( process_t * child_process, |
---|
379 | xptr_t parent_process_xp ) |
---|
380 | { |
---|
381 | error_t error; |
---|
382 | cxy_t parent_cxy; |
---|
383 | process_t * parent_process; |
---|
384 | vmm_t * parent_vmm; |
---|
385 | xptr_t parent_lock_xp; |
---|
386 | vmm_t * child_vmm; |
---|
387 | xptr_t iter_xp; |
---|
388 | xptr_t parent_vseg_xp; |
---|
389 | vseg_t * parent_vseg; |
---|
390 | vseg_t * child_vseg; |
---|
391 | uint32_t type; |
---|
392 | bool_t cow; |
---|
393 | vpn_t vpn; |
---|
394 | vpn_t vpn_base; |
---|
395 | vpn_t vpn_size; |
---|
396 | xptr_t page_xp; |
---|
397 | page_t * page_ptr; |
---|
398 | cxy_t page_cxy; |
---|
399 | xptr_t parent_root_xp; |
---|
400 | bool_t mapped; |
---|
401 | ppn_t ppn; |
---|
402 | |
---|
403 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter\n", |
---|
404 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid ); |
---|
405 | |
---|
406 | // get parent process cluster and local pointer |
---|
407 | parent_cxy = GET_CXY( parent_process_xp ); |
---|
408 | parent_process = (process_t *)GET_PTR( parent_process_xp ); |
---|
409 | |
---|
410 | // get local pointers on parent and child VMM |
---|
411 | parent_vmm = &parent_process->vmm; |
---|
412 | child_vmm = &child_process->vmm; |
---|
413 | |
---|
414 | // get extended pointer on lock protecting the parent VSL |
---|
415 | parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsegs_lock ); |
---|
416 | |
---|
417 | // initialize the lock protecting the child VSL |
---|
418 | remote_rwlock_init( XPTR( local_cxy , &child_vmm->vsegs_lock ) ); |
---|
419 | |
---|
420 | // initialize the child VSL as empty |
---|
421 | xlist_root_init( XPTR( local_cxy, &child_vmm->vsegs_root ) ); |
---|
422 | child_vmm->vsegs_nr = 0; |
---|
423 | |
---|
424 | // create child GPT |
---|
425 | error = hal_gpt_create( &child_vmm->gpt ); |
---|
426 | |
---|
427 | if( error ) |
---|
428 | { |
---|
429 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); |
---|
430 | return -1; |
---|
431 | } |
---|
432 | |
---|
433 | // build extended pointer on parent VSL |
---|
434 | parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root ); |
---|
435 | |
---|
436 | // take the lock protecting the parent VSL |
---|
437 | remote_rwlock_rd_lock( parent_lock_xp ); |
---|
438 | |
---|
439 | // loop on parent VSL xlist |
---|
440 | XLIST_FOREACH( parent_root_xp , iter_xp ) |
---|
441 | { |
---|
442 | // get local and extended pointers on current parent vseg |
---|
443 | parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
444 | parent_vseg = (vseg_t *)GET_PTR( parent_vseg_xp ); |
---|
445 | |
---|
446 | // get vseg type |
---|
447 | type = hal_remote_lw( XPTR( parent_cxy , &parent_vseg->type ) ); |
---|
448 | |
---|
449 | |
---|
450 | vmm_dmsg("\n[DBG] %s : core[%x,%d] found parent vseg %s / vpn_base = %x\n", |
---|
451 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vseg_type_str(type), |
---|
452 | hal_remote_lw( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) ); |
---|
453 | |
---|
454 | // all parent vsegs - but STACK - must be copied in child VSL |
---|
455 | if( type != VSEG_TYPE_STACK ) |
---|
456 | { |
---|
457 | // allocate memory for a new child vseg |
---|
458 | child_vseg = vseg_alloc(); |
---|
459 | if( child_vseg == NULL ) // release all allocated vsegs |
---|
460 | { |
---|
461 | vmm_destroy( child_process ); |
---|
462 | printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ ); |
---|
463 | return -1; |
---|
464 | } |
---|
465 | |
---|
466 | // copy parent vseg to child vseg |
---|
467 | vseg_init_from_ref( child_vseg , parent_vseg_xp ); |
---|
468 | |
---|
469 | // register child vseg in child VSL |
---|
470 | vseg_attach( child_vmm , child_vseg ); |
---|
471 | |
---|
472 | vmm_dmsg("\n[DBG] %s : core[%x,%d] copied to child VSL : vseg %s / vpn_base = %x\n", |
---|
473 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vseg_type_str(type), |
---|
474 | hal_remote_lw( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) ); |
---|
475 | |
---|
476 | // copy DATA, MMAP, REMOTE, FILE parent GPT entries to child GPT |
---|
477 | if( type != VSEG_TYPE_CODE ) |
---|
478 | { |
---|
479 | // activate the COW for DATA, MMAP, REMOTE vsegs only |
---|
480 | cow = ( type != VSEG_TYPE_FILE ); |
---|
481 | |
---|
482 | vpn_base = child_vseg->vpn_base; |
---|
483 | vpn_size = child_vseg->vpn_size; |
---|
484 | |
---|
485 | // scan pages in parent vseg |
---|
486 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) |
---|
487 | { |
---|
488 | error = hal_gpt_pte_copy( &child_vmm->gpt, |
---|
489 | XPTR( parent_cxy , &parent_vmm->gpt ), |
---|
490 | vpn, |
---|
491 | cow, |
---|
492 | &ppn, |
---|
493 | &mapped ); |
---|
494 | if( error ) |
---|
495 | { |
---|
496 | vmm_destroy( child_process ); |
---|
497 | printk("\n[ERROR] in %s : cannot copy GPT\n", __FUNCTION__ ); |
---|
498 | return -1; |
---|
499 | } |
---|
500 | |
---|
501 | // increment page descriptor fork_nr for the referenced page if mapped |
---|
502 | if( mapped ) |
---|
503 | { |
---|
504 | page_xp = ppm_ppn2page( ppn ); |
---|
505 | page_cxy = GET_CXY( page_xp ); |
---|
506 | page_ptr = (page_t *)GET_PTR( page_xp ); |
---|
507 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->fork_nr ) , 1 ); |
---|
508 | |
---|
509 | vmm_dmsg("\n[DBG] %s : core[%x,%d] copied to child GPT : vpn %x\n", |
---|
510 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
511 | |
---|
512 | } |
---|
513 | } |
---|
514 | } // end if no code & no stack |
---|
515 | } // end if no stack |
---|
516 | } // end loop on vsegs |
---|
517 | |
---|
518 | // release the parent vsegs lock |
---|
519 | remote_rwlock_rd_unlock( parent_lock_xp ); |
---|
520 | |
---|
521 | // initialize child GPT (architecture specic) |
---|
522 | // => For TSAR, identity map the kentry_vseg |
---|
523 | error = hal_vmm_init( child_vmm ); |
---|
524 | |
---|
525 | if( error ) |
---|
526 | { |
---|
527 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); |
---|
528 | return -1; |
---|
529 | } |
---|
530 | |
---|
531 | // initialize the child VMM STACK allocator |
---|
532 | child_vmm->stack_mgr.bitmap = 0; |
---|
533 | child_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
534 | |
---|
535 | // initialize the child VMM MMAP allocator |
---|
536 | uint32_t i; |
---|
537 | child_vmm->mmap_mgr.vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
538 | child_vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; |
---|
539 | child_vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_HEAP_BASE; |
---|
540 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &child_vmm->mmap_mgr.zombi_list[i] ); |
---|
541 | |
---|
542 | // initialize instrumentation counters |
---|
543 | child_vmm->pgfault_nr = 0; |
---|
544 | |
---|
545 | // copy base addresses from parent VMM to child VMM |
---|
546 | child_vmm->kent_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->kent_vpn_base)); |
---|
547 | child_vmm->args_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->args_vpn_base)); |
---|
548 | child_vmm->envs_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->envs_vpn_base)); |
---|
549 | child_vmm->heap_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->heap_vpn_base)); |
---|
550 | child_vmm->code_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->code_vpn_base)); |
---|
551 | child_vmm->data_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->data_vpn_base)); |
---|
552 | |
---|
553 | child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point)); |
---|
554 | |
---|
555 | hal_fence(); |
---|
556 | |
---|
557 | return 0; |
---|
558 | |
---|
559 | } // vmm_fork_copy() |
---|
560 | |
---|
561 | /////////////////////////////////////// |
---|
562 | void vmm_destroy( process_t * process ) |
---|
563 | { |
---|
564 | xptr_t vseg_xp; |
---|
565 | vseg_t * vseg; |
---|
566 | |
---|
567 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter\n", |
---|
568 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid ); |
---|
569 | |
---|
570 | // get pointer on VMM |
---|
571 | vmm_t * vmm = &process->vmm; |
---|
572 | |
---|
573 | // get extended pointer on VSL root and VSL lock |
---|
574 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
575 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); |
---|
576 | |
---|
577 | // get lock protecting vseg list |
---|
578 | remote_rwlock_wr_lock( lock_xp ); |
---|
579 | |
---|
580 | // remove all user vsegs registered in VSL |
---|
581 | while( !xlist_is_empty( root_xp ) ) |
---|
582 | { |
---|
583 | // get pointer on first vseg in VSL |
---|
584 | vseg_xp = XLIST_FIRST_ELEMENT( root_xp , vseg_t , xlist ); |
---|
585 | vseg = (vseg_t *)GET_PTR( vseg_xp ); |
---|
586 | |
---|
587 | // unmap and release all pages |
---|
588 | vmm_unmap_vseg( process , vseg ); |
---|
589 | |
---|
590 | // remove vseg from VSL |
---|
591 | vseg_detach( vmm , vseg ); |
---|
592 | |
---|
593 | // release memory allocated to vseg descriptor |
---|
594 | vseg_free( vseg ); |
---|
595 | } |
---|
596 | |
---|
597 | // release lock |
---|
598 | remote_rwlock_wr_unlock( lock_xp ); |
---|
599 | |
---|
600 | // remove all vsegs from zombi_lists in MMAP allocator |
---|
601 | uint32_t i; |
---|
602 | for( i = 0 ; i<32 ; i++ ) |
---|
603 | { |
---|
604 | while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) ) |
---|
605 | { |
---|
606 | vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , zlist ); |
---|
607 | vseg_detach( vmm , vseg ); |
---|
608 | vseg_free( vseg ); |
---|
609 | } |
---|
610 | } |
---|
611 | |
---|
612 | // release memory allocated to the GPT itself |
---|
613 | hal_gpt_destroy( &vmm->gpt ); |
---|
614 | |
---|
615 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit\n", |
---|
616 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid ); |
---|
617 | |
---|
618 | } // end vmm_destroy() |
---|
619 | |
---|
620 | ///////////////////////////////////////////////// |
---|
621 | vseg_t * vmm_check_conflict( process_t * process, |
---|
622 | vpn_t vpn_base, |
---|
623 | vpn_t vpn_size ) |
---|
624 | { |
---|
625 | vmm_t * vmm = &process->vmm; |
---|
626 | |
---|
627 | // scan the VSL |
---|
628 | vseg_t * vseg; |
---|
629 | xptr_t iter_xp; |
---|
630 | xptr_t vseg_xp; |
---|
631 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
632 | |
---|
633 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
634 | { |
---|
635 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
636 | vseg = (vseg_t *)GET_PTR( vseg_xp ); |
---|
637 | |
---|
638 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && |
---|
639 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; |
---|
640 | } |
---|
641 | return NULL; |
---|
642 | |
---|
643 | } // end vmm_check_conflict() |
---|
644 | |
---|
645 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
646 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
647 | // the VMM stack_vseg specific allocator. |
---|
648 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
649 | // @ vmm : pointer on VMM. |
---|
650 | // @ vpn_base : (return value) first allocated page |
---|
651 | // @ vpn_size : (return value) number of allocated pages |
---|
652 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
653 | static error_t vmm_stack_alloc( vmm_t * vmm, |
---|
654 | vpn_t * vpn_base, |
---|
655 | vpn_t * vpn_size ) |
---|
656 | { |
---|
657 | // get stack allocator pointer |
---|
658 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
659 | |
---|
660 | // get lock on stack allocator |
---|
661 | spinlock_lock( &mgr->lock ); |
---|
662 | |
---|
663 | // get first free slot index in bitmap |
---|
664 | int32_t index = bitmap_ffc( &mgr->bitmap , 4 ); |
---|
665 | if( (index < 0) || (index > 31) ) |
---|
666 | { |
---|
667 | spinlock_unlock( &mgr->lock ); |
---|
668 | return ENOMEM; |
---|
669 | } |
---|
670 | |
---|
671 | // update bitmap |
---|
672 | bitmap_set( &mgr->bitmap , index ); |
---|
673 | |
---|
674 | // release lock on stack allocator |
---|
675 | spinlock_unlock( &mgr->lock ); |
---|
676 | |
---|
677 | // returns vpn_base, vpn_size (one page non allocated) |
---|
678 | *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1; |
---|
679 | *vpn_size = CONFIG_VMM_STACK_SIZE - 1; |
---|
680 | return 0; |
---|
681 | |
---|
682 | } // end vmm_stack_alloc() |
---|
683 | |
---|
684 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
685 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
686 | // the VMM MMAP specific allocator. |
---|
687 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
688 | // @ vmm : [in] pointer on VMM. |
---|
689 | // @ npages : [in] requested number of pages. |
---|
690 | // @ vpn_base : [out] first allocated page. |
---|
691 | // @ vpn_size : [out] actual number of allocated pages. |
---|
692 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
693 | static error_t vmm_mmap_alloc( vmm_t * vmm, |
---|
694 | vpn_t npages, |
---|
695 | vpn_t * vpn_base, |
---|
696 | vpn_t * vpn_size ) |
---|
697 | { |
---|
698 | uint32_t index; |
---|
699 | vseg_t * vseg; |
---|
700 | vpn_t base; |
---|
701 | vpn_t size; |
---|
702 | vpn_t free; |
---|
703 | |
---|
704 | // mmap vseg size must be power of 2 |
---|
705 | // compute actual size and index in zombi_list array |
---|
706 | size = POW2_ROUNDUP( npages ); |
---|
707 | index = bits_log2( size ); |
---|
708 | |
---|
709 | // get mmap allocator pointer |
---|
710 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
711 | |
---|
712 | // get lock on mmap allocator |
---|
713 | spinlock_lock( &mgr->lock ); |
---|
714 | |
---|
715 | // get vseg from zombi_list or from mmap zone |
---|
716 | if( list_is_empty( &mgr->zombi_list[index] ) ) // from mmap zone |
---|
717 | { |
---|
718 | // check overflow |
---|
719 | free = mgr->first_free_vpn; |
---|
720 | if( (free + size) > mgr->vpn_size ) return ENOMEM; |
---|
721 | |
---|
722 | // update STACK allocator |
---|
723 | mgr->first_free_vpn += size; |
---|
724 | |
---|
725 | // compute base |
---|
726 | base = free; |
---|
727 | } |
---|
728 | else // from zombi_list |
---|
729 | { |
---|
730 | // get pointer on zombi vseg from zombi_list |
---|
731 | vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , zlist ); |
---|
732 | |
---|
733 | // remove vseg from free-list |
---|
734 | list_unlink( &vseg->zlist ); |
---|
735 | |
---|
736 | // compute base |
---|
737 | base = vseg->vpn_base; |
---|
738 | } |
---|
739 | |
---|
740 | // release lock on mmap allocator |
---|
741 | spinlock_unlock( &mgr->lock ); |
---|
742 | |
---|
743 | // returns vpn_base, vpn_size |
---|
744 | *vpn_base = base; |
---|
745 | *vpn_size = size; |
---|
746 | return 0; |
---|
747 | |
---|
748 | } // end vmm_mmap_alloc() |
---|
749 | |
---|
750 | //////////////////////////////////////////////// |
---|
751 | vseg_t * vmm_create_vseg( process_t * process, |
---|
752 | vseg_type_t type, |
---|
753 | intptr_t base, |
---|
754 | uint32_t size, |
---|
755 | uint32_t file_offset, |
---|
756 | uint32_t file_size, |
---|
757 | xptr_t mapper_xp, |
---|
758 | cxy_t cxy ) |
---|
759 | { |
---|
760 | vseg_t * vseg; // created vseg pointer |
---|
761 | vpn_t vpn_base; // first page index |
---|
762 | vpn_t vpn_size; // number of pages |
---|
763 | error_t error; |
---|
764 | |
---|
765 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enters / process %x / base %x / size %x / %s / cxy = %x\n", |
---|
766 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
767 | process->pid , base , size , vseg_type_str(type) , cxy ); |
---|
768 | |
---|
769 | // get pointer on VMM |
---|
770 | vmm_t * vmm = &process->vmm; |
---|
771 | |
---|
772 | // compute base, size, vpn_base, vpn_size, depending on vseg type |
---|
773 | // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs |
---|
774 | if( type == VSEG_TYPE_STACK ) |
---|
775 | { |
---|
776 | // get vpn_base and vpn_size from STACK allocator |
---|
777 | error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size ); |
---|
778 | if( error ) |
---|
779 | { |
---|
780 | printk("\n[ERROR] in %s : no space for stack vseg / process %x in cluster %x\n", |
---|
781 | __FUNCTION__ , process->pid , local_cxy ); |
---|
782 | return NULL; |
---|
783 | } |
---|
784 | |
---|
785 | // compute vseg base and size from vpn_base and vpn_size |
---|
786 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
787 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
788 | } |
---|
789 | else if( (type == VSEG_TYPE_ANON) || |
---|
790 | (type == VSEG_TYPE_FILE) || |
---|
791 | (type == VSEG_TYPE_REMOTE) ) |
---|
792 | { |
---|
793 | // get vpn_base and vpn_size from MMAP allocator |
---|
794 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
795 | error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size ); |
---|
796 | if( error ) |
---|
797 | { |
---|
798 | printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n", |
---|
799 | __FUNCTION__ , process->pid , local_cxy ); |
---|
800 | return NULL; |
---|
801 | } |
---|
802 | |
---|
803 | // compute vseg base and size from vpn_base and vpn_size |
---|
804 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
805 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
806 | } |
---|
807 | else |
---|
808 | { |
---|
809 | uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
810 | uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
811 | |
---|
812 | vpn_base = vpn_min; |
---|
813 | vpn_size = vpn_max - vpn_min + 1; |
---|
814 | } |
---|
815 | |
---|
816 | // check collisions |
---|
817 | vseg = vmm_check_conflict( process , vpn_base , vpn_size ); |
---|
818 | if( vseg != NULL ) |
---|
819 | { |
---|
820 | printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n" |
---|
821 | " overlap existing vseg [vpn_base = %x / vpn_size = %x]\n", |
---|
822 | __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size ); |
---|
823 | return NULL; |
---|
824 | } |
---|
825 | |
---|
826 | // allocate physical memory for vseg descriptor |
---|
827 | vseg = vseg_alloc(); |
---|
828 | if( vseg == NULL ) |
---|
829 | { |
---|
830 | printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n", |
---|
831 | __FUNCTION__ , process->pid ); |
---|
832 | return NULL; |
---|
833 | } |
---|
834 | |
---|
835 | // initialize vseg descriptor |
---|
836 | vseg_init( vseg, |
---|
837 | type, |
---|
838 | base, |
---|
839 | size, |
---|
840 | vpn_base, |
---|
841 | vpn_size, |
---|
842 | file_offset, |
---|
843 | file_size, |
---|
844 | mapper_xp, |
---|
845 | cxy ); |
---|
846 | |
---|
847 | // attach vseg to VSL |
---|
848 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); |
---|
849 | remote_rwlock_wr_lock( lock_xp ); |
---|
850 | vseg_attach( vmm , vseg ); |
---|
851 | remote_rwlock_wr_unlock( lock_xp ); |
---|
852 | |
---|
853 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit / process %x / base %x / size %x / type %s\n", |
---|
854 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
855 | process->pid , base , size , vseg_type_str(type) ); |
---|
856 | |
---|
857 | return vseg; |
---|
858 | |
---|
859 | } // vmm_create_vseg() |
---|
860 | |
---|
861 | ///////////////////////////////////// |
---|
862 | void vmm_remove_vseg( vseg_t * vseg ) |
---|
863 | { |
---|
864 | // get pointers on calling process and VMM |
---|
865 | thread_t * this = CURRENT_THREAD; |
---|
866 | process_t * process = this->process; |
---|
867 | vmm_t * vmm = &this->process->vmm; |
---|
868 | uint32_t type = vseg->type; |
---|
869 | |
---|
870 | // detach vseg from VSL |
---|
871 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); |
---|
872 | remote_rwlock_wr_lock( lock_xp ); |
---|
873 | vseg_detach( &process->vmm , vseg ); |
---|
874 | remote_rwlock_wr_unlock( lock_xp ); |
---|
875 | |
---|
876 | // release the stack slot to VMM stack allocator if STACK type |
---|
877 | if( type == VSEG_TYPE_STACK ) |
---|
878 | { |
---|
879 | // get pointer on stack allocator |
---|
880 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
881 | |
---|
882 | // compute slot index |
---|
883 | uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE); |
---|
884 | |
---|
885 | // update stacks_bitmap |
---|
886 | spinlock_lock( &mgr->lock ); |
---|
887 | bitmap_clear( &mgr->bitmap , index ); |
---|
888 | spinlock_unlock( &mgr->lock ); |
---|
889 | } |
---|
890 | |
---|
891 | // release the vseg to VMM mmap allocator if MMAP type |
---|
892 | if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) ) |
---|
893 | { |
---|
894 | // get pointer on mmap allocator |
---|
895 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
896 | |
---|
897 | // compute zombi_list index |
---|
898 | uint32_t index = bits_log2( vseg->vpn_size ); |
---|
899 | |
---|
900 | // update zombi_list |
---|
901 | spinlock_lock( &mgr->lock ); |
---|
902 | list_add_first( &mgr->zombi_list[index] , &vseg->zlist ); |
---|
903 | spinlock_unlock( &mgr->lock ); |
---|
904 | } |
---|
905 | |
---|
906 | // release physical memory allocated for vseg descriptor if no MMAP type |
---|
907 | if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) ) |
---|
908 | { |
---|
909 | vseg_free( vseg ); |
---|
910 | } |
---|
911 | } // end vmm_remove_vseg() |
---|
912 | |
---|
913 | ////////////////////////////////////////////// |
---|
914 | error_t vmm_map_kernel_vseg( vseg_t * vseg, |
---|
915 | uint32_t attr ) |
---|
916 | { |
---|
917 | vpn_t vpn; // VPN of PTE to be set |
---|
918 | vpn_t vpn_min; // VPN of first PTE to be set |
---|
919 | vpn_t vpn_max; // VPN of last PTE to be set (excluded) |
---|
920 | ppn_t ppn; // PPN of allocated physical page |
---|
921 | uint32_t order; // ln( number of small pages for one single PTE ) |
---|
922 | page_t * page; |
---|
923 | error_t error; |
---|
924 | |
---|
925 | // check vseg type : must be a kernel vseg |
---|
926 | uint32_t type = vseg->type; |
---|
927 | assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)), |
---|
928 | __FUNCTION__ , "not a kernel vseg\n" ); |
---|
929 | |
---|
930 | // get pointer on page table |
---|
931 | gpt_t * gpt = &process_zero.vmm.gpt; |
---|
932 | |
---|
933 | // define number of small pages per PTE |
---|
934 | if( attr & GPT_SMALL ) order = 0; // 1 small page |
---|
935 | else order = 9; // 512 small pages |
---|
936 | |
---|
937 | // loop on pages in vseg |
---|
938 | vpn_min = vseg->vpn_base; |
---|
939 | vpn_max = vpn_min + vseg->vpn_size; |
---|
940 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
941 | { |
---|
942 | // allocate a physical page from local PPM |
---|
943 | kmem_req_t req; |
---|
944 | req.type = KMEM_PAGE; |
---|
945 | req.size = order; |
---|
946 | req.flags = AF_KERNEL | AF_ZERO; |
---|
947 | page = (page_t *)kmem_alloc( &req ); |
---|
948 | if( page == NULL ) |
---|
949 | { |
---|
950 | printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ ); |
---|
951 | return ENOMEM; |
---|
952 | } |
---|
953 | |
---|
954 | // set page table entry |
---|
955 | ppn = ppm_page2ppn( XPTR( local_cxy , page ) ); |
---|
956 | error = hal_gpt_set_pte( gpt, |
---|
957 | vpn, |
---|
958 | attr, |
---|
959 | ppn ); |
---|
960 | if( error ) |
---|
961 | { |
---|
962 | printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ ); |
---|
963 | return ENOMEM; |
---|
964 | } |
---|
965 | } |
---|
966 | |
---|
967 | return 0; |
---|
968 | |
---|
969 | } // end vmm_map_kernel_vseg() |
---|
970 | |
---|
971 | ///////////////////////////////////////// |
---|
972 | void vmm_unmap_vseg( process_t * process, |
---|
973 | vseg_t * vseg ) |
---|
974 | { |
---|
975 | vpn_t vpn; // VPN of current PTE |
---|
976 | vpn_t vpn_min; // VPN of first PTE |
---|
977 | vpn_t vpn_max; // VPN of last PTE (excluded) |
---|
978 | ppn_t ppn; // current PTE ppn value |
---|
979 | uint32_t attr; // current PTE attributes |
---|
980 | kmem_req_t req; // request to release memory |
---|
981 | xptr_t page_xp; // extended pointer on page descriptor |
---|
982 | cxy_t page_cxy; // page descriptor cluster |
---|
983 | page_t * page_ptr; // page descriptor pointer |
---|
984 | |
---|
985 | vmm_dmsg("\n[DBG] %s : core[%x, %d] enter / process %x / vseg %s / base %x / cycle %d\n", |
---|
986 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid , |
---|
987 | vseg_type_str( vseg->type ), vseg->vpn_base, (uint32_t)hal_get_cycles() ); |
---|
988 | |
---|
989 | // get pointer on process GPT |
---|
990 | gpt_t * gpt = &process->vmm.gpt; |
---|
991 | |
---|
992 | // loop on pages in vseg |
---|
993 | vpn_min = vseg->vpn_base; |
---|
994 | vpn_max = vpn_min + vseg->vpn_size; |
---|
995 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
996 | { |
---|
997 | // get GPT entry |
---|
998 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); |
---|
999 | |
---|
1000 | if( attr & GPT_MAPPED ) // entry is mapped |
---|
1001 | { |
---|
1002 | // check small page |
---|
1003 | assert( (attr & GPT_SMALL) , __FUNCTION__ , |
---|
1004 | "an user vseg must use small pages" ); |
---|
1005 | |
---|
1006 | // unmap GPT entry |
---|
1007 | hal_gpt_reset_pte( gpt , vpn ); |
---|
1008 | |
---|
1009 | // release memory if not identity mapped |
---|
1010 | if( (vseg->flags & VSEG_IDENT) == 0 ) |
---|
1011 | { |
---|
1012 | // get extended pointer on page descriptor |
---|
1013 | page_xp = ppm_ppn2page( ppn ); |
---|
1014 | page_cxy = GET_CXY( page_xp ); |
---|
1015 | page_ptr = (page_t *)GET_PTR( page_xp ); |
---|
1016 | |
---|
1017 | // release physical page to relevant cluster |
---|
1018 | if( page_cxy == local_cxy ) // local cluster |
---|
1019 | { |
---|
1020 | req.type = KMEM_PAGE; |
---|
1021 | req.ptr = page_ptr; |
---|
1022 | kmem_free( &req ); |
---|
1023 | } |
---|
1024 | else // remote cluster |
---|
1025 | { |
---|
1026 | rpc_pmem_release_pages_client( page_cxy , page_ptr ); |
---|
1027 | } |
---|
1028 | } |
---|
1029 | } |
---|
1030 | } |
---|
1031 | } // end vmm_unmap_vseg() |
---|
1032 | |
---|
1033 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1034 | // This low-level static function is called by the vmm_get_vseg() and vmm_resize_vseg() |
---|
1035 | // functions. It scan the list of registered vsegs to find the unique vseg containing |
---|
1036 | // a given virtual address. |
---|
1037 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1038 | // @ vmm : pointer on the process VMM. |
---|
1039 | // @ vaddr : virtual address. |
---|
1040 | // @ return vseg pointer if success / return NULL if not found. |
---|
1041 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1042 | static vseg_t * vseg_from_vaddr( vmm_t * vmm, |
---|
1043 | intptr_t vaddr ) |
---|
1044 | { |
---|
1045 | xptr_t iter_xp; |
---|
1046 | xptr_t vseg_xp; |
---|
1047 | vseg_t * vseg; |
---|
1048 | |
---|
1049 | // get extended pointers on VSL lock and root |
---|
1050 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); |
---|
1051 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
1052 | |
---|
1053 | // get lock protecting the VSL |
---|
1054 | remote_rwlock_rd_lock( lock_xp ); |
---|
1055 | |
---|
1056 | // scan the list of vsegs in VSL |
---|
1057 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
1058 | { |
---|
1059 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
1060 | vseg = (vseg_t *)GET_PTR( vseg_xp ); |
---|
1061 | if( (vaddr >= vseg->min) && (vaddr < vseg->max) ) |
---|
1062 | { |
---|
1063 | // return success |
---|
1064 | remote_rwlock_rd_unlock( lock_xp ); |
---|
1065 | return vseg; |
---|
1066 | } |
---|
1067 | } |
---|
1068 | |
---|
1069 | // return failure |
---|
1070 | remote_rwlock_rd_unlock( lock_xp ); |
---|
1071 | return NULL; |
---|
1072 | |
---|
1073 | } // end vseg_from_vaddr() |
---|
1074 | |
---|
1075 | ///////////////////////////////////////////// |
---|
1076 | error_t vmm_resize_vseg( process_t * process, |
---|
1077 | intptr_t base, |
---|
1078 | intptr_t size ) |
---|
1079 | { |
---|
1080 | error_t error; |
---|
1081 | vseg_t * new; |
---|
1082 | vpn_t vpn_min; |
---|
1083 | vpn_t vpn_max; |
---|
1084 | |
---|
1085 | // get pointer on process VMM |
---|
1086 | vmm_t * vmm = &process->vmm; |
---|
1087 | |
---|
1088 | intptr_t addr_min = base; |
---|
1089 | intptr_t addr_max = base + size; |
---|
1090 | |
---|
1091 | // get pointer on vseg |
---|
1092 | vseg_t * vseg = vseg_from_vaddr( vmm , base ); |
---|
1093 | |
---|
1094 | if( vseg == NULL) return EINVAL; |
---|
1095 | |
---|
1096 | // get extended pointer on VSL lock |
---|
1097 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); |
---|
1098 | |
---|
1099 | // get lock protecting VSL |
---|
1100 | remote_rwlock_wr_lock( lock_xp ); |
---|
1101 | |
---|
1102 | if( (vseg->min > addr_min) || (vseg->max < addr_max) ) // region not included in vseg |
---|
1103 | { |
---|
1104 | error = EINVAL; |
---|
1105 | } |
---|
1106 | else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed |
---|
1107 | { |
---|
1108 | vmm_remove_vseg( vseg ); |
---|
1109 | error = 0; |
---|
1110 | } |
---|
1111 | else if( vseg->min == addr_min ) // vseg must be resized |
---|
1112 | { |
---|
1113 | // update vseg base address |
---|
1114 | vseg->min = addr_max; |
---|
1115 | |
---|
1116 | // update vpn_base and vpn_size |
---|
1117 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
1118 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
1119 | vseg->vpn_base = vpn_min; |
---|
1120 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
1121 | error = 0; |
---|
1122 | } |
---|
1123 | else if( vseg->max == addr_max ) // vseg must be resized |
---|
1124 | { |
---|
1125 | // update vseg max address |
---|
1126 | vseg->max = addr_min; |
---|
1127 | |
---|
1128 | // update vpn_base and vpn_size |
---|
1129 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
1130 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
1131 | vseg->vpn_base = vpn_min; |
---|
1132 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
1133 | error = 0; |
---|
1134 | } |
---|
1135 | else // vseg cut in three regions |
---|
1136 | { |
---|
1137 | // resize existing vseg |
---|
1138 | vseg->max = addr_min; |
---|
1139 | |
---|
1140 | // update vpn_base and vpn_size |
---|
1141 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
1142 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
1143 | vseg->vpn_base = vpn_min; |
---|
1144 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
1145 | |
---|
1146 | // create new vseg |
---|
1147 | new = vmm_create_vseg( process, |
---|
1148 | vseg->type, |
---|
1149 | addr_min, |
---|
1150 | (vseg->max - addr_max), |
---|
1151 | vseg->file_offset, |
---|
1152 | vseg->file_size, |
---|
1153 | vseg->mapper_xp, |
---|
1154 | vseg->cxy ); |
---|
1155 | |
---|
1156 | if( new == NULL ) error = EINVAL; |
---|
1157 | else error = 0; |
---|
1158 | } |
---|
1159 | |
---|
1160 | // release VMM lock |
---|
1161 | remote_rwlock_wr_unlock( lock_xp ); |
---|
1162 | |
---|
1163 | return error; |
---|
1164 | |
---|
1165 | } // vmm_resize_vseg() |
---|
1166 | |
---|
1167 | /////////////////////////////////////////// |
---|
1168 | error_t vmm_get_vseg( process_t * process, |
---|
1169 | intptr_t vaddr, |
---|
1170 | vseg_t ** found_vseg ) |
---|
1171 | { |
---|
1172 | vmm_t * vmm = &process->vmm; |
---|
1173 | |
---|
1174 | // get vseg from vaddr |
---|
1175 | vseg_t * vseg = vseg_from_vaddr( vmm , vaddr ); |
---|
1176 | |
---|
1177 | if( vseg == NULL ) // vseg not found in local cluster => try to get it from ref |
---|
1178 | { |
---|
1179 | // get extended pointer on reference process |
---|
1180 | xptr_t ref_xp = process->ref_xp; |
---|
1181 | |
---|
1182 | // get cluster and local pointer on reference process |
---|
1183 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
1184 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
1185 | |
---|
1186 | if( local_cxy == ref_cxy ) return -1; // local cluster is the reference |
---|
1187 | |
---|
1188 | // get extended pointer on reference vseg |
---|
1189 | xptr_t vseg_xp; |
---|
1190 | error_t error; |
---|
1191 | |
---|
1192 | rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error ); |
---|
1193 | |
---|
1194 | if( error ) return -1; // vseg not found => illegal user vaddr |
---|
1195 | |
---|
1196 | // allocate a vseg in local cluster |
---|
1197 | vseg = vseg_alloc(); |
---|
1198 | |
---|
1199 | if( vseg == NULL ) return -1; |
---|
1200 | |
---|
1201 | // initialise local vseg from reference |
---|
1202 | vseg_init_from_ref( vseg , vseg_xp ); |
---|
1203 | |
---|
1204 | // register local vseg in local VMM |
---|
1205 | vseg_attach( &process->vmm , vseg ); |
---|
1206 | } |
---|
1207 | |
---|
1208 | // success |
---|
1209 | *found_vseg = vseg; |
---|
1210 | return 0; |
---|
1211 | |
---|
1212 | } // end vmm_get_vseg() |
---|
1213 | |
---|
1214 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
1215 | // This static function compute the target cluster to allocate a physical page |
---|
1216 | // for a given <vpn> in a given <vseg>, allocates the page (with an RPC if required) |
---|
1217 | // and returns an extended pointer on the allocated page descriptor. |
---|
1218 | // The vseg cannot have the FILE type. |
---|
1219 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
1220 | static xptr_t vmm_page_allocate( vseg_t * vseg, |
---|
1221 | vpn_t vpn ) |
---|
1222 | { |
---|
1223 | // compute target cluster |
---|
1224 | page_t * page_ptr; |
---|
1225 | cxy_t page_cxy; |
---|
1226 | kmem_req_t req; |
---|
1227 | |
---|
1228 | uint32_t type = vseg->type; |
---|
1229 | uint32_t flags = vseg->flags; |
---|
1230 | |
---|
1231 | assert( ( type != VSEG_TYPE_FILE ) , __FUNCTION__ , "illegal vseg type\n" ); |
---|
1232 | |
---|
1233 | if( flags & VSEG_DISTRIB ) // distributed => cxy depends on vpn LSB |
---|
1234 | { |
---|
1235 | uint32_t x_size = LOCAL_CLUSTER->x_size; |
---|
1236 | uint32_t y_size = LOCAL_CLUSTER->y_size; |
---|
1237 | uint32_t y_width = LOCAL_CLUSTER->y_width; |
---|
1238 | uint32_t index = vpn & ((x_size * y_size) - 1); |
---|
1239 | uint32_t x = index / y_size; |
---|
1240 | uint32_t y = index % y_size; |
---|
1241 | page_cxy = (x<<y_width) + y; |
---|
1242 | } |
---|
1243 | else // other cases => cxy specified in vseg |
---|
1244 | { |
---|
1245 | page_cxy = vseg->cxy; |
---|
1246 | } |
---|
1247 | |
---|
1248 | // allocate a physical page from target cluster |
---|
1249 | if( page_cxy == local_cxy ) // target cluster is the local cluster |
---|
1250 | { |
---|
1251 | req.type = KMEM_PAGE; |
---|
1252 | req.size = 0; |
---|
1253 | req.flags = AF_NONE; |
---|
1254 | page_ptr = (page_t *)kmem_alloc( &req ); |
---|
1255 | } |
---|
1256 | else // target cluster is not the local cluster |
---|
1257 | { |
---|
1258 | rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr ); |
---|
1259 | } |
---|
1260 | |
---|
1261 | if( page_ptr == NULL ) return XPTR_NULL; |
---|
1262 | else return XPTR( page_cxy , page_ptr ); |
---|
1263 | |
---|
1264 | } // end vmm_page_allocate() |
---|
1265 | |
---|
1266 | //////////////////////////////////////// |
---|
1267 | error_t vmm_get_one_ppn( vseg_t * vseg, |
---|
1268 | vpn_t vpn, |
---|
1269 | ppn_t * ppn ) |
---|
1270 | { |
---|
1271 | error_t error; |
---|
1272 | xptr_t page_xp; // extended pointer on physical page descriptor |
---|
1273 | page_t * page_ptr; // local pointer on physical page descriptor |
---|
1274 | uint32_t index; // missing page index in vseg mapper |
---|
1275 | uint32_t type; // vseg type; |
---|
1276 | |
---|
1277 | type = vseg->type; |
---|
1278 | index = vpn - vseg->vpn_base; |
---|
1279 | |
---|
1280 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter for vpn = %x / type = %s / index = %d\n", |
---|
1281 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, vseg_type_str(type), index ); |
---|
1282 | |
---|
1283 | // FILE type : get the physical page from the file mapper |
---|
1284 | if( type == VSEG_TYPE_FILE ) |
---|
1285 | { |
---|
1286 | // get extended pointer on mapper |
---|
1287 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
1288 | |
---|
1289 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, |
---|
1290 | "mapper not defined for a FILE vseg\n" ); |
---|
1291 | |
---|
1292 | // get mapper cluster and local pointer |
---|
1293 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
1294 | mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); |
---|
1295 | |
---|
1296 | // get page descriptor from mapper |
---|
1297 | if( mapper_cxy == local_cxy ) // mapper is local |
---|
1298 | { |
---|
1299 | page_ptr = mapper_get_page( mapper_ptr , index ); |
---|
1300 | } |
---|
1301 | else // mapper is remote |
---|
1302 | { |
---|
1303 | rpc_mapper_get_page_client( mapper_cxy , mapper_ptr , index , &page_ptr ); |
---|
1304 | } |
---|
1305 | |
---|
1306 | if ( page_ptr == NULL ) return EINVAL; |
---|
1307 | |
---|
1308 | page_xp = XPTR( mapper_cxy , page_ptr ); |
---|
1309 | } |
---|
1310 | |
---|
1311 | // Other types : allocate a physical page from target cluster, |
---|
1312 | // as defined by vseg type and vpn value |
---|
1313 | else |
---|
1314 | { |
---|
1315 | // allocate physical page |
---|
1316 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
1317 | |
---|
1318 | if( page_xp == XPTR_NULL ) return ENOMEM; |
---|
1319 | |
---|
1320 | // initialise missing page from .elf file mapper for DATA and CODE types |
---|
1321 | // => the mapper_xp field is an extended pointer on the .elf file mapper |
---|
1322 | if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) |
---|
1323 | { |
---|
1324 | // get extended pointer on mapper |
---|
1325 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
1326 | |
---|
1327 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, |
---|
1328 | "mapper not defined for a CODE or DATA vseg\n" ); |
---|
1329 | |
---|
1330 | // get mapper cluster and local pointer |
---|
1331 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
1332 | mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); |
---|
1333 | |
---|
1334 | // compute missing page offset in vseg |
---|
1335 | uint32_t offset = index << CONFIG_PPM_PAGE_SHIFT; |
---|
1336 | |
---|
1337 | // compute missing page offset in .elf file |
---|
1338 | uint32_t elf_offset = vseg->file_offset + offset; |
---|
1339 | |
---|
1340 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / elf_offset = %x\n", |
---|
1341 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, elf_offset ); |
---|
1342 | |
---|
1343 | // compute extended pointer on page base |
---|
1344 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
1345 | |
---|
1346 | // file_size (in .elf mapper) can be smaller than vseg_size (BSS) |
---|
1347 | uint32_t file_size = vseg->file_size; |
---|
1348 | |
---|
1349 | if( file_size < offset ) // missing page fully in BSS |
---|
1350 | { |
---|
1351 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / fully in BSS\n", |
---|
1352 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); |
---|
1353 | |
---|
1354 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
1355 | { |
---|
1356 | memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
1357 | } |
---|
1358 | else |
---|
1359 | { |
---|
1360 | hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
1361 | } |
---|
1362 | } |
---|
1363 | else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper |
---|
1364 | { |
---|
1365 | |
---|
1366 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / fully in mapper\n", |
---|
1367 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); |
---|
1368 | |
---|
1369 | if( mapper_cxy == local_cxy ) |
---|
1370 | { |
---|
1371 | error = mapper_move_kernel( mapper_ptr, |
---|
1372 | true, // to_buffer |
---|
1373 | elf_offset, |
---|
1374 | base_xp, |
---|
1375 | CONFIG_PPM_PAGE_SIZE ); |
---|
1376 | } |
---|
1377 | else |
---|
1378 | { |
---|
1379 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
1380 | mapper_ptr, |
---|
1381 | true, // to buffer |
---|
1382 | false, // kernel buffer |
---|
1383 | elf_offset, |
---|
1384 | base_xp, |
---|
1385 | CONFIG_PPM_PAGE_SIZE, |
---|
1386 | &error ); |
---|
1387 | } |
---|
1388 | if( error ) return EINVAL; |
---|
1389 | } |
---|
1390 | else // both in mapper and in BSS : |
---|
1391 | // - (file_size - offset) bytes from mapper |
---|
1392 | // - (page_size + offset - file_size) bytes from BSS |
---|
1393 | { |
---|
1394 | |
---|
1395 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / both mapper & BSS\n" |
---|
1396 | " %d bytes from mapper / %d bytes from BSS\n", |
---|
1397 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, |
---|
1398 | file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
1399 | |
---|
1400 | // initialize mapper part |
---|
1401 | if( mapper_cxy == local_cxy ) |
---|
1402 | { |
---|
1403 | error = mapper_move_kernel( mapper_ptr, |
---|
1404 | true, // to buffer |
---|
1405 | elf_offset, |
---|
1406 | base_xp, |
---|
1407 | file_size - offset ); |
---|
1408 | } |
---|
1409 | else |
---|
1410 | { |
---|
1411 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
1412 | mapper_ptr, |
---|
1413 | true, // to buffer |
---|
1414 | false, // kernel buffer |
---|
1415 | elf_offset, |
---|
1416 | base_xp, |
---|
1417 | file_size - offset, |
---|
1418 | &error ); |
---|
1419 | } |
---|
1420 | if( error ) return EINVAL; |
---|
1421 | |
---|
1422 | // initialize BSS part |
---|
1423 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
1424 | { |
---|
1425 | memset( GET_PTR( base_xp ) + file_size - offset , 0 , |
---|
1426 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
1427 | } |
---|
1428 | else |
---|
1429 | { |
---|
1430 | hal_remote_memset( base_xp + file_size - offset , 0 , |
---|
1431 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
1432 | } |
---|
1433 | } |
---|
1434 | } // end initialisation for CODE or DATA types |
---|
1435 | } |
---|
1436 | |
---|
1437 | // return ppn |
---|
1438 | *ppn = ppm_page2ppn( page_xp ); |
---|
1439 | |
---|
1440 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for vpn = %x / ppn = %x\n", |
---|
1441 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , *ppn ); |
---|
1442 | |
---|
1443 | return 0; |
---|
1444 | |
---|
1445 | } // end vmm_get_one_ppn() |
---|
1446 | |
---|
1447 | ///////////////////////////////////////// |
---|
1448 | error_t vmm_get_pte( process_t * process, |
---|
1449 | vpn_t vpn, |
---|
1450 | bool_t cow, |
---|
1451 | uint32_t * attr, |
---|
1452 | ppn_t * ppn ) |
---|
1453 | { |
---|
1454 | vseg_t * vseg; // pointer on vseg containing VPN |
---|
1455 | ppn_t old_ppn; // current PTE_PPN |
---|
1456 | uint32_t old_attr; // current PTE_ATTR |
---|
1457 | ppn_t new_ppn; // new PTE_PPN |
---|
1458 | uint32_t new_attr; // new PTE_ATTR |
---|
1459 | error_t error; |
---|
1460 | |
---|
1461 | // this function must be called by a thread running in the reference cluster |
---|
1462 | assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , |
---|
1463 | "not called in the reference cluster\n" ); |
---|
1464 | |
---|
1465 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter for vpn = %x in process %x / cow = %d\n", |
---|
1466 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , process->pid , cow ); |
---|
1467 | |
---|
1468 | // get VMM pointer |
---|
1469 | vmm_t * vmm = &process->vmm; |
---|
1470 | |
---|
1471 | // get vseg pointer from ref VSL |
---|
1472 | error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg ); |
---|
1473 | |
---|
1474 | if( error ) |
---|
1475 | { |
---|
1476 | printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n", |
---|
1477 | __FUNCTION__ , process->pid , vpn ); |
---|
1478 | return error; |
---|
1479 | } |
---|
1480 | |
---|
1481 | vmm_dmsg("\n[DBG] %s : core[%x,%d] found vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
1482 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
1483 | vseg_type_str(vseg->type) , vseg->vpn_base , vseg->vpn_size ); |
---|
1484 | |
---|
1485 | // access GPT to get current PTE attributes and PPN |
---|
1486 | hal_gpt_get_pte( &vmm->gpt , vpn , &old_attr , &old_ppn ); |
---|
1487 | |
---|
1488 | // for both "copy_on_write" and "page_fault" events, allocate a physical page, |
---|
1489 | // initialize it, register it in the reference GPT, update GPT copies in all |
---|
1490 | // clusters containing a copy, and return the new_ppn and new_attr |
---|
1491 | |
---|
1492 | if( cow ) ////////////// copy_on_write request /////////// |
---|
1493 | { |
---|
1494 | assert( (old_attr & GPT_MAPPED) , __FUNCTION__ , |
---|
1495 | "PTE must be mapped for a copy-on-write exception\n" ); |
---|
1496 | |
---|
1497 | excp_dmsg("\n[DBG] %s : core[%x,%d] handling COW for vpn %x\n", |
---|
1498 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
1499 | |
---|
1500 | // get extended pointer, cluster and local pointer on page descriptor |
---|
1501 | xptr_t page_xp = ppm_ppn2page( old_ppn ); |
---|
1502 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
1503 | page_t * page_ptr = (page_t *)GET_PTR( page_xp ); |
---|
1504 | |
---|
1505 | // get number of pending forks in page descriptor |
---|
1506 | uint32_t count = hal_remote_lw( XPTR( page_cxy , &page_ptr->fork_nr ) ); |
---|
1507 | |
---|
1508 | if( count ) // pending fork => allocate a new page, copy it, reset COW |
---|
1509 | { |
---|
1510 | // allocate a new physical page |
---|
1511 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
1512 | if( page_xp == XPTR_NULL ) |
---|
1513 | { |
---|
1514 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", |
---|
1515 | __FUNCTION__ , process->pid , vpn ); |
---|
1516 | return -1; |
---|
1517 | } |
---|
1518 | |
---|
1519 | // compute allocated page PPN |
---|
1520 | new_ppn = ppm_page2ppn( page_xp ); |
---|
1521 | |
---|
1522 | // copy old page content to new page |
---|
1523 | xptr_t old_base_xp = ppm_ppn2base( old_ppn ); |
---|
1524 | xptr_t new_base_xp = ppm_ppn2base( new_ppn ); |
---|
1525 | memcpy( GET_PTR( new_base_xp ), |
---|
1526 | GET_PTR( old_base_xp ), |
---|
1527 | CONFIG_PPM_PAGE_SIZE ); |
---|
1528 | } |
---|
1529 | else // no pending fork => keep the existing page, reset COW |
---|
1530 | { |
---|
1531 | new_ppn = old_ppn; |
---|
1532 | } |
---|
1533 | |
---|
1534 | // build new_attr : reset COW and set WRITABLE, |
---|
1535 | new_attr = (old_attr | GPT_WRITABLE) & (~GPT_COW); |
---|
1536 | |
---|
1537 | // update GPT[vpn] for all GPT copies |
---|
1538 | // to maintain coherence of copies |
---|
1539 | vmm_update_pte( process, |
---|
1540 | vpn, |
---|
1541 | new_attr, |
---|
1542 | new_ppn ); |
---|
1543 | |
---|
1544 | // decrement fork_nr in page descriptor |
---|
1545 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->fork_nr ) , -1 ); |
---|
1546 | } |
---|
1547 | else /////////////// page_fault request /////////// |
---|
1548 | { |
---|
1549 | if( (old_attr & GPT_MAPPED) == 0 ) // true page_fault => map it |
---|
1550 | { |
---|
1551 | |
---|
1552 | excp_dmsg("\n[DBG] %s : core[%x,%d] handling page fault for vpn %x\n", |
---|
1553 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
1554 | |
---|
1555 | // allocate new_ppn, depending on vseg type |
---|
1556 | error = vmm_get_one_ppn( vseg , vpn , &new_ppn ); |
---|
1557 | if( error ) |
---|
1558 | { |
---|
1559 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", |
---|
1560 | __FUNCTION__ , process->pid , vpn ); |
---|
1561 | return -1; |
---|
1562 | } |
---|
1563 | |
---|
1564 | // define new_attr from vseg flags |
---|
1565 | new_attr = GPT_MAPPED | GPT_SMALL; |
---|
1566 | if( vseg->flags & VSEG_USER ) new_attr |= GPT_USER; |
---|
1567 | if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE; |
---|
1568 | if( vseg->flags & VSEG_EXEC ) new_attr |= GPT_EXECUTABLE; |
---|
1569 | if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE; |
---|
1570 | |
---|
1571 | // register new PTE in reference GPT |
---|
1572 | // on demand policy => no update of GPT copies |
---|
1573 | error = hal_gpt_set_pte( &vmm->gpt, |
---|
1574 | vpn, |
---|
1575 | new_attr, |
---|
1576 | new_ppn ); |
---|
1577 | if( error ) |
---|
1578 | { |
---|
1579 | printk("\n[ERROR] in %s : cannot update GPT / process = %x / vpn = %x\n", |
---|
1580 | __FUNCTION__ , process->pid , vpn ); |
---|
1581 | return -1; |
---|
1582 | } |
---|
1583 | } |
---|
1584 | else // mapped in reference GPT => get it |
---|
1585 | { |
---|
1586 | new_ppn = old_ppn; |
---|
1587 | new_attr = old_attr; |
---|
1588 | } |
---|
1589 | } |
---|
1590 | |
---|
1591 | excp_dmsg("\n[DBG] %s : core[%x,%d] update GPT for vpn %x / ppn = %x / attr = %x\n", |
---|
1592 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , new_ppn , new_attr ); |
---|
1593 | |
---|
1594 | // retur success |
---|
1595 | *ppn = new_ppn; |
---|
1596 | *attr = new_attr; |
---|
1597 | return 0; |
---|
1598 | |
---|
1599 | } // end vmm_get_pte() |
---|
1600 | |
---|
1601 | /////////////////////////////////////////////////// |
---|
1602 | error_t vmm_handle_page_fault( process_t * process, |
---|
1603 | vpn_t vpn ) |
---|
1604 | { |
---|
1605 | uint32_t attr; // missing page attributes |
---|
1606 | ppn_t ppn; // missing page PPN |
---|
1607 | error_t error; |
---|
1608 | |
---|
1609 | // get reference process cluster and local pointer |
---|
1610 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
1611 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
1612 | |
---|
1613 | // get missing PTE attributes and PPN from reference cluster |
---|
1614 | if( local_cxy != ref_cxy ) |
---|
1615 | { |
---|
1616 | rpc_vmm_get_pte_client( ref_cxy, |
---|
1617 | ref_ptr, |
---|
1618 | vpn, |
---|
1619 | false, // page_fault |
---|
1620 | &attr, |
---|
1621 | &ppn, |
---|
1622 | &error ); |
---|
1623 | |
---|
1624 | // get local VMM pointer |
---|
1625 | vmm_t * vmm = &process->vmm; |
---|
1626 | |
---|
1627 | // update local GPT |
---|
1628 | error |= hal_gpt_set_pte( &vmm->gpt, |
---|
1629 | vpn, |
---|
1630 | attr, |
---|
1631 | ppn ); |
---|
1632 | } |
---|
1633 | else // local cluster is the reference cluster |
---|
1634 | { |
---|
1635 | error = vmm_get_pte( process, |
---|
1636 | vpn, |
---|
1637 | false, // page-fault |
---|
1638 | &attr, |
---|
1639 | &ppn ); |
---|
1640 | } |
---|
1641 | |
---|
1642 | return error; |
---|
1643 | |
---|
1644 | } // end vmm_handle_page_fault() |
---|
1645 | |
---|
1646 | //////////////////////////////////////////// |
---|
1647 | error_t vmm_handle_cow( process_t * process, |
---|
1648 | vpn_t vpn ) |
---|
1649 | { |
---|
1650 | uint32_t attr; // missing page attributes |
---|
1651 | ppn_t ppn; // missing page PPN |
---|
1652 | error_t error; |
---|
1653 | |
---|
1654 | // get reference process cluster and local pointer |
---|
1655 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
1656 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
1657 | |
---|
1658 | // get new PTE attributes and PPN from reference cluster |
---|
1659 | if( local_cxy != ref_cxy ) |
---|
1660 | { |
---|
1661 | rpc_vmm_get_pte_client( ref_cxy, |
---|
1662 | ref_ptr, |
---|
1663 | vpn, |
---|
1664 | true, // copy-on-write |
---|
1665 | &attr, |
---|
1666 | &ppn, |
---|
1667 | &error ); |
---|
1668 | |
---|
1669 | // get local VMM pointer |
---|
1670 | vmm_t * vmm = &process->vmm; |
---|
1671 | |
---|
1672 | // update local GPT |
---|
1673 | error |= hal_gpt_set_pte( &vmm->gpt, |
---|
1674 | vpn, |
---|
1675 | attr, |
---|
1676 | ppn ); |
---|
1677 | } |
---|
1678 | else // local cluster is the reference cluster |
---|
1679 | { |
---|
1680 | error = vmm_get_pte( process, |
---|
1681 | vpn, |
---|
1682 | true, // copy-on-write |
---|
1683 | &attr, |
---|
1684 | &ppn ); |
---|
1685 | } |
---|
1686 | |
---|
1687 | return error; |
---|
1688 | |
---|
1689 | } // end vmm_handle_cow() |
---|
1690 | |
---|
1691 | /////////////////////////////////////////// |
---|
1692 | error_t vmm_v2p_translate( bool_t ident, |
---|
1693 | void * ptr, |
---|
1694 | paddr_t * paddr ) |
---|
1695 | { |
---|
1696 | process_t * process = CURRENT_THREAD->process; |
---|
1697 | |
---|
1698 | if( ident ) // identity mapping |
---|
1699 | { |
---|
1700 | *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr ); |
---|
1701 | return 0; |
---|
1702 | } |
---|
1703 | |
---|
1704 | // access page table |
---|
1705 | error_t error; |
---|
1706 | vpn_t vpn; |
---|
1707 | uint32_t attr; |
---|
1708 | ppn_t ppn; |
---|
1709 | uint32_t offset; |
---|
1710 | |
---|
1711 | vpn = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT ); |
---|
1712 | offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK ); |
---|
1713 | |
---|
1714 | if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process |
---|
1715 | { |
---|
1716 | error = vmm_get_pte( process, vpn , false , &attr , &ppn ); |
---|
1717 | } |
---|
1718 | else // calling process is not reference process |
---|
1719 | { |
---|
1720 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
1721 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
1722 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , false , &attr , &ppn , &error ); |
---|
1723 | } |
---|
1724 | |
---|
1725 | // set paddr |
---|
1726 | *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset; |
---|
1727 | |
---|
1728 | return error; |
---|
1729 | |
---|
1730 | } // end vmm_v2p_translate() |
---|
1731 | |
---|
1732 | |
---|