source: trunk/kernel/mm/vmm.c @ 669

Last change on this file since 669 was 665, checked in by alain, 4 years ago

Fix a bug in the vmm_get_vseg() function:
When the local VSL is also the reference VSL,
the vmm_get_vseg() tried to take twice the same lock.

File size: 102.9 KB
RevLine 
[1]1/*
[611]2 * vmm.c - virtual memory manager related operations definition.
[1]3 *
4 * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
[657]5 *           Alain Greiner (2016,2017,2018,2019,2020)
[21]6 *
[1]7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
[14]25#include <kernel_config.h>
[457]26#include <hal_kernel_types.h>
[1]27#include <hal_special.h>
28#include <hal_gpt.h>
[409]29#include <hal_vmm.h>
[641]30#include <hal_irqmask.h>
[577]31#include <hal_macros.h>
[1]32#include <printk.h>
[23]33#include <memcpy.h>
[567]34#include <remote_queuelock.h>
[1]35#include <list.h>
[408]36#include <xlist.h>
[1]37#include <bits.h>
38#include <process.h>
39#include <thread.h>
40#include <vseg.h>
41#include <cluster.h>
42#include <scheduler.h>
43#include <vfs.h>
44#include <mapper.h>
45#include <page.h>
46#include <kmem.h>
47#include <vmm.h>
[585]48#include <hal_exception.h>
[1]49
[635]50////////////////////////////////////////////////////////////////////////////////////////////
[1]51//   Extern global variables
[635]52////////////////////////////////////////////////////////////////////////////////////////////
[1]53
[567]54extern  process_t  process_zero;      // allocated in cluster.c
[1]55
[625]56////////////////////////////////////////////////////////////////////////////////////////////
[651]57// This static function is called by the vmm_user_init() function.
58// It initialises the free lists of vsegs used by the VMM MMAP allocator.
59// It makes the assumption that HEAP_BASE == 1 Gbytes and HEAP_SIZE == 2 Gbytes.
60////////////////////////////////////////////////////////////////////////////////////////////
61static void vmm_stack_init( vmm_t * vmm )
62{
63
64// check STACK zone
65assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREADS_MAX_PER_CLUSTER) <=
66(CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , "STACK zone too small\n");
67
68    // get pointer on STACK allocator
69    stack_mgr_t * mgr = &vmm->stack_mgr;
70
71    mgr->bitmap   = 0;
72    mgr->vpn_base = CONFIG_VMM_STACK_BASE;
73    busylock_init( &mgr->lock , LOCK_VMM_STACK );
74
75}
76
77////////////////////////////////////////////////////////////////////////////////////////////
[625]78// This static function is called by the vmm_create_vseg() function, and implements
[651]79// the VMM STACK specific allocator. Depending on the local thread index <ltid>,
80// it ckeks availability of the corresponding slot in the process STACKS region,
81// allocates a vseg descriptor, and initializes the "vpn_base" and "vpn_size" fields.
[625]82////////////////////////////////////////////////////////////////////////////////////////////
83// @ vmm      : [in]  pointer on VMM.
84// @ ltid     : [in]  requested slot == local user thread identifier.
85////////////////////////////////////////////////////////////////////////////////////////////
[651]86static vseg_t * vmm_stack_alloc( vmm_t  * vmm,
87                                 ltid_t   ltid )
[21]88{
[625]89
90// check ltid argument
91assert( (ltid <= ((CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) / CONFIG_VMM_STACK_SIZE)),
92"slot index %d too large for an user stack vseg", ltid );
93
94    // get stack allocator pointer
95    stack_mgr_t * mgr = &vmm->stack_mgr;
96
[651]97    // get lock protecting stack allocator
[625]98    busylock_acquire( &mgr->lock );
99
100// check requested slot is available
101assert( (bitmap_state( &mgr->bitmap , ltid ) == false),
102"slot index %d already allocated", ltid );
103
[651]104    // allocate a vseg descriptor
105    vseg_t * vseg = vseg_alloc();
106
107    if( vseg == NULL )
108        {
109        // release lock protecting free lists
110        busylock_release( &mgr->lock );
111
112        printk("\n[ERROR] %s cannot allocate memory for vseg in cluster %x\n",
113        __FUNCTION__ , local_cxy );
114
115        return NULL;
116    }
117
[625]118    // update bitmap
119    bitmap_set( &mgr->bitmap , ltid );
120
121    // release lock on stack allocator
122    busylock_release( &mgr->lock );
123
[651]124    // set "vpn_base" & "vpn_size" fields (first page non allocated)
125    vseg->vpn_base = mgr->vpn_base + (ltid * CONFIG_VMM_STACK_SIZE) + 1;
126    vseg->vpn_size = CONFIG_VMM_STACK_SIZE - 1;
[625]127
[651]128    return vseg;
129
[625]130} // end vmm_stack_alloc()
131
132////////////////////////////////////////////////////////////////////////////////////////////
133// This static function is called by the vmm_remove_vseg() function, and implements
134// the VMM STACK specific desallocator.
[651]135// It updates the bitmap to release the corresponding slot in the process STACKS region,
136// and releases memory allocated to vseg descriptor.
[625]137////////////////////////////////////////////////////////////////////////////////////////////
138// @ vmm      : [in] pointer on VMM.
139// @ vseg     : [in] pointer on released vseg.
140////////////////////////////////////////////////////////////////////////////////////////////
141static void vmm_stack_free( vmm_t  * vmm,
142                            vseg_t * vseg )
143{
144    // get stack allocator pointer
145    stack_mgr_t * mgr = &vmm->stack_mgr;
146
147    // compute slot index
148    uint32_t index = (vseg->vpn_base - 1 - mgr->vpn_base) / CONFIG_VMM_STACK_SIZE;
149
150// check index
151assert( (index <= ((CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) / CONFIG_VMM_STACK_SIZE)),
152"slot index %d too large for an user stack vseg", index );
153
154// check released slot is allocated
155assert( (bitmap_state( &mgr->bitmap , index ) == true),
156"released slot index %d non allocated", index );
157
158    // get lock on stack allocator
159    busylock_acquire( &mgr->lock );
160
161    // update stacks_bitmap
162    bitmap_clear( &mgr->bitmap , index );
163
164    // release lock on stack allocator
165    busylock_release( &mgr->lock );
166
[651]167    // release memory allocated to vseg descriptor
168    vseg_free( vseg );
169
[625]170}  // end vmm_stack_free()
171
[651]172
173
[625]174////////////////////////////////////////////////////////////////////////////////////////////
[651]175// This function display the current state of the VMM MMAP allocator of a process VMM
176// identified by the <vmm> argument.
177////////////////////////////////////////////////////////////////////////////////////////////
178void vmm_mmap_display( vmm_t * vmm )
179{
180    uint32_t  order;
181    xptr_t    root_xp;
182    xptr_t    iter_xp;
183
184    // get pointer on process
185    process_t * process = (process_t *)(((char*)vmm) - OFFSETOF( process_t , vmm ));
186
187    // get process PID
188    pid_t pid = process->pid;
189
190    // get pointer on VMM MMAP allocator
191    mmap_mgr_t * mgr = &vmm->mmap_mgr;
192
193    // display header
194    printk("***** VMM MMAP allocator / process %x *****\n", pid );
195
196    // scan the array of free lists of vsegs
197    for( order = 0 ; order <= CONFIG_VMM_HEAP_MAX_ORDER ; order++ )
198    {
199        root_xp = XPTR( local_cxy , &mgr->free_list_root[order] );
200
201        if( !xlist_is_empty( root_xp ) )
202        {
203            printk(" - %d (%x pages) : ", order , 1<<order );
204
205            XLIST_FOREACH( root_xp , iter_xp )
206            {
207                xptr_t   vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
208                vseg_t * vseg    = GET_PTR( vseg_xp );
209
210                printk("%x | ", vseg->vpn_base );
211            }
212
213            printk("\n");
214        }
215    }
216}  // end vmm_mmap_display()
217
218////////////////////////////////////////////////////////////////////////////////////////////
219// This static function is called by the vmm_user_init() function.
220// It initialises the free lists of vsegs used by the VMM MMAP allocator.
221// TODO this function is only valid for 32 bits cores, and makes three assumptions:
222// HEAP_BASE == 1 Gbytes / HEAP_SIZE == 2 Gbytes / MMAP_MAX_SIZE == 1 Gbytes
223////////////////////////////////////////////////////////////////////////////////////////////
224void vmm_mmap_init( vmm_t * vmm )
225{
226
227// check HEAP base and size
228assert( (CONFIG_VMM_HEAP_BASE == 0x40000) & (CONFIG_VMM_STACK_BASE == 0xc0000),
229"CONFIG_VMM_HEAP_BASE != 0x40000 or CONFIG_VMM_STACK_BASE != 0xc0000" );
230
231// check  MMAP vseg max order
232assert( (CONFIG_VMM_HEAP_MAX_ORDER == 18), "max mmap vseg size is 256K pages" );
233
234    // get pointer on MMAP allocator
235    mmap_mgr_t * mgr = &vmm->mmap_mgr;
236
237    // initialize HEAP base and size
238    mgr->vpn_base        = CONFIG_VMM_HEAP_BASE;
239    mgr->vpn_size        = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE;
240
241    // initialize lock
242    busylock_init( &mgr->lock , LOCK_VMM_MMAP );
243
244    // initialize free lists
245    uint32_t   i;
246    for( i = 0 ; i <= CONFIG_VMM_HEAP_MAX_ORDER ; i++ )
247    {
248        xlist_root_init( XPTR( local_cxy , &mgr->free_list_root[i] ) );
249    }
250
251    // allocate and register first 1 Gbytes vseg
252    vseg_t * vseg0 = vseg_alloc();
253
254assert( (vseg0 != NULL) , "cannot allocate vseg" );
255
256    vseg0->vpn_base = CONFIG_VMM_HEAP_BASE;
257    vseg0->vpn_size = CONFIG_VMM_HEAP_BASE;
258
259    xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[CONFIG_VMM_HEAP_MAX_ORDER] ),
260                     XPTR( local_cxy , &vseg0->xlist ) );
261
262    // allocate and register second 1 Gbytes vseg
263    vseg_t * vseg1 = vseg_alloc();
264
265assert( (vseg1 != NULL) , "cannot allocate vseg" );
266
267    vseg1->vpn_base = CONFIG_VMM_HEAP_BASE << 1;
268    vseg1->vpn_size = CONFIG_VMM_HEAP_BASE;
269
270    xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[CONFIG_VMM_HEAP_MAX_ORDER] ),
271                     XPTR( local_cxy , &vseg1->xlist ) );
272
273#if DEBUG_VMM_MMAP
274thread_t * this = CURRENT_THREAD;
275uint32_t cycle = (uint32_t)hal_get_cycles();
276printk("\n[%s] thread[%x,%x] / cycle %d\n",
277__FUNCTION__, this->process->pid, this->trdid, cycle );
278vmm_mmap_display( vmm );
279#endif
280
281}  // end vmm_mmap_init()
282
283////////////////////////////////////////////////////////////////////////////////////////////
[625]284// This static function is called by the vmm_create_vseg() function, and implements
[651]285// the VMM MMAP specific allocator.  Depending on the requested number of pages <npages>,
286// it get a free vseg from the relevant free_list, and initializes the "vpn_base" and
287// "vpn_size" fields.
[625]288////////////////////////////////////////////////////////////////////////////////////////////
289// @ vmm      : [in] pointer on VMM.
290// @ npages   : [in] requested number of pages.
[651]291// @ returns local pointer on vseg if success / returns NULL if failure.
[625]292////////////////////////////////////////////////////////////////////////////////////////////
[651]293static vseg_t * vmm_mmap_alloc( vmm_t * vmm,
294                                vpn_t   npages )
[625]295{
296
[651]297#if DEBUG_VMM_MMAP
[625]298thread_t * this = CURRENT_THREAD;
299uint32_t cycle = (uint32_t)hal_get_cycles();
[651]300if( DEBUG_VMM_MMAP < cycle )
301printk("\n[%s] thread[%x,%x] for %x pages / cycle %d\n",
302__FUNCTION__, this->process->pid, this->trdid, npages, cycle );
[625]303#endif
304
305    // number of allocated pages must be power of 2
306    // compute actual size and order
[651]307    vpn_t    required_vpn_size = POW2_ROUNDUP( npages );
308    uint32_t required_order    = bits_log2( required_vpn_size );
[625]309
310    // get mmap allocator pointer
311    mmap_mgr_t * mgr = &vmm->mmap_mgr;
312
[651]313    // take lock protecting free lists in MMAP allocator
[625]314    busylock_acquire( &mgr->lock );
315
[651]316    // initialises the while loop variables
317    uint32_t   current_order = required_order;
318    vseg_t   * current_vseg  = NULL;
[625]319
[651]320    // search a free vseg equal or larger than requested size
321        while( current_order <= CONFIG_VMM_HEAP_MAX_ORDER )
322        {
323        // build extended pointer on free_pages_root[current_order]
324        xptr_t root_xp = XPTR( local_cxy , &mgr->free_list_root[current_order] );
[625]325
[651]326                if( !xlist_is_empty( root_xp ) )
327                {
328            // get extended pointer on first vseg in this free_list
329                        xptr_t current_vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
330            current_vseg = GET_PTR( current_vseg_xp );
331
332            // build extended pointer on xlist field in vseg descriptor
333            xptr_t list_entry_xp = XPTR( local_cxy , &current_vseg->xlist );
334
335            // remove this vseg from the free_list
336                        xlist_unlink( list_entry_xp );
337
338                        break; 
339                }
340
341        // increment loop index
342        current_order++;
343
344    }  // end while loop
345
346    if( current_vseg == NULL )  // return failure
[625]347    {
[651]348        // release lock protecting free lists
349        busylock_release( &mgr->lock );
[625]350
[651]351        printk("\n[ERROR] %s cannot allocate ) %d page(s) in cluster %x\n",
352        __FUNCTION__, npages , local_cxy );
[625]353
[651]354        return NULL;
[625]355    }
356
[651]357        // split recursively the found vseg in smaller vsegs
358    // if required, and update the free-lists accordingly
359        while( current_order > required_order )
360        {
361        // get found vseg base and size
362        vpn_t  vpn_base = current_vseg->vpn_base;
363        vpn_t  vpn_size = current_vseg->vpn_size;
364       
365        // allocate a new vseg for the upper half of current vseg
366            vseg_t * new_vseg = vseg_alloc();
[625]367
[651]368            if( new_vseg == NULL )
369        {
370                // release lock protecting free lists
371            busylock_release( &mgr->lock );
372
373            printk("\n[ERROR] %s cannot allocate memory for vseg in cluster %x\n",
374            __FUNCTION__ , local_cxy );
375
376            return NULL;
377            }
378
379        // initialise new vseg (upper half of found vseg)
380        new_vseg->vmm      = vmm;
381        new_vseg->vpn_base = vpn_base + (vpn_size >> 1);
382        new_vseg->vpn_size = vpn_size >> 1;
383
384        // insert new vseg in relevant free_list
385                xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[current_order-1] ),
386                         XPTR( local_cxy , &new_vseg->xlist ) );
387
388        // update found vseg
389        current_vseg->vpn_size = vpn_size>>1; 
390
391        // update order
392                current_order --;
393        }
394
395        // release lock protecting free lists
396        busylock_release( &mgr->lock );
397
398#if DEBUG_VMM_MMAP
399vmm_mmap_display( vmm );
[625]400#endif
401
[651]402    return current_vseg;
[625]403
404}  // end vmm_mmap_alloc()
405
406////////////////////////////////////////////////////////////////////////////////////////////
[641]407// This static function implements the VMM MMAP specific desallocator.
408// It is called by the vmm_remove_vseg() function.
[651]409// It releases the vseg to the relevant free_list, after trying (recursively) to
410// merge it to the buddy vseg.
[625]411////////////////////////////////////////////////////////////////////////////////////////////
412// @ vmm      : [in] pointer on VMM.
413// @ vseg     : [in] pointer on released vseg.
414////////////////////////////////////////////////////////////////////////////////////////////
415static void vmm_mmap_free( vmm_t  * vmm,
416                           vseg_t * vseg )
417{
[651]418
419#if DEBUG_VMM_MMAP
420thread_t * this = CURRENT_THREAD;
421uint32_t cycle = (uint32_t)hal_get_cycles();
422if( DEBUG_VMM_MMAP < cycle )
423printk("\n[%s] thread[%x,%x] for vpn_base %x / vpn_size %x / cycle %d\n",
424__FUNCTION__, this->process->pid, this->trdid, vseg->vpn_base, vseg->vpn_size, cycle );
425#endif
426
427    vseg_t * buddy_vseg;
428
429    // get mmap allocator pointer
[625]430    mmap_mgr_t * mgr = &vmm->mmap_mgr;
431
[651]432    // take lock protecting free lists
[625]433    busylock_acquire( &mgr->lock );
434
[651]435    // initialise loop variables
436    // released_vseg is the currently released vseg
437    vseg_t * released_vseg     = vseg;
438    uint32_t released_order    = bits_log2( vseg->vpn_size );
[625]439
[651]440        // iteratively merge the released vseg to the buddy vseg
441        // release the current page and exit when buddy not found
442    while( released_order <= CONFIG_VMM_HEAP_MAX_ORDER )
443    {
444        // compute buddy_vseg vpn_base
445                vpn_t buddy_vpn_base = released_vseg->vpn_base ^ (1 << released_order);
446       
447        // build extended pointer on free_pages_root[current_order]
448        xptr_t root_xp = XPTR( local_cxy , &mgr->free_list_root[released_order] );
449
450        // scan this free list to find the buddy vseg
451        xptr_t   iter_xp;
452        buddy_vseg = NULL;
453        XLIST_FOREACH( root_xp , iter_xp )
454        {
455            xptr_t   current_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
456            vseg_t * current_vseg    = GET_PTR( current_vseg_xp );
457
458            if( current_vseg->vpn_base == buddy_vpn_base )
459            {
460                buddy_vseg = current_vseg;
461                break;
462            }
463        }
464       
465        if( buddy_vseg != NULL )     // buddy found => merge released & buddy
466        {
467            // update released vseg fields
468            released_vseg->vpn_size = buddy_vseg->vpn_size<<1;
469            if( released_vseg->vpn_base > buddy_vseg->vpn_base) 
470                released_vseg->vpn_base = buddy_vseg->vpn_base;
471
472            // remove buddy vseg from free_list
473            xlist_unlink( XPTR( local_cxy , &buddy_vseg->xlist ) );
474
475            // release memory allocated to buddy descriptor
476            vseg_free( buddy_vseg );
477        }
478        else                         // buddy not found => register & exit
479        {
480            // register released vseg in free list
481            xlist_add_first( root_xp , XPTR( local_cxy , &released_vseg->xlist ) );
482
483            // exit while loop
484            break;
485        }
486
487        // increment released_order
488        released_order++;
489    }
490
[625]491    // release lock
492    busylock_release( &mgr->lock );
493
[651]494#if DEBUG_VMM_MMAP
495vmm_mmap_display( vmm );
496#endif
[625]497
[651]498}  // end vmm_mmap_free()
499
[625]500////////////////////////////////////////////////////////////////////////////////////////////
501// This static function registers one vseg in the VSL of a local process descriptor.
502////////////////////////////////////////////////////////////////////////////////////////////
503// vmm       : [in] pointer on VMM.
504// vseg      : [in] pointer on vseg.
505////////////////////////////////////////////////////////////////////////////////////////////
506void vmm_attach_vseg_to_vsl( vmm_t  * vmm,
507                             vseg_t * vseg )
508{
509    // update vseg descriptor
510    vseg->vmm = vmm;
511
512    // increment vsegs number
513    vmm->vsegs_nr++;
514
515    // add vseg in vmm list
516    xlist_add_last( XPTR( local_cxy , &vmm->vsegs_root ),
517                    XPTR( local_cxy , &vseg->xlist ) );
518
519}  // end vmm_attach_vseg_from_vsl()
520
521////////////////////////////////////////////////////////////////////////////////////////////
522// This static function removes one vseg from the VSL of a local process descriptor.
523////////////////////////////////////////////////////////////////////////////////////////////
524// vmm       : [in] pointer on VMM.
525// vseg      : [in] pointer on vseg.
526////////////////////////////////////////////////////////////////////////////////////////////
527void vmm_detach_vseg_from_vsl( vmm_t  * vmm,
528                               vseg_t * vseg )
529{
530    // update vseg descriptor
531    vseg->vmm = NULL;
532
533    // decrement vsegs number
534    vmm->vsegs_nr--;
535
536    // remove vseg from VSL
537    xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
538
539}  // end vmm_detach_from_vsl()
540
541////////////////////////////////////////////
542error_t vmm_user_init( process_t * process )
543{
[1]544
[625]545#if DEBUG_VMM_USER_INIT
[567]546thread_t * this = CURRENT_THREAD;
[433]547uint32_t cycle = (uint32_t)hal_get_cycles();
[625]548if( DEBUG_VMM_USER_INIT )
[614]549printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", 
550__FUNCTION__ , this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]551#endif
[204]552
[1]553    // get pointer on VMM
554    vmm_t   * vmm = &process->vmm;
555
[625]556// check UTILS zone
[624]557assert( ((CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) <= 
558         (CONFIG_VMM_ELF_BASE - CONFIG_VMM_UTILS_BASE)) ,
559         "UTILS zone too small\n" );
[21]560
[651]561    // initialize lock protecting the VSL
[640]562        remote_queuelock_init( XPTR( local_cxy , &vmm->vsl_lock ) , LOCK_VMM_VSL );
[635]563
564
[651]565    // initialize STACK allocator
566    vmm_stack_init( vmm );
567
568    // initialize MMAP allocator
569    vmm_mmap_init( vmm );
570
571    // initialize instrumentation counters
572        vmm->false_pgfault_nr    = 0;
573        vmm->local_pgfault_nr    = 0;
574        vmm->global_pgfault_nr   = 0;
575        vmm->false_pgfault_cost  = 0;
576        vmm->local_pgfault_cost  = 0;
577        vmm->global_pgfault_cost = 0;
578
[635]579/*
[625]580    // register "args" vseg in VSL
[624]581    base = CONFIG_VMM_UTILS_BASE << CONFIG_PPM_PAGE_SHIFT;
[1]582    size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]583
[407]584    vseg_args = vmm_create_vseg( process,
585                                 VSEG_TYPE_DATA,
586                                 base,
587                                 size,
588                                 0,             // file_offset unused
589                                 0,             // file_size unused
590                                 XPTR_NULL,     // mapper_xp unused
591                                 local_cxy );
[415]592    if( vseg_args == NULL )
593    {
594        printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ );
595        return -1;
596    }
[204]597
[406]598    vmm->args_vpn_base = base;
[1]599
[625]600    // register "envs" vseg in VSL
[624]601    base = (CONFIG_VMM_UTILS_BASE + CONFIG_VMM_ARGS_SIZE) << CONFIG_PPM_PAGE_SHIFT;
[1]602    size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]603
[407]604    vseg_envs = vmm_create_vseg( process,
605                                 VSEG_TYPE_DATA,
606                                 base,
607                                 size,
608                                 0,             // file_offset unused
609                                 0,             // file_size unused
610                                 XPTR_NULL,     // mapper_xp unused
611                                 local_cxy );
[415]612    if( vseg_envs == NULL )
613    {
614        printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ );
615        return -1;
616    }
[204]617
[406]618    vmm->envs_vpn_base = base;
[635]619*/
[124]620    hal_fence();
[1]621
[625]622#if DEBUG_VMM_USER_INIT
[433]623cycle = (uint32_t)hal_get_cycles();
[625]624if( DEBUG_VMM_USER_INIT )
[614]625printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", 
626__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]627#endif
[204]628
[415]629    return 0;
630
[625]631}  // end vmm_user_init()
[204]632
[611]633//////////////////////////////////////////
[625]634void vmm_user_reset( process_t * process )
[567]635{
[625]636    xptr_t       vseg_xp;
637        vseg_t     * vseg;
638    vseg_type_t  vseg_type;
[567]639
[625]640#if DEBUG_VMM_USER_RESET
[635]641uint32_t   cycle;
[625]642thread_t * this = CURRENT_THREAD;
[635]643#endif
644
645#if (DEBUG_VMM_USER_RESET & 1 )
646cycle = (uint32_t)hal_get_cycles();
[625]647if( DEBUG_VMM_USER_RESET < cycle )
648printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
649__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle );
650#endif
[567]651
[625]652#if (DEBUG_VMM_USER_RESET & 1 )
653if( DEBUG_VMM_USER_RESET < cycle )
[635]654hal_vmm_display( XPTR( local_cxy , process ) , true );
[625]655#endif
[567]656
[625]657    // get pointer on local VMM
658    vmm_t * vmm = &process->vmm;
[624]659
[625]660    // build extended pointer on VSL root and VSL lock
661    xptr_t   root_xp = XPTR( local_cxy , &vmm->vsegs_root );
662    xptr_t   lock_xp = XPTR( local_cxy , &vmm->vsl_lock );
[567]663
[625]664    // take the VSL lock
[640]665        remote_queuelock_acquire( lock_xp );
[567]666
[625]667    // scan the VSL to delete all non kernel vsegs
668    // (we don't use a FOREACH in case of item deletion)
669    xptr_t   iter_xp;
670    xptr_t   next_xp;
671        for( iter_xp = hal_remote_l64( root_xp ) ; 
672         iter_xp != root_xp ;
673         iter_xp = next_xp )
674        {
675        // save extended pointer on next item in xlist
676        next_xp = hal_remote_l64( iter_xp );
[611]677
[625]678        // get pointers on current vseg in VSL
679        vseg_xp   = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
680        vseg      = GET_PTR( vseg_xp );
681        vseg_type = vseg->type;
[567]682
[625]683#if( DEBUG_VMM_USER_RESET & 1 )
684if( DEBUG_VMM_USER_RESET < cycle )
685printk("\n[%s] found %s vseg / vpn_base %x / vpn_size %d\n",
686__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
687#endif
688        // delete non kernel vseg 
689        if( (vseg_type != VSEG_TYPE_KCODE) && 
690            (vseg_type != VSEG_TYPE_KDATA) && 
691            (vseg_type != VSEG_TYPE_KDEV ) )
692        {
693            // remove vseg from VSL
694            vmm_remove_vseg( process , vseg );
[567]695
[625]696#if( DEBUG_VMM_USER_RESET & 1 )
697if( DEBUG_VMM_USER_RESET < cycle )
698printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n",
699__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
700#endif
701        }
702        else
703        {
[567]704
[625]705#if( DEBUG_VMM_USER_RESET & 1 )
706if( DEBUG_VMM_USER_RESET < cycle )
707printk("\n[%s] keep %s vseg / vpn_base %x / vpn_size %d\n",
708__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
709#endif
710        }
711        }  // end loop on vsegs in VSL
[567]712
[625]713    // release the VSL lock
[640]714        remote_queuelock_release( lock_xp );
[567]715
[625]716// FIXME il faut gérer les process copies...
[611]717
[625]718#if DEBUG_VMM_USER_RESET
719cycle = (uint32_t)hal_get_cycles();
720if( DEBUG_VMM_USER_RESET < cycle )
721printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
722__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle );
723#endif
[611]724
[635]725#if (DEBUG_VMM_USER_RESET & 1 )
726if( DEBUG_VMM_USER_RESET < cycle )
727hal_vmm_display( XPTR( local_cxy , process ) , true );
728#endif
729
[625]730}  // end vmm_user_reset()
[611]731
[640]732/////////////////////////////////////////////////
733void vmm_global_delete_vseg( process_t * process,
734                             intptr_t    base )
735{
736    cxy_t           owner_cxy;
737    lpid_t          owner_lpid;
[641]738    reg_t           save_sr;
[640]739
[641]740    xptr_t          process_lock_xp;
[640]741    xptr_t          process_root_xp;
742    xptr_t          process_iter_xp;
743
744    xptr_t          remote_process_xp;
745    cxy_t           remote_process_cxy;
746    process_t     * remote_process_ptr;
747
748    xptr_t          vsl_root_xp;
749    xptr_t          vsl_lock_xp;
750    xptr_t          vsl_iter_xp;
751
[641]752    rpc_desc_t      rpc;                  // shared rpc descriptor for parallel RPCs
753    uint32_t        responses;            // RPC responses counter
754
755    thread_t      * this    = CURRENT_THREAD;
756    pid_t           pid     = process->pid;
757    cluster_t     * cluster = LOCAL_CLUSTER;
758
[640]759#if DEBUG_VMM_GLOBAL_DELETE_VSEG
760uint32_t cycle = (uint32_t)hal_get_cycles();
761#endif
762
763#if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1)
764if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]765printk("\n[%s] thread[%x,%x] enters / process %x / base %x / cycle %d\n",
[640]766__FUNCTION__, this->process->pid, this->trdid, process->pid, base, cycle );
767#endif
768
[641]769    // initialize a shared RPC descriptor
770    rpc.rsp       = &responses;
771    rpc.blocking  = false;                  // non blocking behaviour for rpc_send()
772    rpc.index     = RPC_VMM_REMOVE_VSEG;
773    rpc.thread    = this;
774    rpc.lid       = this->core->lid;
775    rpc.args[0]   = this->process->pid;
776    rpc.args[1]   = base;
777
[640]778    // get owner process cluster and local index
779    owner_cxy        = CXY_FROM_PID( pid );
780    owner_lpid       = LPID_FROM_PID( pid );
781
[641]782    // get extended pointer on root and lock of process copies xlist in owner cluster
783    process_root_xp  = XPTR( owner_cxy , &cluster->pmgr.copies_root[owner_lpid] );
784    process_lock_xp  = XPTR( owner_cxy , &cluster->pmgr.copies_lock[owner_lpid] );
[640]785
[641]786    // mask IRQs
787    hal_disable_irq( &save_sr );
788
789    // client thread blocks itself
790    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC );
791
792    // take the lock protecting process copies
793    remote_queuelock_acquire( process_lock_xp );
794
795    // initialize responses counter
796    responses = 0;
797
[640]798    // loop on process copies
799    XLIST_FOREACH( process_root_xp , process_iter_xp )
800    {
801        // get cluster and local pointer on remote process
802        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
803        remote_process_ptr = GET_PTR( remote_process_xp );
804        remote_process_cxy = GET_CXY( remote_process_xp );
805
806        // build extended pointers on remote VSL root and lock
807        vsl_root_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsegs_root );
808        vsl_lock_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsl_lock );
809
810        // get lock on remote VSL
811        remote_queuelock_acquire( vsl_lock_xp );
812
813        // loop on vsegs in remote process VSL
814        XLIST_FOREACH( vsl_root_xp , vsl_iter_xp )
815        {
816            // get pointers on current vseg
817            xptr_t   vseg_xp  = XLIST_ELEMENT( vsl_iter_xp , vseg_t , xlist );
818            vseg_t * vseg_ptr = GET_PTR( vseg_xp );
819
820            // get current vseg base address
821            intptr_t vseg_base = (intptr_t)hal_remote_lpt( XPTR( remote_process_cxy,
822                                                                 &vseg_ptr->min ) );
823
824            if( vseg_base == base )   // found searched vseg
825            {
[641]826                // atomically increment responses counter
827                hal_atomic_add( &responses , 1 );
[640]828
829#if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1)
830if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]831printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n",
832__FUNCTION__, this->process->pid, this->trdid, remote_process_cxy );
[640]833#endif
[641]834                // send RPC to remote cluster
835                rpc_send( remote_process_cxy , &rpc );
[640]836
[641]837                // exit loop on vsegs
838                break;
[640]839            }
840        }  // end of loop on vsegs
841
[641]842        // release lock on remote VSL
843        remote_queuelock_release( vsl_lock_xp );
844
845    }  // end of loop on process copies
846
847    // release the lock protecting process copies
848    remote_queuelock_release( process_lock_xp );
849
[640]850#if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1)
851if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]852printk("\n[%s] thread[%x,%x] deschedule / process %x / base %x\n",
853__FUNCTION__, this->process->pid, this->trdid, process->pid, base );
[640]854#endif
855
[641]856    // client thread deschedule
857    sched_yield("blocked on rpc_vmm_delete_vseg");
858 
859    // restore IRQs
860    hal_restore_irq( save_sr );
[640]861
862#if DEBUG_VMM_GLOBAL_DELETE_VSEG
863cycle = (uint32_t)hal_get_cycles();
864if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]865printk("\n[%s] thread[%x,%x] exit / process %x / base %x / cycle %d\n",
866__FUNCTION__, this->process->pid, this->trdid, process->pid, base, cycle );
[640]867#endif
868
869}  // end vmm_global_delete_vseg()
870
[595]871////////////////////////////////////////////////
[640]872void vmm_global_resize_vseg( process_t * process,
873                             intptr_t    base,
874                             intptr_t    new_base,
875                             intptr_t    new_size )
876{
877    cxy_t           owner_cxy;
878    lpid_t          owner_lpid;
[641]879    reg_t           save_sr;
[640]880
[641]881    xptr_t          process_lock_xp;
[640]882    xptr_t          process_root_xp;
883    xptr_t          process_iter_xp;
884
885    xptr_t          remote_process_xp;
886    cxy_t           remote_process_cxy;
887    process_t     * remote_process_ptr;
888
889    xptr_t          vsl_root_xp;
890    xptr_t          vsl_lock_xp;
891    xptr_t          vsl_iter_xp;
892
[641]893    rpc_desc_t      rpc;                  // shared rpc descriptor for parallel RPCs
894    uint32_t        responses;            // RPC responses counter
895
896    thread_t      * this    = CURRENT_THREAD; 
897    pid_t           pid     = process->pid;
898    cluster_t     * cluster = LOCAL_CLUSTER;
899
[640]900#if DEBUG_VMM_GLOBAL_RESIZE_VSEG
901uint32_t cycle = (uint32_t)hal_get_cycles();
902#endif
903
904#if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1)
905if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
906printk("\n[%s] thread[%x,%x] : process %x / base %x / new_base %x / new_size %x / cycle %d\n",
907__FUNCTION__, this->process->pid, this->trdid, process->pid, base, new_base, new_size, cycle );
908#endif
909
[641]910    // initialize a shared RPC descriptor
911    rpc.rsp       = &responses;
912    rpc.blocking  = false;                  // non blocking behaviour for rpc_send()
913    rpc.index     = RPC_VMM_REMOVE_VSEG;
914    rpc.thread    = this;
915    rpc.lid       = this->core->lid;
916    rpc.args[0]   = this->process->pid;
917    rpc.args[1]   = base;
918    rpc.args[2]   = new_base;
919    rpc.args[3]   = new_size;
920
921    // get owner process cluster and local index
[640]922    owner_cxy        = CXY_FROM_PID( pid );
923    owner_lpid       = LPID_FROM_PID( pid );
924
[641]925    // get extended pointer on root and lock of process copies xlist in owner cluster
926    process_root_xp  = XPTR( owner_cxy , &cluster->pmgr.copies_root[owner_lpid] );
927    process_lock_xp  = XPTR( owner_cxy , &cluster->pmgr.copies_lock[owner_lpid] );
[640]928
[641]929    // mask IRQs
930    hal_disable_irq( &save_sr );
931
932    // client thread blocks itself
933    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC );
934
935    // take the lock protecting process copies
936    remote_queuelock_acquire( process_lock_xp );
937
938    // initialize responses counter
939    responses = 0;
940
[640]941    // loop on process copies
942    XLIST_FOREACH( process_root_xp , process_iter_xp )
943    {
944        // get cluster and local pointer on remote process
945        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
946        remote_process_ptr = GET_PTR( remote_process_xp );
947        remote_process_cxy = GET_CXY( remote_process_xp );
948
949        // build extended pointers on remote VSL root and lock
950        vsl_root_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsegs_root );
951        vsl_lock_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsl_lock );
952
953        // get lock on remote VSL
954        remote_queuelock_acquire( vsl_lock_xp );
955
956        // loop on vsegs in remote process VSL
957        XLIST_FOREACH( vsl_root_xp , vsl_iter_xp )
958        {
959            // get pointers on current vseg
960            xptr_t   vseg_xp  = XLIST_ELEMENT( vsl_iter_xp , vseg_t , xlist );
961            vseg_t * vseg_ptr = GET_PTR( vseg_xp );
962
963            // get current vseg base address
964            intptr_t vseg_base = (intptr_t)hal_remote_lpt( XPTR( remote_process_cxy,
965                                                                 &vseg_ptr->min ) );
966
967            if( vseg_base == base )   // found searched vseg
968            {
[641]969                // atomically increment responses counter
970                hal_atomic_add( &responses , 1 );
971
[640]972#if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1)
973if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
[641]974printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n",
975__FUNCTION__, this->process->pid, this->trdid, remote_process_cxy );
[640]976#endif
[641]977                // send RPC to remote cluster
978                rpc_send( remote_process_cxy , & rpc );
[640]979
[641]980                // exit loop on vsegs
981                break;
[640]982            }
[641]983
[640]984        }  // end of loop on vsegs
985
986#if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1)
987if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
988hal_vmm_display( remote_process_xp , false );
989#endif
990
991        // release lock on remote VSL
992        remote_queuelock_release( vsl_lock_xp );
[641]993
[640]994    }  // end of loop on process copies
995
[641]996    // release the lock protecting process copies
997    remote_queuelock_release( process_lock_xp );
998
999#if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1)
1000if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
1001printk("\n[%s] thread[%x,%x] deschedule / process %x / base %x\n",
1002__FUNCTION__, this->process->pid, this->trdid, process->pid, base );
1003#endif
1004
1005    // client thread deschedule
1006    sched_yield("blocked on rpc_vmm_delete_vseg");
1007
1008    // restore IRQs
1009    hal_restore_irq( save_sr );
1010
[640]1011#if DEBUG_VMM_GLOBAL_RESIZE_VSEG
1012cycle = (uint32_t)hal_get_cycles();
1013if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
1014printk("\n[%s] thread[%x,%x] exit for process %x / base %x / cycle %d\n",
1015__FUNCTION__, this->process->pid, this->trdid, process->pid , base, cycle );
1016#endif
1017
1018}  // end vmm_global_resize_vseg()
1019
1020////////////////////////////////////////////////
[433]1021void vmm_global_update_pte( process_t * process,
1022                            vpn_t       vpn,
1023                            uint32_t    attr,
1024                            ppn_t       ppn )
[23]1025{
[640]1026    pid_t           pid;
1027    cxy_t           owner_cxy;
1028    lpid_t          owner_lpid;
1029
[408]1030    xlist_entry_t * process_root_ptr;
1031    xptr_t          process_root_xp;
1032    xptr_t          process_iter_xp;
[23]1033
[408]1034    xptr_t          remote_process_xp;
1035    cxy_t           remote_process_cxy;
1036    process_t     * remote_process_ptr;
1037    xptr_t          remote_gpt_xp;
[23]1038
[640]1039#if DEBUG_VMM_GLOBAL_UPDATE_PTE
[433]1040uint32_t cycle = (uint32_t)hal_get_cycles();
[595]1041thread_t * this = CURRENT_THREAD;
[640]1042#endif
1043
1044
1045#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
1046if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[635]1047printk("\n[%s] thread[%x,%x] enter for process %x / vpn %x / attr %x / ppn %x / ycle %d\n",
1048__FUNCTION__, this->process->pid, this->trdid, process->pid, vpn, attr, ppn, cycle );
[433]1049#endif
1050
[640]1051    // get owner process cluster and local index
[408]1052    pid              = process->pid;
1053    owner_cxy        = CXY_FROM_PID( pid );
1054    owner_lpid       = LPID_FROM_PID( pid );
[640]1055
1056    // get extended pointer on root of process copies xlist in owner cluster
[408]1057    process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid];
1058    process_root_xp  = XPTR( owner_cxy , process_root_ptr );
[23]1059
[640]1060    // loop on process copies
[408]1061    XLIST_FOREACH( process_root_xp , process_iter_xp )
1062    {
1063        // get cluster and local pointer on remote process
1064        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
[433]1065        remote_process_ptr = GET_PTR( remote_process_xp );
[408]1066        remote_process_cxy = GET_CXY( remote_process_xp );
[407]1067
[640]1068#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
1069if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[635]1070printk("\n[%s] thread[%x,%x] handling vpn %x for process %x in cluster %x\n",
[595]1071__FUNCTION__, this->process->pid, this->trdid, vpn, process->pid, remote_process_cxy );
[433]1072#endif
1073
[408]1074        // get extended pointer on remote gpt
1075        remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt );
1076
[433]1077        // update remote GPT
1078        hal_gpt_update_pte( remote_gpt_xp, vpn, attr, ppn );
[408]1079    } 
1080
[640]1081#if DEBUG_VMM_GLOBAL_UPDATE_PTE
[433]1082cycle = (uint32_t)hal_get_cycles();
[640]1083if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[595]1084printk("\n[%s] thread[%x,%x] exit for process %x / vpn %x / cycle %d\n",
1085__FUNCTION__, this->process->pid, this->trdid, process->pid , vpn , cycle );
[433]1086#endif
1087
[640]1088#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
[635]1089hal_vmm_display( process , true );
1090#endif
1091
[433]1092}  // end vmm_global_update_pte()
1093
[408]1094///////////////////////////////////////
1095void vmm_set_cow( process_t * process )
1096{
1097    vmm_t         * vmm;
1098
1099    xlist_entry_t * process_root_ptr;
1100    xptr_t          process_root_xp;
1101    xptr_t          process_iter_xp;
1102
1103    xptr_t          remote_process_xp;
1104    cxy_t           remote_process_cxy;
1105    process_t     * remote_process_ptr;
1106    xptr_t          remote_gpt_xp;
1107
1108    xptr_t          vseg_root_xp;
1109    xptr_t          vseg_iter_xp;
1110
1111    xptr_t          vseg_xp;
1112    vseg_t        * vseg;
1113
1114    pid_t           pid;
1115    cxy_t           owner_cxy;
1116    lpid_t          owner_lpid;
1117
[635]1118    // get target process PID
1119    pid = process->pid;
1120
[438]1121#if DEBUG_VMM_SET_COW
[595]1122uint32_t   cycle = (uint32_t)hal_get_cycles();
1123thread_t * this  = CURRENT_THREAD;
[438]1124if( DEBUG_VMM_SET_COW < cycle )
[595]1125printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n",
[635]1126__FUNCTION__, this->process->pid, this->trdid, pid , cycle );
[433]1127#endif
[408]1128
[635]1129#if (DEBUG_VMM_SET_COW & 1)
1130if( DEBUG_VMM_SET_COW < cycle )
1131hal_vmm_display( process , true );
1132#endif
1133
[567]1134// check cluster is reference
[635]1135assert( (XPTR( local_cxy , process ) == process->ref_xp),
1136"local cluster must be process reference cluster\n");
[408]1137
1138    // get pointer on reference VMM
1139    vmm = &process->vmm;
1140
1141    // get extended pointer on root of process copies xlist in owner cluster
1142    owner_cxy        = CXY_FROM_PID( pid );
1143    owner_lpid       = LPID_FROM_PID( pid );
1144    process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid];
1145    process_root_xp  = XPTR( owner_cxy , process_root_ptr );
1146
1147    // get extended pointer on root of vsegs xlist from reference VMM
1148    vseg_root_xp  = XPTR( local_cxy , &vmm->vsegs_root ); 
1149
[635]1150    // loop on target process copies
[408]1151    XLIST_FOREACH( process_root_xp , process_iter_xp )
1152    {
[635]1153        // get cluster and local pointer on remote process copy
[408]1154        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
[433]1155        remote_process_ptr = GET_PTR( remote_process_xp );
[408]1156        remote_process_cxy = GET_CXY( remote_process_xp );
1157
[595]1158#if (DEBUG_VMM_SET_COW & 1)
[438]1159if( DEBUG_VMM_SET_COW < cycle )
[635]1160printk("\n[%s] thread[%x,%x] (%x) handles process %x in cluster %x\n",
1161__FUNCTION__, this->process->pid, this->trdid, this, pid, remote_process_cxy );
[433]1162#endif
[408]1163
1164        // get extended pointer on remote gpt
1165        remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt );
1166
1167        // loop on vsegs in (local) reference process VSL
1168        XLIST_FOREACH( vseg_root_xp , vseg_iter_xp )
1169        {
1170            // get pointer on vseg
1171            vseg_xp  = XLIST_ELEMENT( vseg_iter_xp , vseg_t , xlist );
[433]1172            vseg     = GET_PTR( vseg_xp );
[408]1173
1174            // get vseg type, base and size
1175            uint32_t type     = vseg->type;
1176            vpn_t    vpn_base = vseg->vpn_base;
1177            vpn_t    vpn_size = vseg->vpn_size;
1178
[595]1179#if (DEBUG_VMM_SET_COW & 1)
[438]1180if( DEBUG_VMM_SET_COW < cycle )
[635]1181printk("\n[%s] thread[%x,%x] found vseg %s / vpn_base = %x / vpn_size = %x\n",
[595]1182__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size );
[433]1183#endif
1184            // only DATA, ANON and REMOTE vsegs
[408]1185            if( (type == VSEG_TYPE_DATA)  ||
1186                (type == VSEG_TYPE_ANON)  ||
1187                (type == VSEG_TYPE_REMOTE) )
1188            {
[433]1189                vpn_t      vpn;
1190                uint32_t   attr;
1191                ppn_t      ppn;
1192                xptr_t     page_xp;
1193                cxy_t      page_cxy;
1194                page_t   * page_ptr;
1195                xptr_t     forks_xp;
[469]1196                xptr_t     lock_xp;
[433]1197
1198                // update flags in remote GPT
1199                hal_gpt_set_cow( remote_gpt_xp,
1200                                 vpn_base,
1201                                 vpn_size ); 
1202
1203                // atomically increment pending forks counter in physical pages,
[635]1204                // this is only done once, when handling the reference copy
[433]1205                if( remote_process_cxy == local_cxy )
1206                {
[635]1207
1208#if (DEBUG_VMM_SET_COW & 1)
1209if( DEBUG_VMM_SET_COW < cycle )
1210printk("\n[%s] thread[%x,%x] handles vseg %s / vpn_base = %x / vpn_size = %x\n",
1211__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size );
1212#endif
[433]1213                    // scan all pages in vseg
1214                    for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
1215                    {
1216                        // get page attributes and PPN from reference GPT
[585]1217                        hal_gpt_get_pte( remote_gpt_xp , vpn , &attr , &ppn ); 
[433]1218
1219                        // atomically update pending forks counter if page is mapped
1220                        if( attr & GPT_MAPPED )
1221                        {
[469]1222                            // get pointers and cluster on page descriptor
[433]1223                            page_xp  = ppm_ppn2page( ppn );
1224                            page_cxy = GET_CXY( page_xp );
1225                            page_ptr = GET_PTR( page_xp );
[469]1226
1227                            // get extended pointers on "forks" and "lock"
[433]1228                            forks_xp = XPTR( page_cxy , &page_ptr->forks );
[469]1229                            lock_xp  = XPTR( page_cxy , &page_ptr->lock );
1230
[567]1231                            // take lock protecting "forks" counter
1232                            remote_busylock_acquire( lock_xp );
1233
[469]1234                            // increment "forks"
[433]1235                            hal_remote_atomic_add( forks_xp , 1 );
[567]1236
1237                            // release lock protecting "forks" counter
1238                            remote_busylock_release( lock_xp );
[433]1239                        }
1240                    }   // end loop on vpn
[635]1241
1242#if (DEBUG_VMM_SET_COW & 1)
1243if( DEBUG_VMM_SET_COW < cycle )
1244printk("\n[%s] thread[%x,%x] completes vseg %s / vpn_base = %x / vpn_size = %x\n",
1245__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size );
1246#endif
[433]1247                }   // end if local
1248            }   // end if vseg type
1249        }   // end loop on vsegs
[408]1250    }   // end loop on process copies
1251 
[438]1252#if DEBUG_VMM_SET_COW
[433]1253cycle = (uint32_t)hal_get_cycles();
[438]1254if( DEBUG_VMM_SET_COW < cycle )
[595]1255printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n",
1256__FUNCTION__, this->process->pid, this->trdid, process->pid , cycle );
[433]1257#endif
[408]1258
1259}  // end vmm_set-cow()
1260
1261/////////////////////////////////////////////////
1262error_t vmm_fork_copy( process_t * child_process,
1263                       xptr_t      parent_process_xp )
1264{
1265    error_t     error;
1266    cxy_t       parent_cxy;
1267    process_t * parent_process;
1268    vmm_t     * parent_vmm;
1269    xptr_t      parent_lock_xp;
1270    vmm_t     * child_vmm;
1271    xptr_t      iter_xp;
1272    xptr_t      parent_vseg_xp;
1273    vseg_t    * parent_vseg;
1274    vseg_t    * child_vseg;
1275    uint32_t    type;
1276    vpn_t       vpn;           
1277    vpn_t       vpn_base;
1278    vpn_t       vpn_size;
1279    xptr_t      parent_root_xp;
1280    bool_t      mapped; 
1281    ppn_t       ppn;
1282
[438]1283#if DEBUG_VMM_FORK_COPY
[433]1284uint32_t cycle = (uint32_t)hal_get_cycles();
[595]1285thread_t * this = CURRENT_THREAD;
[438]1286if( DEBUG_VMM_FORK_COPY < cycle )
[595]1287printk("\n[%s] thread %x enter / cycle %d\n",
1288__FUNCTION__ , this->process->pid, this->trdid, cycle );
[433]1289#endif
[408]1290
1291    // get parent process cluster and local pointer
1292    parent_cxy     = GET_CXY( parent_process_xp );
[433]1293    parent_process = GET_PTR( parent_process_xp );
[408]1294
1295    // get local pointers on parent and child VMM
1296    parent_vmm = &parent_process->vmm; 
1297    child_vmm  = &child_process->vmm;
1298
[625]1299    // build extended pointer on parent VSL root and lock
[408]1300    parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root );
[625]1301    parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsl_lock );
[408]1302
[640]1303    // take the lock protecting the parent VSL
1304    remote_queuelock_acquire( parent_lock_xp );
[415]1305
[408]1306    // loop on parent VSL xlist
1307    XLIST_FOREACH( parent_root_xp , iter_xp )
[23]1308    {
[625]1309        // get pointers on current parent vseg
[408]1310        parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1311        parent_vseg    = GET_PTR( parent_vseg_xp );
[23]1312
[408]1313        // get vseg type
[567]1314        type = hal_remote_l32( XPTR( parent_cxy , &parent_vseg->type ) );
[408]1315       
[438]1316#if DEBUG_VMM_FORK_COPY
[433]1317cycle = (uint32_t)hal_get_cycles();
[438]1318if( DEBUG_VMM_FORK_COPY < cycle )
[595]1319printk("\n[%s] thread[%x,%x] found parent vseg %s / vpn_base = %x / cycle %d\n",
1320__FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type),
[567]1321hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]1322#endif
[23]1323
[623]1324        // all parent vsegs - but STACK and kernel vsegs - must be copied in child VSL
1325        if( (type != VSEG_TYPE_STACK) && (type != VSEG_TYPE_KCODE) &&
1326            (type != VSEG_TYPE_KDATA) && (type != VSEG_TYPE_KDEV) )
[23]1327        {
[408]1328            // allocate memory for a new child vseg
1329            child_vseg = vseg_alloc();
1330            if( child_vseg == NULL )   // release all allocated vsegs
[23]1331            {
[408]1332                vmm_destroy( child_process );
1333                printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ );
1334                return -1;
[23]1335            }
1336
[408]1337            // copy parent vseg to child vseg
1338            vseg_init_from_ref( child_vseg , parent_vseg_xp );
[23]1339
[640]1340            // build extended pointer on child VSL lock
1341            xptr_t child_lock_xp = XPTR( local_cxy , &child_vmm->vsl_lock );
[625]1342 
[640]1343            // take the child VSL lock
1344            remote_queuelock_acquire( child_lock_xp );
[625]1345
[408]1346            // register child vseg in child VSL
[611]1347            vmm_attach_vseg_to_vsl( child_vmm , child_vseg );
[407]1348
[640]1349            // release the child VSL lock
1350            remote_queuelock_release( child_lock_xp );
[625]1351
[438]1352#if DEBUG_VMM_FORK_COPY
[433]1353cycle = (uint32_t)hal_get_cycles();
[438]1354if( DEBUG_VMM_FORK_COPY < cycle )
[595]1355printk("\n[%s] thread[%x,%x] copied vseg %s / vpn_base = %x to child VSL / cycle %d\n",
1356__FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type),
[567]1357hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]1358#endif
[625]1359            // copy DATA, ANON, REMOTE, FILE parent GPT entries to child GPT
[408]1360            if( type != VSEG_TYPE_CODE )
1361            {
[625]1362                // activate the COW for DATA, ANON, REMOTE vsegs only
[635]1363                // cow = ( type != VSEG_TYPE_FILE );
[23]1364
[408]1365                vpn_base = child_vseg->vpn_base;
1366                vpn_size = child_vseg->vpn_size;
[23]1367
[408]1368                // scan pages in parent vseg
1369                for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
1370                {
1371                    error = hal_gpt_pte_copy( &child_vmm->gpt,
[625]1372                                              vpn,
[408]1373                                              XPTR( parent_cxy , &parent_vmm->gpt ),
1374                                              vpn,
[635]1375                                              false,      // does not handle COW flag
1376                                              &ppn,       // unused
1377                                              &mapped );  // unused
[408]1378                    if( error )
1379                    {
1380                        vmm_destroy( child_process );
1381                        printk("\n[ERROR] in %s : cannot copy GPT\n", __FUNCTION__ );
1382                        return -1;
1383                    }
1384
[438]1385#if DEBUG_VMM_FORK_COPY
[433]1386cycle = (uint32_t)hal_get_cycles();
[438]1387if( DEBUG_VMM_FORK_COPY < cycle )
[595]1388printk("\n[%s] thread[%x,%x] copied vpn %x to child GPT / cycle %d\n",
1389__FUNCTION__ , this->process->pid, this->trdid , vpn , cycle );
[433]1390#endif
[408]1391                }
1392            }   // end if no code & no stack
1393        }   // end if no stack
1394    }   // end loop on vsegs
1395
[567]1396    // release the parent VSL lock in read mode
[640]1397    remote_queuelock_release( parent_lock_xp );
[408]1398
[651]1399/* deprecated [AG] : this is already done by the vmm_user_init() funcfion
1400
[408]1401    // initialize the child VMM STACK allocator
[651]1402    vmm_stack_init( child_vmm );
[408]1403
1404    // initialize the child VMM MMAP allocator
[651]1405    vmm_mmap_init( child_vmm );
[23]1406
[178]1407    // initialize instrumentation counters
[635]1408        child_vmm->false_pgfault_nr    = 0;
1409        child_vmm->local_pgfault_nr    = 0;
1410        child_vmm->global_pgfault_nr   = 0;
1411        child_vmm->false_pgfault_cost  = 0;
1412        child_vmm->local_pgfault_cost  = 0;
1413        child_vmm->global_pgfault_cost = 0;
[651]1414*/
[408]1415    // copy base addresses from parent VMM to child VMM
1416    child_vmm->args_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->args_vpn_base));
1417    child_vmm->envs_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->envs_vpn_base));
1418    child_vmm->heap_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->heap_vpn_base));
1419    child_vmm->code_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->code_vpn_base));
1420    child_vmm->data_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->data_vpn_base));
[23]1421
[408]1422    child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point));
[23]1423
[124]1424    hal_fence();
[23]1425
[438]1426#if DEBUG_VMM_FORK_COPY
[433]1427cycle = (uint32_t)hal_get_cycles();
[438]1428if( DEBUG_VMM_FORK_COPY < cycle )
[595]1429printk("\n[%s] thread[%x,%x] exit successfully / cycle %d\n",
1430__FUNCTION__ , this->process->pid, this->trdid , cycle );
[433]1431#endif
1432
[23]1433    return 0;
1434
[408]1435}  // vmm_fork_copy()
[204]1436
[1]1437///////////////////////////////////////
1438void vmm_destroy( process_t * process )
1439{
[408]1440    xptr_t   vseg_xp;
[1]1441        vseg_t * vseg;
1442
[438]1443#if DEBUG_VMM_DESTROY
[635]1444uint32_t   cycle = (uint32_t)hal_get_cycles();
1445thread_t * this  = CURRENT_THREAD;
[438]1446if( DEBUG_VMM_DESTROY < cycle )
[595]1447printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
1448__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]1449#endif
[416]1450
[438]1451#if (DEBUG_VMM_DESTROY & 1 )
[443]1452if( DEBUG_VMM_DESTROY < cycle )
[635]1453hal_vmm_display( XPTR( local_cxy, process ) , true );
[437]1454#endif
1455
[433]1456    // get pointer on local VMM
[1]1457    vmm_t  * vmm = &process->vmm;
1458
[625]1459    // build extended pointer on VSL root, VSL lock and GPT lock
1460    xptr_t   vsl_root_xp = XPTR( local_cxy , &vmm->vsegs_root );
1461    xptr_t   vsl_lock_xp = XPTR( local_cxy , &vmm->vsl_lock );
[408]1462
[625]1463    // take the VSL lock
[640]1464    remote_queuelock_acquire( vsl_lock_xp );
[625]1465
[611]1466    // scan the VSL to delete all registered vsegs
[625]1467    // (we don't use a FOREACH in case of item deletion)
1468    xptr_t  iter_xp;
1469    xptr_t  next_xp;
1470        for( iter_xp = hal_remote_l64( vsl_root_xp ) ; 
1471         iter_xp != vsl_root_xp ;
1472         iter_xp = next_xp )
[1]1473        {
[625]1474        // save extended pointer on next item in xlist
1475        next_xp = hal_remote_l64( iter_xp );
[409]1476
[625]1477        // get pointers on current vseg in VSL
1478        vseg_xp   = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
1479        vseg      = GET_PTR( vseg_xp );
1480
[611]1481        // delete vseg and release physical pages
[625]1482        vmm_remove_vseg( process , vseg );
[409]1483
[443]1484#if( DEBUG_VMM_DESTROY & 1 )
1485if( DEBUG_VMM_DESTROY < cycle )
[611]1486printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n",
[443]1487__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1488#endif
1489
[1]1490        }
1491
[625]1492    // release the VSL lock
[640]1493    remote_queuelock_release( vsl_lock_xp );
[625]1494
[651]1495    // remove all registered MMAP vsegs from free_lists in MMAP allocator
[1]1496    uint32_t i;
[651]1497    for( i = 0 ; i <= CONFIG_VMM_HEAP_MAX_ORDER ; i++ )
[1]1498    {
[651]1499        // build extended pointer on free list root
1500        xptr_t root_xp = XPTR( local_cxy , &vmm->mmap_mgr.free_list_root[i] );
[625]1501 
1502        // scan zombi_list[i]
1503            while( !xlist_is_empty( root_xp ) )
[1]1504            {
[625]1505                    vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
1506            vseg    = GET_PTR( vseg_xp );
[443]1507
1508#if( DEBUG_VMM_DESTROY & 1 )
1509if( DEBUG_VMM_DESTROY < cycle )
[595]1510printk("\n[%s] found zombi vseg / vpn_base %x / vpn_size %d\n",
[443]1511__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1512#endif
[611]1513            // clean vseg descriptor
1514            vseg->vmm = NULL;
1515
[625]1516            // remove vseg from  zombi_list
[611]1517            xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
1518
1519                    // release vseg descriptor
[1]1520            vseg_free( vseg );
[443]1521
1522#if( DEBUG_VMM_DESTROY & 1 )
1523if( DEBUG_VMM_DESTROY < cycle )
[595]1524printk("\n[%s] zombi vseg released / vpn_base %x / vpn_size %d\n",
[443]1525__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1526#endif
[1]1527            }
1528    }
1529
[409]1530    // release memory allocated to the GPT itself
[1]1531    hal_gpt_destroy( &vmm->gpt );
1532
[438]1533#if DEBUG_VMM_DESTROY
[433]1534cycle = (uint32_t)hal_get_cycles();
[438]1535if( DEBUG_VMM_DESTROY < cycle )
[595]1536printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
1537__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle );
[433]1538#endif
[416]1539
[204]1540}  // end vmm_destroy()
1541
[1]1542/////////////////////////////////////////////////
1543vseg_t * vmm_check_conflict( process_t * process,
[21]1544                             vpn_t       vpn_base,
[1]1545                             vpn_t       vpn_size )
1546{
1547    vmm_t        * vmm = &process->vmm;
[408]1548
1549    // scan the VSL
[1]1550        vseg_t       * vseg;
[408]1551    xptr_t         iter_xp;
1552    xptr_t         vseg_xp;
1553    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[1]1554
[408]1555        XLIST_FOREACH( root_xp , iter_xp )
[1]1556        {
[408]1557                vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1558        vseg    = GET_PTR( vseg_xp );
[204]1559
[21]1560                if( ((vpn_base + vpn_size) > vseg->vpn_base) &&
1561             (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg;
[1]1562        }
1563    return NULL;
1564
[204]1565}  // end vmm_check_conflict()
1566
[407]1567////////////////////////////////////////////////
1568vseg_t * vmm_create_vseg( process_t   * process,
1569                              vseg_type_t   type,
[635]1570                          intptr_t      base,         // ltid for VSEG_TYPE_STACK
[407]1571                              uint32_t      size,
1572                          uint32_t      file_offset,
1573                          uint32_t      file_size,
1574                          xptr_t        mapper_xp,
1575                          cxy_t         cxy )
[1]1576{
[651]1577    vseg_t     * vseg;          // pointer on allocated vseg descriptor
[1]1578
[640]1579#if DEBUG_VMM_CREATE_VSEG
1580thread_t * this  = CURRENT_THREAD;
1581uint32_t   cycle;
1582#endif
1583
[635]1584#if (DEBUG_VMM_CREATE_VSEG & 1)
[640]1585cycle = (uint32_t)hal_get_cycles();
[438]1586if( DEBUG_VMM_CREATE_VSEG < cycle )
[635]1587printk("\n[%s] thread[%x,%x] enter / process %x / %s / base %x / cxy %x / cycle %d\n",
1588__FUNCTION__, this->process->pid, this->trdid,
1589process->pid, vseg_type_str(type), base, cxy, cycle );
[433]1590#endif
[21]1591
[407]1592    // get pointer on VMM
1593        vmm_t * vmm    = &process->vmm;
[21]1594
[651]1595    // allocate a vseg descriptor and initialize it, depending on type
1596    // we use specific allocators for "stack" and "mmap" types
[595]1597
[651]1598    /////////////////////////////
[1]1599    if( type == VSEG_TYPE_STACK )
1600    {
[651]1601        // get vseg from STACK allocator
1602        vseg = vmm_stack_alloc( vmm , base );    // base == ltid
1603       
1604        if( vseg == NULL )
1605        {
1606            printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n",
1607            __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy );
1608            return NULL;
1609        }
[1]1610
[651]1611        // initialize vseg
1612        vseg->type = type;
1613        vseg->vmm  = vmm;
1614        vseg->min  = vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT;
1615        vseg->max  = vseg->min + (vseg->vpn_size << CONFIG_PPM_PAGE_SHIFT);
1616        vseg->cxy  = cxy;
1617
1618        vseg_init_flags( vseg , type );
[1]1619    }
[651]1620    /////////////////////////////////
[595]1621    else if( type == VSEG_TYPE_FILE )
1622    {
[651]1623        // compute page index (in mapper) for first and last byte
[595]1624        vpn_t    vpn_min    = file_offset >> CONFIG_PPM_PAGE_SHIFT;
1625        vpn_t    vpn_max    = (file_offset + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
1626
[651]1627        // compute offset in first page and number of pages
[595]1628        uint32_t offset = file_offset & CONFIG_PPM_PAGE_MASK;
1629        vpn_t    npages      = vpn_max - vpn_min + 1;
1630
[651]1631        // get vseg from MMAP allocator
1632        vseg = vmm_mmap_alloc( vmm , npages );
1633
1634        if( vseg == NULL )
[595]1635        {
[651]1636            printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n",
1637            __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy );
[595]1638            return NULL;
1639        }
1640
[651]1641        // initialize vseg
1642        vseg->type        = type;
1643        vseg->vmm         = vmm;
1644        vseg->min         = (vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT) + offset; 
1645        vseg->max         = vseg->min + size;
1646        vseg->file_offset = file_offset;
1647        vseg->file_size   = file_size;
1648        vseg->mapper_xp   = mapper_xp;
1649        vseg->cxy         = cxy;
1650
1651        vseg_init_flags( vseg , type );
[595]1652    }
[651]1653    /////////////////////////////////////////////////////////////////
1654    else if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_REMOTE) )
[1]1655    {
[595]1656        // compute number of required pages in virtual space
1657        vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
1658        if( size & CONFIG_PPM_PAGE_MASK) npages++;
1659       
[651]1660        // allocate vseg from MMAP allocator
1661        vseg = vmm_mmap_alloc( vmm , npages );
1662
1663        if( vseg == NULL )
[1]1664        {
[651]1665            printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n",
1666            __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy );
[1]1667            return NULL;
1668        }
1669
[651]1670        // initialize vseg
1671        vseg->type = type;
1672        vseg->vmm  = vmm;
1673        vseg->min  = vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT;
1674        vseg->max  = vseg->min + (vseg->vpn_size << CONFIG_PPM_PAGE_SHIFT);
1675        vseg->cxy  = cxy;
1676
1677        vseg_init_flags( vseg , type );
[1]1678    }
[651]1679    /////////////////////////////////////////////////////////////////
[623]1680    else    // VSEG_TYPE_DATA, VSEG_TYPE_CODE or KERNEL vseg
[1]1681    {
[204]1682        uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT;
1683        uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
1684
[651]1685        // allocate vseg descriptor
1686            vseg = vseg_alloc();
1687
1688            if( vseg == NULL )
1689            {
1690            printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n",
1691            __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy );
1692            return NULL;
1693            }
1694        // initialize vseg
1695        vseg->type        = type;
1696        vseg->vmm         = vmm;
1697        vseg->min         = base;
1698        vseg->max         = base + size;
1699        vseg->vpn_base    = base >> CONFIG_PPM_PAGE_SHIFT;
1700        vseg->vpn_size    = vpn_max - vpn_min + 1;
1701        vseg->file_offset = file_offset;
1702        vseg->file_size   = file_size;
1703        vseg->mapper_xp   = mapper_xp;
1704        vseg->cxy         = cxy;
1705
1706        vseg_init_flags( vseg , type );
[1]1707    }
1708
1709    // check collisions
[651]1710    vseg_t * existing_vseg = vmm_check_conflict( process , vseg->vpn_base , vseg->vpn_size );
[624]1711
[651]1712    if( existing_vseg != NULL )
[1]1713    {
[651]1714        printk("\n[ERROR] in %s for process %x : new vseg %s [vpn_base %x / vpn_size %x]\n"
1715               "        overlap existing vseg %s [vpn_base %x / vpn_size %x]\n",
1716        __FUNCTION__ , process->pid, vseg_type_str(vseg->type), vseg->vpn_base, vseg->vpn_size, 
1717        vseg_type_str(existing_vseg->type), existing_vseg->vpn_base, existing_vseg->vpn_size );
1718        vseg_free( vseg );
[1]1719        return NULL;
1720    }
1721
[625]1722    // build extended pointer on VSL lock
1723    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsl_lock );
1724 
1725    // take the VSL lock in write mode
[640]1726    remote_queuelock_acquire( lock_xp );
[625]1727
[408]1728    // attach vseg to VSL
[611]1729        vmm_attach_vseg_to_vsl( vmm , vseg );
[1]1730
[625]1731    // release the VSL lock
[640]1732    remote_queuelock_release( lock_xp );
[625]1733
[651]1734#if DEBUG_VMM_CREATE_VSEG
[433]1735cycle = (uint32_t)hal_get_cycles();
[651]1736if( DEBUG_VMM_CREATE_VSEG < cycle )
1737printk("\n[%s] thread[%x,%x] exit / %s / vpn_base %x / vpn_size %x / cycle %d\n",
1738__FUNCTION__, this->process->pid, this->trdid,
1739vseg_type_str(type), vseg->vpn_base, vseg->vpn_size, cycle );
[433]1740#endif
[21]1741
[1]1742        return vseg;
1743
[406]1744}  // vmm_create_vseg()
1745
[640]1746////////////////////////////////////////////////////////////////////////////////////////////
[656]1747// This static function is called by the vmm_remove_vseg() and vmm_resize_vseg() functions
1748// to update the physical page descriptor identified by the <ppn> argument.
1749// It decrements the refcount, set the dirty bit when required, and releases the physical
1750// page to kmem depending on the vseg type.
1751// - KERNEL : refcount decremented / not released to kmem    / dirty bit not set
1752// - FILE   : refcount decremented / not released to kmem    / dirty bit set when required.
1753// - CODE   : refcount decremented / released to kmem        / dirty bit not set.
1754// - STAK   : refcount decremented / released to kmem        / dirty bit not set.
1755// - DATA   : refcount decremented / released to kmem if ref / dirty bit not set.
1756// - MMAP   : refcount decremented / released to kmem if ref / dirty bit not set.
[640]1757////////////////////////////////////////////////////////////////////////////////////////////
1758// @ process  : local pointer on process.
1759// @ vseg     : local pointer on vseg.
1760// @ ppn      : released pysical page index.
[656]1761// @ dirty    : set the dirty bit in page descriptor when non zero.
[640]1762////////////////////////////////////////////////////////////////////////////////////////////
1763static void vmm_ppn_release( process_t * process,
1764                             vseg_t    * vseg,
[656]1765                             ppn_t       ppn,
1766                             uint32_t    dirty )
[640]1767{
[656]1768    bool_t do_kmem_release;
[625]1769
[640]1770    // get vseg type
1771    vseg_type_t type = vseg->type;
1772
[656]1773    // compute is_ref <=> this vseg is the reference vseg
[640]1774    bool_t is_ref = (GET_CXY( process->ref_xp ) == local_cxy);
1775
1776    // get pointers on physical page descriptor
1777    xptr_t   page_xp  = ppm_ppn2page( ppn );
1778    cxy_t    page_cxy = GET_CXY( page_xp );
1779    page_t * page_ptr = GET_PTR( page_xp );
1780
1781    // decrement page refcount
1782    xptr_t count_xp = XPTR( page_cxy , &page_ptr->refcount );
1783    hal_remote_atomic_add( count_xp , -1 );
1784
[656]1785    // compute the do_kmem_release condition depending on vseg type
1786    if( (type == VSEG_TYPE_KCODE) || 
[640]1787        (type == VSEG_TYPE_KDATA) || 
1788        (type == VSEG_TYPE_KDEV) )           
1789    {
[656]1790        // no physical page release for KERNEL
1791        do_kmem_release = false;
[640]1792    }
[656]1793    else if( type == VSEG_TYPE_FILE )
1794    {
1795        // no physical page release for KERNEL
1796        do_kmem_release = false;
1797
1798        // set dirty bit if required
1799        if( dirty ) ppm_page_do_dirty( page_xp );
1800    }   
[640]1801    else if( (type == VSEG_TYPE_CODE)  ||
1802             (type == VSEG_TYPE_STACK) ) 
1803    {
1804        // always release physical page for private vsegs
[656]1805        do_kmem_release = true;
[640]1806    }
1807    else if( (type == VSEG_TYPE_ANON)  ||
1808             (type == VSEG_TYPE_REMOTE) )
1809    {
1810        // release physical page if reference cluster
[656]1811        do_kmem_release = is_ref;
[640]1812    }
1813    else if( is_ref )  // vseg_type == DATA in reference cluster
1814    {
1815        // get extended pointers on forks and lock field in page descriptor
1816        xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks );
1817        xptr_t lock_xp  = XPTR( page_cxy , &page_ptr->lock );
1818
1819        // take lock protecting "forks" counter
1820        remote_busylock_acquire( lock_xp );
1821
1822        // get number of pending forks from page descriptor
1823        uint32_t forks = hal_remote_l32( forks_xp );
1824
1825        // decrement pending forks counter if required
1826        if( forks )  hal_remote_atomic_add( forks_xp , -1 );
1827
1828        // release lock protecting "forks" counter
1829        remote_busylock_release( lock_xp );
1830
1831        // release physical page if forks == 0
[656]1832        do_kmem_release = (forks == 0); 
[640]1833    }
1834    else              // vseg_type == DATA not in reference cluster
1835    {
1836        // no physical page release if not in reference cluster
[656]1837        do_kmem_release = false;
[640]1838    }
1839
1840    // release physical page to relevant kmem when required
[656]1841    if( do_kmem_release )
[640]1842    {
[656]1843        kmem_req_t req;
1844        req.type = KMEM_PPM;
1845        req.ptr  = GET_PTR( ppm_ppn2base( ppn ) );
[640]1846
[656]1847        kmem_remote_free( page_cxy , &req );
1848
[640]1849#if DEBUG_VMM_PPN_RELEASE
1850thread_t * this = CURRENT_THREAD;
1851if( DEBUG_VMM_PPN_RELEASE < cycle )
1852printk("\n[%s] thread[%x,%x] released ppn %x to kmem\n",
1853__FUNCTION__, this->process->pid, this->trdid, ppn );
1854#endif
1855
1856    }
1857} // end vmm_ppn_release()
1858
[625]1859//////////////////////////////////////////
1860void vmm_remove_vseg( process_t * process,
1861                      vseg_t    * vseg )
[1]1862{
[625]1863    uint32_t    vseg_type;  // vseg type
[21]1864    vpn_t       vpn;        // VPN of current PTE
1865    vpn_t       vpn_min;    // VPN of first PTE
[1]1866    vpn_t       vpn_max;    // VPN of last PTE (excluded)
[409]1867    ppn_t       ppn;        // current PTE ppn value
1868    uint32_t    attr;       // current PTE attributes
[1]1869
[625]1870// check arguments
1871assert( (process != NULL), "process argument is NULL" );
1872assert( (vseg    != NULL), "vseg argument is NULL" );
[409]1873
[625]1874    // get pointers on local process VMM
[640]1875    vmm_t * vmm = &process->vmm;
[611]1876
[629]1877    // build extended pointer on GPT
[640]1878    xptr_t gpt_xp = XPTR( local_cxy , &vmm->gpt );
[629]1879
[623]1880    // get relevant vseg infos
[624]1881    vseg_type = vseg->type;
1882    vpn_min   = vseg->vpn_base;
1883    vpn_max   = vpn_min + vseg->vpn_size;
[623]1884
[625]1885#if DEBUG_VMM_REMOVE_VSEG
1886uint32_t   cycle = (uint32_t)hal_get_cycles();
1887thread_t * this  = CURRENT_THREAD;
[640]1888#endif
1889
1890#if (DEBUG_VMM_REMOVE_VSEG & 1 )
[625]1891if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1892printk("\n[%s] thread[%x,%x] enters / process %x / type %s / base %x / cycle %d\n",
[625]1893__FUNCTION__, this->process->pid, this->trdid, 
1894process->pid, vseg_type_str(vseg->type), vseg->min, cycle );
1895#endif
1896
[640]1897    // loop on PTEs in GPT to unmap all mapped PTE
[1]1898        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
1899    {
[625]1900        // get ppn and attr
[629]1901        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[409]1902
[625]1903        if( attr & GPT_MAPPED )  // PTE is mapped
[409]1904        { 
[437]1905
[625]1906#if( DEBUG_VMM_REMOVE_VSEG & 1 )
1907if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1908printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / type %s\n",
[640]1909__FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) );
[437]1910#endif
[585]1911            // unmap GPT entry in local GPT
[629]1912            hal_gpt_reset_pte( gpt_xp , vpn );
[409]1913
[656]1914            // release physical page depending on vseg type
1915            vmm_ppn_release( process , vseg , ppn , attr & GPT_DIRTY );
[409]1916        }
[1]1917    }
[433]1918
[625]1919    // remove vseg from VSL
[611]1920    vmm_detach_vseg_from_vsl( vmm , vseg );
1921
[625]1922    // release vseg descriptor depending on vseg type
1923    if( vseg_type == VSEG_TYPE_STACK )
1924    {
1925        // release slot to local stack allocator
1926        vmm_stack_free( vmm , vseg );
1927    }
1928    else if( (vseg_type == VSEG_TYPE_ANON) || 
1929             (vseg_type == VSEG_TYPE_FILE) || 
1930             (vseg_type == VSEG_TYPE_REMOTE) ) 
1931    {
1932        // release vseg to local mmap allocator
1933        vmm_mmap_free( vmm , vseg );
1934    }
1935    else
1936    {
1937        // release vseg descriptor to local kmem
1938        vseg_free( vseg );
1939    }
1940
1941#if DEBUG_VMM_REMOVE_VSEG
[433]1942cycle = (uint32_t)hal_get_cycles();
[625]1943if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1944printk("\n[%s] thread[%x,%x] exit / process %x / type %s / base %x / cycle %d\n",
[625]1945__FUNCTION__, this->process->pid, this->trdid, 
1946process->pid, vseg_type_str(vseg->type), vseg->min, cycle );
[433]1947#endif
1948
[625]1949}  // end vmm_remove_vseg()
[1]1950
[611]1951/////////////////////////////////////////////
[640]1952void vmm_resize_vseg( process_t * process,
1953                      vseg_t    * vseg,
1954                      intptr_t    new_base,
1955                      intptr_t    new_size )
[406]1956{
[640]1957    vpn_t     vpn;
1958    ppn_t     ppn;
1959    uint32_t  attr;
[406]1960
[640]1961// check arguments
1962assert( (process != NULL), "process argument is NULL" );
1963assert( (vseg    != NULL), "vseg argument is NULL" );
[406]1964
[623]1965#if DEBUG_VMM_RESIZE_VSEG
1966uint32_t   cycle = (uint32_t)hal_get_cycles();
1967thread_t * this  = CURRENT_THREAD;
[640]1968#endif
1969
1970#if (DEBUG_VMM_RESIZE_VSEG & 1)
[623]1971if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]1972printk("\n[%s] thread[%x,%x] enter / process %x / %s / base %x / cycle %d\n",
1973__FUNCTION__, this->process->pid, this->trdid, 
1974process->pid, vseg_type_str(vseg->type), old_base, cycle );
[623]1975#endif
1976
[640]1977    // get existing vseg vpn_min and vpn_max
1978    vpn_t     old_vpn_min = vseg->vpn_base;
1979    vpn_t     old_vpn_max = old_vpn_min + vseg->vpn_size - 1;
[1]1980
[640]1981    // compute new vseg vpn_min & vpn_max 
1982    intptr_t min          = new_base;
1983    intptr_t max          = new_base + new_size;
1984    vpn_t    new_vpn_min  = min >> CONFIG_PPM_PAGE_SHIFT;
1985    vpn_t    new_vpn_max  = (max - 1) >> CONFIG_PPM_PAGE_SHIFT;
[1]1986
[640]1987    // build extended pointer on GPT
1988    xptr_t gpt_xp = XPTR( local_cxy , &process->vmm.gpt );
[1]1989
[657]1990    // loop on PTEs in GPT to unmap PTE if (old_vpn_min <= vpn < new_vpn_min)
[640]1991        for( vpn = old_vpn_min ; vpn < new_vpn_min ; vpn++ )
[623]1992    {
[640]1993        // get ppn and attr
1994        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[21]1995
[640]1996        if( attr & GPT_MAPPED )  // PTE is mapped
1997        { 
[623]1998
1999#if( DEBUG_VMM_RESIZE_VSEG & 1 )
2000if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]2001printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / %s",
2002__FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) );
[623]2003#endif
[640]2004            // unmap GPT entry
2005            hal_gpt_reset_pte( gpt_xp , vpn );
[623]2006
[640]2007            // release physical page when required
[656]2008            vmm_ppn_release( process , vseg , ppn , attr & GPT_DIRTY );
[640]2009        }
[1]2010    }
[640]2011
2012    // loop on PTEs in GPT to unmap PTE if (new vpn_max <= vpn < old_vpn_max)
2013        for( vpn = new_vpn_max ; vpn < old_vpn_max ; vpn++ )
[1]2014    {
[640]2015        // get ppn and attr
2016        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[623]2017
[640]2018        if( attr & GPT_MAPPED )  // PTE is mapped
2019        { 
2020
[641]2021#if( DEBUG_VMM_RESIZE_VSEG & 1 )
[623]2022if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]2023printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / %s",
2024__FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) );
[623]2025#endif
[640]2026            // unmap GPT entry in local GPT
2027            hal_gpt_reset_pte( gpt_xp , vpn );
[406]2028
[640]2029            // release physical page when required
[656]2030            vmm_ppn_release( process , vseg , ppn , attr & GPT_DIRTY );
[640]2031        }
[1]2032    }
[623]2033
[640]2034    // resize vseg in VSL
2035    vseg->min      = min;
2036    vseg->max      = max;
2037    vseg->vpn_base = new_vpn_min;
2038    vseg->vpn_size = new_vpn_max - new_vpn_min + 1;
2039
2040#if DEBUG_VMM_RESIZE_VSEG
2041cycle = (uint32_t)hal_get_cycles();
[623]2042if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]2043printk("[%s] thread[%x,%x] exit / process %x / %s / base %x / cycle %d\n",
2044__FUNCTION__, this->process->pid, this->trdid, 
2045process->pid, vseg_type_str(vseg->type), vseg->min, cycle );
[623]2046#endif
[406]2047
[640]2048}  // end vmm_resize_vseg
[623]2049
[640]2050/////////////////////////////////////////////////////////////////////////////////////////////
2051// This static function is called twice by the vmm_get_vseg() function.
2052// It scan the - possibly remote - VSL defined by the <vmm_xp> argument to find the vseg
2053// containing a given virtual address <vaddr>. It uses remote accesses to access the remote
2054// VSL if required. The VSL lock protecting the VSL must be taken by the caller.
2055/////////////////////////////////////////////////////////////////////////////////////////////
2056// @ vmm_xp  : extended pointer on the process VMM.
2057// @ vaddr   : virtual address.
2058// @ return local pointer on remote vseg if success / return NULL if not found.
2059/////////////////////////////////////////////////////////////////////////////////////////////
2060static vseg_t * vmm_vseg_from_vaddr( xptr_t     vmm_xp,
2061                                     intptr_t   vaddr )
2062{
2063    xptr_t   iter_xp;
2064    xptr_t   vseg_xp;
2065    vseg_t * vseg;
2066    intptr_t min;
2067    intptr_t max;
[623]2068
[640]2069    // get cluster and local pointer on target VMM
2070    vmm_t * vmm_ptr = GET_PTR( vmm_xp );
2071    cxy_t   vmm_cxy = GET_CXY( vmm_xp );
[623]2072
[640]2073    // build extended pointer on VSL root
2074    xptr_t root_xp = XPTR( vmm_cxy , &vmm_ptr->vsegs_root );
[406]2075
[640]2076    // scan the list of vsegs in VSL
2077    XLIST_FOREACH( root_xp , iter_xp )
2078    {
2079        vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
2080        vseg    = GET_PTR( vseg_xp );
[406]2081
[640]2082        min = hal_remote_l32( XPTR( vmm_cxy , &vseg->min ) );
2083        max = hal_remote_l32( XPTR( vmm_cxy , &vseg->max ) );
[407]2084
[640]2085        // return success when match
2086        if( (vaddr >= min) && (vaddr < max) ) return vseg;
[1]2087    }
2088
[640]2089    // return failure
2090    return NULL;
[1]2091
[640]2092}  // end vmm_vseg_from_vaddr()
[1]2093
2094///////////////////////////////////////////
[388]2095error_t  vmm_get_vseg( process_t * process,
[394]2096                       intptr_t    vaddr,
[388]2097                       vseg_t   ** found_vseg )
[1]2098{
[640]2099    xptr_t    loc_lock_xp;     // extended pointer on local VSL lock
2100    xptr_t    ref_lock_xp;     // extended pointer on reference VSL lock
2101    vseg_t  * loc_vseg;        // local pointer on local vseg
2102    vseg_t  * ref_vseg;        // local pointer on reference vseg
[1]2103
[640]2104    // build extended pointer on local VSL lock
2105    loc_lock_xp = XPTR( local_cxy , &process->vmm.vsl_lock );
2106     
2107    // get local VSL lock
2108    remote_queuelock_acquire( loc_lock_xp );
[1]2109
[665]2110    // try to get vseg from local VSL
[640]2111    loc_vseg = vmm_vseg_from_vaddr( XPTR( local_cxy, &process->vmm ) , vaddr );
[440]2112
[640]2113    if (loc_vseg == NULL)   // vseg not found => access reference VSL
2114    {
[388]2115        // get extended pointer on reference process
2116        xptr_t ref_xp = process->ref_xp;
[1]2117
[640]2118        // get cluster and local pointer on reference process
[388]2119        cxy_t       ref_cxy = GET_CXY( ref_xp );
[433]2120        process_t * ref_ptr = GET_PTR( ref_xp );
[388]2121
[665]2122        if( ref_cxy == local_cxy )    // local is ref => return error
[640]2123        {
2124            printk("\n[ERROR] in %s : vaddr %x in process %x out of segment\n",
2125            __FUNCTION__, vaddr, process->pid );
[388]2126
[665]2127            // release local VSL lock
2128            remote_queuelock_release( loc_lock_xp );
[388]2129
[640]2130            return -1;
2131        }
[665]2132        else                          // ref != local => access ref VSL                     
[640]2133        {
[665]2134            // build extended pointer on reference VSL lock
2135            ref_lock_xp = XPTR( ref_cxy , &ref_ptr->vmm.vsl_lock );
2136     
2137            // get reference VSL lock
2138            remote_queuelock_acquire( ref_lock_xp );
[625]2139
[665]2140            // try to get vseg from reference VSL
2141            ref_vseg = vmm_vseg_from_vaddr( XPTR( ref_cxy , &ref_ptr->vmm ) , vaddr );
2142
2143            if( ref_vseg == NULL )  // vseg not found => return error
[640]2144            {
[665]2145                // release both VSL locks
2146                remote_queuelock_release( loc_lock_xp );
2147                remote_queuelock_release( ref_lock_xp );
2148
2149                printk("\n[ERROR] in %s : vaddr %x in process %x out of segment\n",
[640]2150                __FUNCTION__, vaddr, process->pid );
[595]2151
[640]2152                return -1;
2153            }
[665]2154            else                    // vseg found => try to update local VSL
[640]2155            {
[665]2156                // allocate a local vseg descriptor
2157                loc_vseg = vseg_alloc();
[640]2158
[665]2159                if( loc_vseg == NULL )   // no memory => return error
2160                {
2161                    printk("\n[ERROR] in %s : vaddr %x in process %x / no memory\n",
2162                    __FUNCTION__, vaddr, process->pid );
[640]2163
[665]2164                    // release both VSL locks
2165                    remote_queuelock_release( ref_lock_xp );
2166                    remote_queuelock_release( loc_lock_xp );
[640]2167
[665]2168                    return -1;
2169                }
2170                else                     // update local VSL and return success
2171                {
2172                    // initialize local vseg
2173                    vseg_init_from_ref( loc_vseg , XPTR( ref_cxy , ref_vseg ) );
2174
2175                    // register local vseg in local VSL
2176                    vmm_attach_vseg_to_vsl( &process->vmm , loc_vseg );
2177
2178                    // release both VSL locks
2179                    remote_queuelock_release( ref_lock_xp );
2180                    remote_queuelock_release( loc_lock_xp );
2181
2182                    *found_vseg = loc_vseg;
2183                    return 0;
2184                }
[640]2185            }
2186        }
2187    }
2188    else                        // vseg found in local VSL => return success
2189    {
[665]2190        // release local VSL lock
[640]2191        remote_queuelock_release( loc_lock_xp );
2192
2193        *found_vseg = loc_vseg;
2194        return 0;
2195    }
[388]2196}  // end vmm_get_vseg()
2197
[407]2198//////////////////////////////////////////////////////////////////////////////////////
2199// This static function compute the target cluster to allocate a physical page
[632]2200// for a given <vpn> in a given <vseg>, allocates the page and returns an extended
2201// pointer on the allocated page descriptor.
[407]2202// The vseg cannot have the FILE type.
2203//////////////////////////////////////////////////////////////////////////////////////
[640]2204// @ vseg   : local pointer on vseg.
2205// @ vpn    : unmapped vpn.
[656]2206// @ return an extended pointer on the allocated page descriptor.
[640]2207//////////////////////////////////////////////////////////////////////////////////////
[407]2208static xptr_t vmm_page_allocate( vseg_t * vseg,
2209                                 vpn_t    vpn )
2210{
[433]2211
[632]2212#if DEBUG_VMM_PAGE_ALLOCATE
[619]2213uint32_t   cycle   = (uint32_t)hal_get_cycles();
2214thread_t * this    = CURRENT_THREAD;
[632]2215if( DEBUG_VMM_PAGE_ALLOCATE < cycle )
[595]2216printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n",
2217__FUNCTION__ , this->process->pid, this->trdid, vpn, cycle );
[433]2218#endif
2219
[632]2220    xptr_t       page_xp;
[407]2221    cxy_t        page_cxy;
[577]2222    uint32_t     index;
[407]2223
[577]2224    uint32_t     type   = vseg->type;
2225    uint32_t     flags  = vseg->flags;
2226    uint32_t     x_size = LOCAL_CLUSTER->x_size;
2227    uint32_t     y_size = LOCAL_CLUSTER->y_size;
[407]2228
[567]2229// check vseg type
2230assert( ( type != VSEG_TYPE_FILE ) , "illegal vseg type\n" );
[407]2231
[656]2232    // compute target cluster identifier
[407]2233    if( flags & VSEG_DISTRIB )    // distributed => cxy depends on vpn LSB
2234    {
[577]2235        index    = vpn & ((x_size * y_size) - 1);
2236        page_cxy = HAL_CXY_FROM_XY( (index / y_size) , (index % y_size) );
[561]2237
[577]2238        // If the cluster selected from VPN's LSBs is empty, we select one randomly
2239        if ( cluster_is_active( page_cxy ) == false )
2240        {
2241            page_cxy = cluster_random_select();
[561]2242        }
[407]2243    }
2244    else                          // other cases => cxy specified in vseg
2245    {
[561]2246        page_cxy = vseg->cxy;
[407]2247    }
2248
[635]2249    // allocate one small physical page from target cluster
[656]2250    kmem_req_t req;
2251    req.type  = KMEM_PPM;
2252    req.order = 0;
2253    req.flags = AF_ZERO;
[407]2254
[656]2255    // get local pointer on page base
2256    void * ptr = kmem_remote_alloc( page_cxy , &req );
[635]2257
[656]2258    // get extended pointer on page descriptor
2259    page_xp = ppm_base2page( XPTR( page_cxy , ptr ) );
2260
[632]2261#if DEBUG_VMM_PAGE_ALLOCATE
[595]2262cycle = (uint32_t)hal_get_cycles();
[632]2263if( DEBUG_VMM_PAGE_ALLOCATE < cycle )
[635]2264printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n",
2265__FUNCTION__ , this->process->pid, this->trdid, vpn, ppm_page2ppn(page_xp), cycle );
[433]2266#endif
2267
[632]2268    return page_xp;
[407]2269
2270}  // end vmm_page_allocate() 
2271
[313]2272////////////////////////////////////////
2273error_t vmm_get_one_ppn( vseg_t * vseg,
2274                         vpn_t    vpn,
2275                         ppn_t  * ppn )
2276{
2277    error_t    error;
[407]2278    xptr_t     page_xp;           // extended pointer on physical page descriptor
[606]2279    uint32_t   page_id;           // missing page index in vseg mapper
[406]2280    uint32_t   type;              // vseg type;
[313]2281
[406]2282    type      = vseg->type;
[606]2283    page_id   = vpn - vseg->vpn_base;
[313]2284
[438]2285#if DEBUG_VMM_GET_ONE_PPN
[595]2286uint32_t   cycle = (uint32_t)hal_get_cycles();
2287thread_t * this  = CURRENT_THREAD;
[656]2288if( DEBUG_VMM_GET_ONE_PPN < cycle )
2289printk("\n[%s] thread[%x,%x] enter for vpn %x / vseg %s / page_id  %d / cycle %d\n",
[606]2290__FUNCTION__, this->process->pid, this->trdid, vpn, vseg_type_str(type), page_id, cycle );
[433]2291#endif
[313]2292
[656]2293#if (DEBUG_VMM_GET_ONE_PPN & 2)
2294if( DEBUG_VMM_GET_ONE_PPN < cycle )
2295hal_vmm_display( XPTR( local_cxy , this->process ) , true );
2296#endif
2297
[406]2298    // FILE type : get the physical page from the file mapper
[313]2299    if( type == VSEG_TYPE_FILE )
2300    {
[406]2301        // get extended pointer on mapper
[407]2302        xptr_t mapper_xp = vseg->mapper_xp;
[313]2303
[567]2304assert( (mapper_xp != XPTR_NULL),
2305"mapper not defined for a FILE vseg\n" );
[406]2306       
[606]2307        // get extended pointer on page descriptor
[657]2308        page_xp = mapper_get_page( mapper_xp , page_id );
[406]2309
[606]2310        if ( page_xp == XPTR_NULL ) return EINVAL;
[313]2311    }
2312
[406]2313    // Other types : allocate a physical page from target cluster,
[407]2314    // as defined by vseg type and vpn value
[313]2315    else
2316    {
[433]2317        // allocate one physical page
[407]2318        page_xp = vmm_page_allocate( vseg , vpn );
[406]2319
[635]2320        if( page_xp == XPTR_NULL ) return -1;
[313]2321
[406]2322        // initialise missing page from .elf file mapper for DATA and CODE types
[440]2323        // the vseg->mapper_xp field is an extended pointer on the .elf file mapper
[313]2324        if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) )
2325        {
[406]2326            // get extended pointer on mapper
2327            xptr_t     mapper_xp = vseg->mapper_xp;
[313]2328
[567]2329assert( (mapper_xp != XPTR_NULL),
2330"mapper not defined for a CODE or DATA vseg\n" );
[406]2331       
2332            // compute missing page offset in vseg
[606]2333            uint32_t offset = page_id << CONFIG_PPM_PAGE_SHIFT;
[406]2334
[313]2335            // compute missing page offset in .elf file
[406]2336            uint32_t elf_offset = vseg->file_offset + offset;
[313]2337
[438]2338#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[656]2339if( DEBUG_VMM_GET_ONE_PPN < cycle )
[595]2340printk("\n[%s] thread[%x,%x] for vpn = %x / elf_offset = %x\n",
2341__FUNCTION__, this->process->pid, this->trdid, vpn, elf_offset );
[433]2342#endif
[406]2343            // compute extended pointer on page base
[407]2344            xptr_t base_xp  = ppm_page2base( page_xp );
[313]2345
[406]2346            // file_size (in .elf mapper) can be smaller than vseg_size (BSS)
2347            uint32_t file_size = vseg->file_size;
2348
2349            if( file_size < offset )                 // missing page fully in  BSS
[313]2350            {
[406]2351
[438]2352#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[656]2353if( DEBUG_VMM_GET_ONE_PPN < cycle )
[595]2354printk("\n[%s] thread[%x,%x] for vpn  %x / fully in BSS\n",
2355__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]2356#endif
[407]2357                if( GET_CXY( page_xp ) == local_cxy )
[313]2358                {
[315]2359                    memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE );
[313]2360                }
2361                else
2362                {
[315]2363                   hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE );       
[313]2364                }
2365            }
[406]2366            else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) )  // fully in  mapper
[315]2367            {
[406]2368
[438]2369#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[656]2370if( DEBUG_VMM_GET_ONE_PPN < cycle )
[595]2371printk("\n[%s] thread[%x,%x] for vpn  %x / fully in mapper\n",
2372__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]2373#endif
[606]2374                error = mapper_move_kernel( mapper_xp,
2375                                            true,             // to_buffer
2376                                            elf_offset,
2377                                            base_xp,
2378                                            CONFIG_PPM_PAGE_SIZE ); 
[313]2379                if( error ) return EINVAL;
2380            }
[406]2381            else  // both in mapper and in BSS :
2382                  // - (file_size - offset)             bytes from mapper
2383                  // - (page_size + offset - file_size) bytes from BSS
[313]2384            {
[406]2385
[438]2386#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[656]2387if( DEBUG_VMM_GET_ONE_PPN < cycle )
[610]2388printk("\n[%s] thread[%x,%x] for vpn  %x / both mapper & BSS\n"
[433]2389"      %d bytes from mapper / %d bytes from BSS\n",
[595]2390__FUNCTION__, this->process->pid, this->trdid, vpn,
[407]2391file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size  );
[433]2392#endif
[313]2393                // initialize mapper part
[606]2394                error = mapper_move_kernel( mapper_xp,
2395                                            true,         // to buffer
2396                                            elf_offset,
2397                                            base_xp,
2398                                            file_size - offset ); 
[313]2399                if( error ) return EINVAL;
2400
2401                // initialize BSS part
[407]2402                if( GET_CXY( page_xp ) == local_cxy )
[313]2403                {
[406]2404                    memset( GET_PTR( base_xp ) + file_size - offset , 0 , 
2405                            offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]2406                }
2407                else
2408                {
[406]2409                   hal_remote_memset( base_xp + file_size - offset , 0 , 
2410                                      offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]2411                }
2412            }   
[656]2413
2414        }  // end if CODE or DATA types   
[313]2415    } 
2416
2417    // return ppn
[407]2418    *ppn = ppm_page2ppn( page_xp );
[406]2419
[438]2420#if DEBUG_VMM_GET_ONE_PPN
[656]2421if( DEBUG_VMM_GET_ONE_PPN < cycle )
[635]2422printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n",
[595]2423__FUNCTION__ , this->process->pid, this->trdid , vpn , *ppn, cycle );
[433]2424#endif
[406]2425
[656]2426#if (DEBUG_VMM_GET_ONE_PPN & 2)
2427if( DEBUG_VMM_GET_ONE_PPN < cycle )
2428hal_vmm_display( XPTR( local_cxy , this->process ) , true );
2429#endif
2430
[313]2431    return 0;
2432
2433}  // end vmm_get_one_ppn()
2434
[585]2435///////////////////////////////////////////////////
2436error_t vmm_handle_page_fault( process_t * process,
2437                               vpn_t       vpn )
[1]2438{
[585]2439    vseg_t         * vseg;            // vseg containing vpn
[629]2440    uint32_t         attr;            // PTE_ATTR value
2441    ppn_t            ppn;             // PTE_PPN value
[585]2442    uint32_t         ref_attr;        // PTE_ATTR value in reference GPT
2443    ppn_t            ref_ppn;         // PTE_PPN value in reference GPT
2444    cxy_t            ref_cxy;         // reference cluster for missing vpn
2445    process_t      * ref_ptr;         // reference process for missing vpn
2446    xptr_t           local_gpt_xp;    // extended pointer on local GPT
2447    xptr_t           ref_gpt_xp;      // extended pointer on reference GPT
2448    error_t          error;           // value returned by called functions
[1]2449
[629]2450    thread_t * this  = CURRENT_THREAD;
2451
2452#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2453uint32_t start_cycle = (uint32_t)hal_get_cycles();
2454#endif
2455
[625]2456#if DEBUG_VMM_HANDLE_PAGE_FAULT
[656]2457if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) & (vpn > 0) )
[625]2458printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n",
[629]2459__FUNCTION__, this->process->pid, this->trdid, vpn, start_cycle );
[625]2460#endif
2461
[656]2462#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 2)
[635]2463if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[656]2464hal_vmm_display( XPTR( local_cxy , this->process ) , true );
[629]2465#endif
2466
[585]2467    // get local vseg (access to reference VSL can be required)
2468    error = vmm_get_vseg( process, 
2469                          (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT,
2470                          &vseg );
2471    if( error )
2472    {
[629]2473        printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in registered vseg\n",
2474        __FUNCTION__ , vpn , process->pid, this->trdid );
[585]2475       
2476        return EXCP_USER_ERROR;
2477    }
2478
[635]2479#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2480if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[634]2481printk("\n[%s] thread[%x,%x] found vseg %s\n",
2482__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) );
[433]2483#endif
[406]2484
[629]2485    // build extended pointer on local GPT
2486    local_gpt_xp  = XPTR( local_cxy , &process->vmm.gpt );
2487
[632]2488    // lock PTE in local GPT and get current PPN and attributes
[629]2489    error = hal_gpt_lock_pte( local_gpt_xp,
2490                              vpn,
2491                              &attr,
2492                              &ppn );
2493    if( error )
[438]2494    {
[629]2495        printk("\n[PANIC] in %s : cannot lock PTE in local GPT / vpn %x / process %x\n",
2496        __FUNCTION__ , vpn , process->pid );
2497       
2498        return EXCP_KERNEL_PANIC;
2499    }
[407]2500
[635]2501#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2502if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2503printk("\n[%s] thread[%x,%x] locked vpn %x in cluster %x\n",
[634]2504__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy );
[632]2505#endif
2506
2507    // handle page fault only if local PTE still unmapped after lock
[629]2508    if( (attr & GPT_MAPPED) == 0 )
2509    {
2510        // get reference process cluster and local pointer
2511        ref_cxy = GET_CXY( process->ref_xp );
2512        ref_ptr = GET_PTR( process->ref_xp );
[407]2513
[630]2514        /////////////// private vseg or (local == reference)
2515        /////////////// => access only the local GPT
[629]2516        if( (vseg->type == VSEG_TYPE_STACK) ||
2517            (vseg->type == VSEG_TYPE_CODE)  ||
2518            (ref_cxy    == local_cxy ) )
2519        {
[632]2520
[635]2521#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2522if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2523printk("\n[%s] thread[%x,%x] access local gpt : cxy %x / ref_cxy %x / type %s / cycle %d\n",
2524__FUNCTION__, this->process->pid, this->trdid,
2525local_cxy, ref_cxy, vseg_type_str(vseg->type), (uint32_t)hal_get_cycles() );
[632]2526#endif
2527            // allocate and initialise a physical page
[629]2528            error = vmm_get_one_ppn( vseg , vpn , &ppn );
[407]2529
[585]2530            if( error )
[408]2531            {
[629]2532                printk("\n[ERROR] in %s : no physical page / process = %x / vpn = %x\n",
[408]2533                __FUNCTION__ , process->pid , vpn );
[1]2534
[629]2535                // unlock PTE in local GPT
2536                hal_gpt_unlock_pte( local_gpt_xp , vpn );
[406]2537
[585]2538                return EXCP_KERNEL_PANIC;
[407]2539            }
2540
[629]2541            // define attr from vseg flags
[632]2542            attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE;
[629]2543            if( vseg->flags & VSEG_USER  ) attr |= GPT_USER;
2544            if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE;
2545            if( vseg->flags & VSEG_EXEC  ) attr |= GPT_EXECUTABLE;
2546            if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE;
[407]2547
[629]2548            // set PTE to local GPT
[632]2549            // it unlocks this PTE
[629]2550            hal_gpt_set_pte( local_gpt_xp,
2551                             vpn,
2552                             attr,
2553                             ppn );
[585]2554
[629]2555#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2556uint32_t end_cycle = (uint32_t)hal_get_cycles();
2557#endif
[585]2558
2559#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2560if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2561printk("\n[%s] thread[%x,%x] handled local pgfault / ppn %x / attr %x / cycle %d\n",
2562__FUNCTION__, this->process->pid, this->trdid, ppn, attr, end_cycle );
[585]2563#endif
2564
[656]2565#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 2)
2566if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2567hal_vmm_display( XPTR( local_cxy , this->process ) , true );
2568#endif
2569
[629]2570#if CONFIG_INSTRUMENTATION_PGFAULTS
[656]2571uint32_t cost      = end_cycle - start_cycle;
[629]2572this->info.local_pgfault_nr++;
[641]2573this->info.local_pgfault_cost += cost;
2574if( cost > this->info.local_pgfault_max ) this->info.local_pgfault_max = cost;
[629]2575#endif
2576            return EXCP_NON_FATAL;
[585]2577
[629]2578        }   // end local GPT access
[585]2579
[630]2580        /////////////////// public vseg and (local != reference)
2581        /////////////////// => access ref GPT to update local GPT
[629]2582        else                               
2583        {
[632]2584
[635]2585#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2586if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2587printk("\n[%s] thread[%x,%x] access ref gpt : cxy %x / ref_cxy %x / type %s / cycle %d\n",
2588__FUNCTION__, this->process->pid, this->trdid, 
2589local_cxy, ref_cxy, vseg_type_str(vseg->type), (uint32_t)hal_get_cycles() );
[632]2590#endif
[629]2591            // build extended pointer on reference GPT
2592            ref_gpt_xp = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
[585]2593
[632]2594            // lock PTE in reference GPT and get current PPN and attributes
2595            error = hal_gpt_lock_pte( ref_gpt_xp,
2596                                      vpn,
2597                                      &ref_attr,
2598                                      &ref_ppn );
2599            if( error )
2600            {
2601                printk("\n[PANIC] in %s : cannot lock PTE in ref GPT / vpn %x / process %x\n",
2602                __FUNCTION__ , vpn , process->pid );
2603       
2604                // unlock PTE in local GPT
2605                hal_gpt_unlock_pte( local_gpt_xp , vpn );
2606                   
2607                return EXCP_KERNEL_PANIC;
2608            }
[1]2609
[635]2610#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2611if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2612printk("\n[%s] thread[%x,%x] get pte from ref gpt / attr %x / ppn %x\n",
2613__FUNCTION__, this->process->pid, this->trdid, ref_attr, ref_ppn );
2614#endif
2615
2616            if( ref_attr & GPT_MAPPED )        // false page fault
[585]2617            {
[629]2618                // update local GPT from reference GPT values
[632]2619                // this unlocks the PTE in local GPT
[629]2620                hal_gpt_set_pte( local_gpt_xp,
2621                                 vpn,
2622                                 ref_attr,
2623                                 ref_ppn );
[585]2624
[635]2625#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2626if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2627printk("\n[%s] thread[%x,%x] updated local gpt for a false pgfault\n",
2628__FUNCTION__, this->process->pid, this->trdid );
2629#endif
2630
2631                // unlock the PTE in reference GPT
2632                hal_gpt_unlock_pte( ref_gpt_xp, vpn );
2633                             
[635]2634#if (DEBUG_VMM_HANDLE_PAGE_FAULT &1)
2635if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2636printk("\n[%s] thread[%x,%x] unlock the ref gpt after a false pgfault\n",
2637__FUNCTION__, this->process->pid, this->trdid );
2638#endif
2639
[629]2640#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2641uint32_t end_cycle = (uint32_t)hal_get_cycles();
2642#endif
2643
[585]2644#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2645if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2646printk("\n[%s] thread[%x,%x] handled false pgfault / ppn %x / attr %x / cycle %d\n",
2647__FUNCTION__, this->process->pid, this->trdid, ref_ppn, ref_attr, end_cycle );
[433]2648#endif
[406]2649
[656]2650#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 2)
2651if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2652hal_vmm_display( XPTR( local_cxy , this->process ) , true );
2653#endif
2654
[629]2655#if CONFIG_INSTRUMENTATION_PGFAULTS
[656]2656uint32_t cost      = end_cycle - start_cycle;
[629]2657this->info.false_pgfault_nr++;
[641]2658this->info.false_pgfault_cost += cost;
2659if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost;
[629]2660#endif
2661                return EXCP_NON_FATAL;
2662            }
[632]2663            else                            // true page fault
[629]2664            {
[585]2665                // allocate and initialise a physical page depending on the vseg type
[629]2666                error = vmm_get_one_ppn( vseg , vpn , &ppn );
[1]2667
[585]2668                if( error )
2669                {
2670                    printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
2671                    __FUNCTION__ , process->pid , vpn );
[313]2672
[632]2673                    // unlock PTE in local GPT and in reference GPT
[629]2674                    hal_gpt_unlock_pte( local_gpt_xp , vpn );
[632]2675                    hal_gpt_unlock_pte( ref_gpt_xp   , vpn );
[585]2676                   
[629]2677                    return EXCP_KERNEL_PANIC;
[585]2678                }
[1]2679
[629]2680                // define attr from vseg flags
[632]2681                attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE;
[629]2682                if( vseg->flags & VSEG_USER  ) attr |= GPT_USER;
2683                if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE;
2684                if( vseg->flags & VSEG_EXEC  ) attr |= GPT_EXECUTABLE;
2685                if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE;
[585]2686
[635]2687#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2688if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2689printk("\n[%s] thread[%x,%x] build a new PTE for a true pgfault\n",
2690__FUNCTION__, this->process->pid, this->trdid );
2691#endif
[629]2692                // set PTE in reference GPT
[632]2693                // this unlock the PTE
[629]2694                hal_gpt_set_pte( ref_gpt_xp,
2695                                 vpn,
2696                                 attr,
2697                                 ppn );
2698
[635]2699#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2700if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2701printk("\n[%s] thread[%x,%x] set new PTE in ref gpt for a true page fault\n",
2702__FUNCTION__, this->process->pid, this->trdid );
2703#endif
2704
[629]2705                // set PTE in local GPT
[632]2706                // this unlock the PTE
[629]2707                hal_gpt_set_pte( local_gpt_xp,
2708                                 vpn,
2709                                 attr,
2710                                 ppn );
2711
2712#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2713uint32_t end_cycle = (uint32_t)hal_get_cycles();
2714#endif
2715
[440]2716#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2717if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2718printk("\n[%s] thread[%x,%x] handled global pgfault / ppn %x / attr %x / cycle %d\n",
2719__FUNCTION__, this->process->pid, this->trdid, ppn, attr, end_cycle );
[435]2720#endif
[629]2721
[656]2722#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 2)
2723if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2724hal_vmm_display( XPTR( local_cxy , this->process ) , true );
2725#endif
2726
[629]2727#if CONFIG_INSTRUMENTATION_PGFAULTS
[656]2728uint32_t cost      = end_cycle - start_cycle;
[629]2729this->info.global_pgfault_nr++;
[641]2730this->info.global_pgfault_cost += cost;
2731if( cost > this->info.global_pgfault_max ) this->info.global_pgfault_max = cost;
[629]2732#endif
2733                return EXCP_NON_FATAL;
2734            }
[585]2735        }
2736    }
[629]2737    else   // page has been locally mapped by another concurrent thread
2738    {
[632]2739        // unlock the PTE in local GPT
[629]2740        hal_gpt_unlock_pte( local_gpt_xp , vpn );
2741
[632]2742#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2743uint32_t end_cycle = (uint32_t)hal_get_cycles();
2744#endif
2745
2746#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2747if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2748printk("\n[%s] handled by another thread / vpn %x / ppn %x / attr %x / cycle %d\n",
2749__FUNCTION__, vpn, ppn, attr, end_cycle );
2750#endif
2751
2752#if CONFIG_INSTRUMENTATION_PGFAULTS
[656]2753uint32_t cost      = end_cycle - start_cycle;
[632]2754this->info.false_pgfault_nr++;
[641]2755this->info.false_pgfault_cost += cost;
2756if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost;
[632]2757#endif
[629]2758        return EXCP_NON_FATAL;
2759    }
2760
[585]2761}   // end vmm_handle_page_fault()
[435]2762
[585]2763////////////////////////////////////////////
2764error_t vmm_handle_cow( process_t * process,
2765                        vpn_t       vpn )
2766{
2767    vseg_t         * vseg;            // vseg containing vpn
[629]2768    xptr_t           gpt_xp;          // extended pointer on GPT (local or reference)
2769    gpt_t          * gpt_ptr;         // local pointer on GPT (local or reference)
2770    cxy_t            gpt_cxy;         // GPT cluster identifier
[585]2771    uint32_t         old_attr;        // current PTE_ATTR value
2772    ppn_t            old_ppn;         // current PTE_PPN value
2773    uint32_t         new_attr;        // new PTE_ATTR value
2774    ppn_t            new_ppn;         // new PTE_PPN value
[629]2775    cxy_t            ref_cxy;         // reference process cluster
2776    process_t      * ref_ptr;         // local pointer on reference process
[585]2777    error_t          error;
[1]2778
[629]2779    thread_t * this  = CURRENT_THREAD;
[625]2780
[585]2781#if DEBUG_VMM_HANDLE_COW
[629]2782uint32_t   cycle = (uint32_t)hal_get_cycles();
[640]2783if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[595]2784printk("\n[%s] thread[%x,%x] enter for vpn %x / core[%x,%d] / cycle %d\n",
[619]2785__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
[629]2786#endif
2787
[656]2788#if (DEBUG_VMM_HANDLE_COW & 2)
[640]2789hal_vmm_display( XPTR( local_cxy , process ) , true );
[585]2790#endif
2791
2792    // get local vseg
2793    error = vmm_get_vseg( process, 
2794                          (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT,
2795                          &vseg );
[440]2796    if( error )
[1]2797    {
[629]2798        printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in a registered vseg\n",
[625]2799        __FUNCTION__, vpn, process->pid, this->trdid );
[585]2800
[629]2801        return EXCP_USER_ERROR;
[440]2802    }
[407]2803
[629]2804#if DEBUG_VMM_HANDLE_COW
[640]2805if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[629]2806printk("\n[%s] thread[%x,%x] get vseg %s\n",
2807__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) );
[619]2808#endif
2809
[629]2810    // get reference process cluster and local pointer
[585]2811    ref_cxy = GET_CXY( process->ref_xp );
2812    ref_ptr = GET_PTR( process->ref_xp );
[407]2813
[629]2814    // build pointers on relevant GPT
2815    // - access only local GPT for a private vseg 
2816    // - access reference GPT and all copies for a public vseg
[585]2817    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[440]2818    {
[629]2819        gpt_cxy = local_cxy;
2820        gpt_ptr = &process->vmm.gpt;
2821        gpt_xp  = XPTR( gpt_cxy , gpt_ptr );
[1]2822    }
[440]2823    else
[1]2824    {
[629]2825        gpt_cxy = ref_cxy;
2826        gpt_ptr = &ref_ptr->vmm.gpt;
2827        gpt_xp  = XPTR( gpt_cxy , gpt_ptr );
[1]2828    }
2829
[629]2830    // lock target PTE in relevant GPT (local or reference)
[632]2831    // and get current PTE value
[629]2832    error = hal_gpt_lock_pte( gpt_xp,
2833                              vpn,
2834                              &old_attr,
2835                              &old_ppn );
2836    if( error )
2837    {
2838        printk("\n[PANIC] in %s : cannot lock PTE in GPT / cxy %x / vpn %x / process %x\n",
2839        __FUNCTION__ , gpt_cxy, vpn , process->pid );
2840       
2841        return EXCP_KERNEL_PANIC;
2842    }
[441]2843
[629]2844#if DEBUG_VMM_HANDLE_COW
[640]2845if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]2846printk("\n[%s] thread[%x,%x] get pte for vpn %x : ppn %x / attr %x\n",
2847__FUNCTION__, this->process->pid, this->trdid, vpn, old_ppn, old_attr );
2848#endif
2849
[629]2850    // return user error if COW attribute not set or PTE2 unmapped
2851    if( ((old_attr & GPT_COW) == 0) || ((old_attr & GPT_MAPPED) == 0) )
[585]2852    {
[629]2853        hal_gpt_unlock_pte( gpt_xp , vpn );
[407]2854
[629]2855        return EXCP_USER_ERROR;
[407]2856    }
2857
[619]2858    // get pointers on physical page descriptor
[585]2859    xptr_t   page_xp  = ppm_ppn2page( old_ppn );
2860    cxy_t    page_cxy = GET_CXY( page_xp );
2861    page_t * page_ptr = GET_PTR( page_xp );
[435]2862
[585]2863    // get extended pointers on forks and lock field in page descriptor
2864    xptr_t forks_xp       = XPTR( page_cxy , &page_ptr->forks );
2865    xptr_t forks_lock_xp  = XPTR( page_cxy , &page_ptr->lock );
[407]2866
[585]2867    // take lock protecting "forks" counter
2868    remote_busylock_acquire( forks_lock_xp );
[407]2869
[585]2870    // get number of pending forks from page descriptor
2871    uint32_t forks = hal_remote_l32( forks_xp );
[441]2872
[629]2873#if DEBUG_VMM_HANDLE_COW
[640]2874if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]2875printk("\n[%s] thread[%x,%x] get forks = %d for vpn %x\n",
2876__FUNCTION__, this->process->pid, this->trdid, forks, vpn );
2877#endif
2878
[585]2879    if( forks )        // pending fork => allocate a new page, and copy old to new
2880    {
[619]2881        // decrement pending forks counter in page descriptor
2882        hal_remote_atomic_add( forks_xp , -1 );
2883
2884        // release lock protecting "forks" counter
2885        remote_busylock_release( forks_lock_xp );
2886
[629]2887        // allocate a new physical page depending on vseg type
[585]2888        page_xp = vmm_page_allocate( vseg , vpn );
[619]2889
[585]2890        if( page_xp == XPTR_NULL ) 
2891        {
2892            printk("\n[PANIC] in %s : no memory for vpn %x in process %x\n",
2893            __FUNCTION__ , vpn, process->pid );
[441]2894
[629]2895            hal_gpt_unlock_pte( gpt_xp , vpn ); 
[441]2896
[585]2897            return EXCP_KERNEL_PANIC;
2898        }
[441]2899
[585]2900        // compute allocated page PPN
2901        new_ppn = ppm_page2ppn( page_xp );
[441]2902
[629]2903#if DEBUG_VMM_HANDLE_COW
[640]2904if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]2905printk("\n[%s] thread[%x,%x] get new ppn %x for vpn %x\n",
2906__FUNCTION__, this->process->pid, this->trdid, new_ppn, vpn );
2907#endif
2908
[585]2909        // copy old page content to new page
[619]2910        hal_remote_memcpy( ppm_ppn2base( new_ppn ),
2911                           ppm_ppn2base( old_ppn ),
2912                           CONFIG_PPM_PAGE_SIZE );
[441]2913
[629]2914#if DEBUG_VMM_HANDLE_COW
[640]2915if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]2916printk("\n[%s] thread[%x,%x] copied old page to new page\n",
2917__FUNCTION__, this->process->pid, this->trdid );
[585]2918#endif
[440]2919
[585]2920    }             
2921    else               // no pending fork => keep the existing page
2922    {
[619]2923        // release lock protecting "forks" counter
2924        remote_busylock_release( forks_lock_xp );
[1]2925
[585]2926#if(DEBUG_VMM_HANDLE_COW & 1)
[640]2927if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[635]2928printk("\n[%s] thread[%x,%x] no pending forks / keep existing PPN %x\n",
[619]2929__FUNCTION__, this->process->pid, this->trdid, old_ppn );
[585]2930#endif
2931        new_ppn = old_ppn;
2932    }
[1]2933
[629]2934    // build new_attr : set WRITABLE, reset COW, reset LOCKED
2935    new_attr = (((old_attr | GPT_WRITABLE) & (~GPT_COW)) & (~GPT_LOCKED));
[585]2936
[635]2937#if(DEBUG_VMM_HANDLE_COW & 1)
[640]2938if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[635]2939printk("\n[%s] thread[%x,%x] new_attr %x / new_ppn %x\n",
2940__FUNCTION__, this->process->pid, this->trdid, new_attr, new_ppn );
2941#endif
2942
[629]2943    // update the relevant GPT(s)
2944    // - private vseg => update only the local GPT
2945    // - public vseg => update the reference GPT AND all the GPT copies
[585]2946    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[1]2947    {
[635]2948        // set new PTE in local gpt
[585]2949        hal_gpt_set_pte( gpt_xp,
2950                         vpn,
2951                         new_attr,
2952                         new_ppn );
[1]2953    }
[585]2954    else
[1]2955    {
[640]2956        // set new PTE in all GPT copies
2957        vmm_global_update_pte( process,
2958                               vpn,
2959                               new_attr,
2960                               new_ppn );
[1]2961    }
2962
[585]2963#if DEBUG_VMM_HANDLE_COW
2964cycle = (uint32_t)hal_get_cycles();
[640]2965if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[595]2966printk("\n[%s] thread[%x,%x] exit for vpn %x / core[%x,%d] / cycle %d\n",
[619]2967__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
[585]2968#endif
[313]2969
[656]2970#if (DEBUG_VMM_HANDLE_COW & 2)
[640]2971hal_vmm_display( XPTR( local_cxy , process ) , true );
[635]2972#endif
2973
[585]2974     return EXCP_NON_FATAL;
[1]2975
[585]2976}   // end vmm_handle_cow()
2977
Note: See TracBrowser for help on using the repository browser.