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