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

Last change on this file since 641 was 641, checked in by alain, 3 years ago
  • Fix several bugs.
  • Introduce the "stat" command in KSH.

This almos-mkh version sucessfully executed the FFT application
(65536 complex points) on the TSAR architecture from 1 to 64 cores.

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