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

Last change on this file since 642 was 641, checked in by alain, 5 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
RevLine 
[1]1/*
[611]2 * vmm.c - virtual memory manager related operations definition.
[1]3 *
4 * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
[625]5 *           Alain Greiner (2016,2017,2018,2019)
[21]6 *
[1]7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
[14]25#include <kernel_config.h>
[457]26#include <hal_kernel_types.h>
[1]27#include <hal_special.h>
28#include <hal_gpt.h>
[409]29#include <hal_vmm.h>
[641]30#include <hal_irqmask.h>
[577]31#include <hal_macros.h>
[1]32#include <printk.h>
[23]33#include <memcpy.h>
[567]34#include <remote_queuelock.h>
[1]35#include <list.h>
[408]36#include <xlist.h>
[1]37#include <bits.h>
38#include <process.h>
39#include <thread.h>
40#include <vseg.h>
41#include <cluster.h>
42#include <scheduler.h>
43#include <vfs.h>
44#include <mapper.h>
45#include <page.h>
46#include <kmem.h>
47#include <vmm.h>
[585]48#include <hal_exception.h>
[1]49
[635]50////////////////////////////////////////////////////////////////////////////////////////////
[1]51//   Extern global variables
[635]52////////////////////////////////////////////////////////////////////////////////////////////
[1]53
[567]54extern  process_t  process_zero;      // allocated in cluster.c
[1]55
[625]56////////////////////////////////////////////////////////////////////////////////////////////
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 )
[21]69{
[625]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////////////////////////////////////////////////////////////////////////////////////////////
[641]219// This static function implements the VMM MMAP specific desallocator.
220// It is called by the vmm_remove_vseg() function.
[625]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{
[614]290    uint32_t  i;
[1]291
[625]292#if DEBUG_VMM_USER_INIT
[567]293thread_t * this = CURRENT_THREAD;
[433]294uint32_t cycle = (uint32_t)hal_get_cycles();
[625]295if( DEBUG_VMM_USER_INIT )
[614]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 );
[433]298#endif
[204]299
[1]300    // get pointer on VMM
301    vmm_t   * vmm = &process->vmm;
302
[625]303// check UTILS zone
[624]304assert( ((CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) <= 
305         (CONFIG_VMM_ELF_BASE - CONFIG_VMM_UTILS_BASE)) ,
306         "UTILS zone too small\n" );
[21]307
[625]308// check STACK zone
[567]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");
[1]312
[635]313    // initialize the lock protecting the VSL
[640]314        remote_queuelock_init( XPTR( local_cxy , &vmm->vsl_lock ) , LOCK_VMM_VSL );
[635]315
316
317/*
[625]318    // register "args" vseg in VSL
[624]319    base = CONFIG_VMM_UTILS_BASE << CONFIG_PPM_PAGE_SHIFT;
[1]320    size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]321
[407]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 );
[415]330    if( vseg_args == NULL )
331    {
332        printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ );
333        return -1;
334    }
[204]335
[406]336    vmm->args_vpn_base = base;
[1]337
[625]338    // register "envs" vseg in VSL
[624]339    base = (CONFIG_VMM_UTILS_BASE + CONFIG_VMM_ARGS_SIZE) << CONFIG_PPM_PAGE_SHIFT;
[1]340    size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]341
[407]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 );
[415]350    if( vseg_envs == NULL )
351    {
352        printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ );
353        return -1;
354    }
[204]355
[406]356    vmm->envs_vpn_base = base;
[635]357*/
[1]358    // initialize STACK allocator
359    vmm->stack_mgr.bitmap   = 0;
360    vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE;
[567]361    busylock_init( &vmm->stack_mgr.lock , LOCK_VMM_STACK );
[1]362
363    // initialize MMAP allocator
[407]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;
[567]367    busylock_init( &vmm->mmap_mgr.lock , LOCK_VMM_MMAP );
[625]368    for( i = 0 ; i < 32 ; i++ )
369    {
370        xlist_root_init( XPTR( local_cxy , &vmm->mmap_mgr.zombi_list[i] ) );
371    }
[1]372
[21]373    // initialize instrumentation counters
[635]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;
[1]380
[124]381    hal_fence();
[1]382
[625]383#if DEBUG_VMM_USER_INIT
[433]384cycle = (uint32_t)hal_get_cycles();
[625]385if( DEBUG_VMM_USER_INIT )
[614]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 );
[433]388#endif
[204]389
[415]390    return 0;
391
[625]392}  // end vmm_user_init()
[204]393
[611]394//////////////////////////////////////////
[625]395void vmm_user_reset( process_t * process )
[567]396{
[625]397    xptr_t       vseg_xp;
398        vseg_t     * vseg;
399    vseg_type_t  vseg_type;
[567]400
[625]401#if DEBUG_VMM_USER_RESET
[635]402uint32_t   cycle;
[625]403thread_t * this = CURRENT_THREAD;
[635]404#endif
405
406#if (DEBUG_VMM_USER_RESET & 1 )
407cycle = (uint32_t)hal_get_cycles();
[625]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
[567]412
[625]413#if (DEBUG_VMM_USER_RESET & 1 )
414if( DEBUG_VMM_USER_RESET < cycle )
[635]415hal_vmm_display( XPTR( local_cxy , process ) , true );
[625]416#endif
[567]417
[625]418    // get pointer on local VMM
419    vmm_t * vmm = &process->vmm;
[624]420
[625]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 );
[567]424
[625]425    // take the VSL lock
[640]426        remote_queuelock_acquire( lock_xp );
[567]427
[625]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 );
[611]438
[625]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;
[567]443
[625]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 );
[567]456
[625]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        {
[567]465
[625]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
[567]473
[625]474    // release the VSL lock
[640]475        remote_queuelock_release( lock_xp );
[567]476
[625]477// FIXME il faut gérer les process copies...
[611]478
[625]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
[611]485
[635]486#if (DEBUG_VMM_USER_RESET & 1 )
487if( DEBUG_VMM_USER_RESET < cycle )
488hal_vmm_display( XPTR( local_cxy , process ) , true );
489#endif
490
[625]491}  // end vmm_user_reset()
[611]492
[640]493/////////////////////////////////////////////////
494void vmm_global_delete_vseg( process_t * process,
495                             intptr_t    base )
496{
497    cxy_t           owner_cxy;
498    lpid_t          owner_lpid;
[641]499    reg_t           save_sr;
[640]500
[641]501    xptr_t          process_lock_xp;
[640]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
[641]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
[640]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 )
[641]526printk("\n[%s] thread[%x,%x] enters / process %x / base %x / cycle %d\n",
[640]527__FUNCTION__, this->process->pid, this->trdid, process->pid, base, cycle );
528#endif
529
[641]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
[640]539    // get owner process cluster and local index
540    owner_cxy        = CXY_FROM_PID( pid );
541    owner_lpid       = LPID_FROM_PID( pid );
542
[641]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] );
[640]546
[641]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
[640]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            {
[641]587                // atomically increment responses counter
588                hal_atomic_add( &responses , 1 );
[640]589
590#if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1)
591if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]592printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n",
593__FUNCTION__, this->process->pid, this->trdid, remote_process_cxy );
[640]594#endif
[641]595                // send RPC to remote cluster
596                rpc_send( remote_process_cxy , &rpc );
[640]597
[641]598                // exit loop on vsegs
599                break;
[640]600            }
601        }  // end of loop on vsegs
602
[641]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
[640]611#if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1)
612if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]613printk("\n[%s] thread[%x,%x] deschedule / process %x / base %x\n",
614__FUNCTION__, this->process->pid, this->trdid, process->pid, base );
[640]615#endif
616
[641]617    // client thread deschedule
618    sched_yield("blocked on rpc_vmm_delete_vseg");
619 
620    // restore IRQs
621    hal_restore_irq( save_sr );
[640]622
623#if DEBUG_VMM_GLOBAL_DELETE_VSEG
624cycle = (uint32_t)hal_get_cycles();
625if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle )
[641]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 );
[640]628#endif
629
630}  // end vmm_global_delete_vseg()
631
[595]632////////////////////////////////////////////////
[640]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;
[641]640    reg_t           save_sr;
[640]641
[641]642    xptr_t          process_lock_xp;
[640]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
[641]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
[640]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
[641]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
[640]683    owner_cxy        = CXY_FROM_PID( pid );
684    owner_lpid       = LPID_FROM_PID( pid );
685
[641]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] );
[640]689
[641]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
[640]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            {
[641]730                // atomically increment responses counter
731                hal_atomic_add( &responses , 1 );
732
[640]733#if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1)
734if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle )
[641]735printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n",
736__FUNCTION__, this->process->pid, this->trdid, remote_process_cxy );
[640]737#endif
[641]738                // send RPC to remote cluster
739                rpc_send( remote_process_cxy , & rpc );
[640]740
[641]741                // exit loop on vsegs
742                break;
[640]743            }
[641]744
[640]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 );
[641]754
[640]755    }  // end of loop on process copies
756
[641]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
[640]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////////////////////////////////////////////////
[433]782void vmm_global_update_pte( process_t * process,
783                            vpn_t       vpn,
784                            uint32_t    attr,
785                            ppn_t       ppn )
[23]786{
[640]787    pid_t           pid;
788    cxy_t           owner_cxy;
789    lpid_t          owner_lpid;
790
[408]791    xlist_entry_t * process_root_ptr;
792    xptr_t          process_root_xp;
793    xptr_t          process_iter_xp;
[23]794
[408]795    xptr_t          remote_process_xp;
796    cxy_t           remote_process_cxy;
797    process_t     * remote_process_ptr;
798    xptr_t          remote_gpt_xp;
[23]799
[640]800#if DEBUG_VMM_GLOBAL_UPDATE_PTE
[433]801uint32_t cycle = (uint32_t)hal_get_cycles();
[595]802thread_t * this = CURRENT_THREAD;
[640]803#endif
804
805
806#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
807if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[635]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 );
[433]810#endif
811
[640]812    // get owner process cluster and local index
[408]813    pid              = process->pid;
814    owner_cxy        = CXY_FROM_PID( pid );
815    owner_lpid       = LPID_FROM_PID( pid );
[640]816
817    // get extended pointer on root of process copies xlist in owner cluster
[408]818    process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid];
819    process_root_xp  = XPTR( owner_cxy , process_root_ptr );
[23]820
[640]821    // loop on process copies
[408]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 );
[433]826        remote_process_ptr = GET_PTR( remote_process_xp );
[408]827        remote_process_cxy = GET_CXY( remote_process_xp );
[407]828
[640]829#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
830if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[635]831printk("\n[%s] thread[%x,%x] handling vpn %x for process %x in cluster %x\n",
[595]832__FUNCTION__, this->process->pid, this->trdid, vpn, process->pid, remote_process_cxy );
[433]833#endif
834
[408]835        // get extended pointer on remote gpt
836        remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt );
837
[433]838        // update remote GPT
839        hal_gpt_update_pte( remote_gpt_xp, vpn, attr, ppn );
[408]840    } 
841
[640]842#if DEBUG_VMM_GLOBAL_UPDATE_PTE
[433]843cycle = (uint32_t)hal_get_cycles();
[640]844if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle )
[595]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 );
[433]847#endif
848
[640]849#if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1)
[635]850hal_vmm_display( process , true );
851#endif
852
[433]853}  // end vmm_global_update_pte()
854
[408]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
[635]879    // get target process PID
880    pid = process->pid;
881
[438]882#if DEBUG_VMM_SET_COW
[595]883uint32_t   cycle = (uint32_t)hal_get_cycles();
884thread_t * this  = CURRENT_THREAD;
[438]885if( DEBUG_VMM_SET_COW < cycle )
[595]886printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n",
[635]887__FUNCTION__, this->process->pid, this->trdid, pid , cycle );
[433]888#endif
[408]889
[635]890#if (DEBUG_VMM_SET_COW & 1)
891if( DEBUG_VMM_SET_COW < cycle )
892hal_vmm_display( process , true );
893#endif
894
[567]895// check cluster is reference
[635]896assert( (XPTR( local_cxy , process ) == process->ref_xp),
897"local cluster must be process reference cluster\n");
[408]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
[635]911    // loop on target process copies
[408]912    XLIST_FOREACH( process_root_xp , process_iter_xp )
913    {
[635]914        // get cluster and local pointer on remote process copy
[408]915        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
[433]916        remote_process_ptr = GET_PTR( remote_process_xp );
[408]917        remote_process_cxy = GET_CXY( remote_process_xp );
918
[595]919#if (DEBUG_VMM_SET_COW & 1)
[438]920if( DEBUG_VMM_SET_COW < cycle )
[635]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 );
[433]923#endif
[408]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 );
[433]933            vseg     = GET_PTR( vseg_xp );
[408]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
[595]940#if (DEBUG_VMM_SET_COW & 1)
[438]941if( DEBUG_VMM_SET_COW < cycle )
[635]942printk("\n[%s] thread[%x,%x] found vseg %s / vpn_base = %x / vpn_size = %x\n",
[595]943__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size );
[433]944#endif
945            // only DATA, ANON and REMOTE vsegs
[408]946            if( (type == VSEG_TYPE_DATA)  ||
947                (type == VSEG_TYPE_ANON)  ||
948                (type == VSEG_TYPE_REMOTE) )
949            {
[433]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;
[469]957                xptr_t     lock_xp;
[433]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,
[635]965                // this is only done once, when handling the reference copy
[433]966                if( remote_process_cxy == local_cxy )
967                {
[635]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
[433]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
[585]978                        hal_gpt_get_pte( remote_gpt_xp , vpn , &attr , &ppn ); 
[433]979
980                        // atomically update pending forks counter if page is mapped
981                        if( attr & GPT_MAPPED )
982                        {
[469]983                            // get pointers and cluster on page descriptor
[433]984                            page_xp  = ppm_ppn2page( ppn );
985                            page_cxy = GET_CXY( page_xp );
986                            page_ptr = GET_PTR( page_xp );
[469]987
988                            // get extended pointers on "forks" and "lock"
[433]989                            forks_xp = XPTR( page_cxy , &page_ptr->forks );
[469]990                            lock_xp  = XPTR( page_cxy , &page_ptr->lock );
991
[567]992                            // take lock protecting "forks" counter
993                            remote_busylock_acquire( lock_xp );
994
[469]995                            // increment "forks"
[433]996                            hal_remote_atomic_add( forks_xp , 1 );
[567]997
998                            // release lock protecting "forks" counter
999                            remote_busylock_release( lock_xp );
[433]1000                        }
1001                    }   // end loop on vpn
[635]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
[433]1008                }   // end if local
1009            }   // end if vseg type
1010        }   // end loop on vsegs
[408]1011    }   // end loop on process copies
1012 
[438]1013#if DEBUG_VMM_SET_COW
[433]1014cycle = (uint32_t)hal_get_cycles();
[438]1015if( DEBUG_VMM_SET_COW < cycle )
[595]1016printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n",
1017__FUNCTION__, this->process->pid, this->trdid, process->pid , cycle );
[433]1018#endif
[408]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
[438]1044#if DEBUG_VMM_FORK_COPY
[433]1045uint32_t cycle = (uint32_t)hal_get_cycles();
[595]1046thread_t * this = CURRENT_THREAD;
[438]1047if( DEBUG_VMM_FORK_COPY < cycle )
[595]1048printk("\n[%s] thread %x enter / cycle %d\n",
1049__FUNCTION__ , this->process->pid, this->trdid, cycle );
[433]1050#endif
[408]1051
1052    // get parent process cluster and local pointer
1053    parent_cxy     = GET_CXY( parent_process_xp );
[433]1054    parent_process = GET_PTR( parent_process_xp );
[408]1055
1056    // get local pointers on parent and child VMM
1057    parent_vmm = &parent_process->vmm; 
1058    child_vmm  = &child_process->vmm;
1059
[625]1060    // build extended pointer on parent VSL root and lock
[408]1061    parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root );
[625]1062    parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsl_lock );
[408]1063
[640]1064    // take the lock protecting the parent VSL
1065    remote_queuelock_acquire( parent_lock_xp );
[415]1066
[408]1067    // loop on parent VSL xlist
1068    XLIST_FOREACH( parent_root_xp , iter_xp )
[23]1069    {
[625]1070        // get pointers on current parent vseg
[408]1071        parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1072        parent_vseg    = GET_PTR( parent_vseg_xp );
[23]1073
[408]1074        // get vseg type
[567]1075        type = hal_remote_l32( XPTR( parent_cxy , &parent_vseg->type ) );
[408]1076       
[438]1077#if DEBUG_VMM_FORK_COPY
[433]1078cycle = (uint32_t)hal_get_cycles();
[438]1079if( DEBUG_VMM_FORK_COPY < cycle )
[595]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),
[567]1082hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]1083#endif
[23]1084
[623]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) )
[23]1088        {
[408]1089            // allocate memory for a new child vseg
1090            child_vseg = vseg_alloc();
1091            if( child_vseg == NULL )   // release all allocated vsegs
[23]1092            {
[408]1093                vmm_destroy( child_process );
1094                printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ );
1095                return -1;
[23]1096            }
1097
[408]1098            // copy parent vseg to child vseg
1099            vseg_init_from_ref( child_vseg , parent_vseg_xp );
[23]1100
[640]1101            // build extended pointer on child VSL lock
1102            xptr_t child_lock_xp = XPTR( local_cxy , &child_vmm->vsl_lock );
[625]1103 
[640]1104            // take the child VSL lock
1105            remote_queuelock_acquire( child_lock_xp );
[625]1106
[408]1107            // register child vseg in child VSL
[611]1108            vmm_attach_vseg_to_vsl( child_vmm , child_vseg );
[407]1109
[640]1110            // release the child VSL lock
1111            remote_queuelock_release( child_lock_xp );
[625]1112
[438]1113#if DEBUG_VMM_FORK_COPY
[433]1114cycle = (uint32_t)hal_get_cycles();
[438]1115if( DEBUG_VMM_FORK_COPY < cycle )
[595]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),
[567]1118hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]1119#endif
[625]1120            // copy DATA, ANON, REMOTE, FILE parent GPT entries to child GPT
[408]1121            if( type != VSEG_TYPE_CODE )
1122            {
[625]1123                // activate the COW for DATA, ANON, REMOTE vsegs only
[635]1124                // cow = ( type != VSEG_TYPE_FILE );
[23]1125
[408]1126                vpn_base = child_vseg->vpn_base;
1127                vpn_size = child_vseg->vpn_size;
[23]1128
[408]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,
[625]1133                                              vpn,
[408]1134                                              XPTR( parent_cxy , &parent_vmm->gpt ),
1135                                              vpn,
[635]1136                                              false,      // does not handle COW flag
1137                                              &ppn,       // unused
1138                                              &mapped );  // unused
[408]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
[438]1146#if DEBUG_VMM_FORK_COPY
[433]1147cycle = (uint32_t)hal_get_cycles();
[438]1148if( DEBUG_VMM_FORK_COPY < cycle )
[595]1149printk("\n[%s] thread[%x,%x] copied vpn %x to child GPT / cycle %d\n",
1150__FUNCTION__ , this->process->pid, this->trdid , vpn , cycle );
[433]1151#endif
[408]1152                }
1153            }   // end if no code & no stack
1154        }   // end if no stack
1155    }   // end loop on vsegs
1156
[567]1157    // release the parent VSL lock in read mode
[640]1158    remote_queuelock_release( parent_lock_xp );
[408]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
[23]1165    uint32_t i;
[408]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;
[625]1169    for( i = 0 ; i < 32 ; i++ ) 
1170    {
1171        xlist_root_init( XPTR( local_cxy , &child_vmm->mmap_mgr.zombi_list[i] ) );
1172    }
[23]1173
[178]1174    // initialize instrumentation counters
[635]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;
[23]1181
[408]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));
[23]1188
[408]1189    child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point));
[23]1190
[124]1191    hal_fence();
[23]1192
[438]1193#if DEBUG_VMM_FORK_COPY
[433]1194cycle = (uint32_t)hal_get_cycles();
[438]1195if( DEBUG_VMM_FORK_COPY < cycle )
[595]1196printk("\n[%s] thread[%x,%x] exit successfully / cycle %d\n",
1197__FUNCTION__ , this->process->pid, this->trdid , cycle );
[433]1198#endif
1199
[23]1200    return 0;
1201
[408]1202}  // vmm_fork_copy()
[204]1203
[1]1204///////////////////////////////////////
1205void vmm_destroy( process_t * process )
1206{
[408]1207    xptr_t   vseg_xp;
[1]1208        vseg_t * vseg;
1209
[438]1210#if DEBUG_VMM_DESTROY
[635]1211uint32_t   cycle = (uint32_t)hal_get_cycles();
1212thread_t * this  = CURRENT_THREAD;
[438]1213if( DEBUG_VMM_DESTROY < cycle )
[595]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 );
[433]1216#endif
[416]1217
[438]1218#if (DEBUG_VMM_DESTROY & 1 )
[443]1219if( DEBUG_VMM_DESTROY < cycle )
[635]1220hal_vmm_display( XPTR( local_cxy, process ) , true );
[437]1221#endif
1222
[433]1223    // get pointer on local VMM
[1]1224    vmm_t  * vmm = &process->vmm;
1225
[625]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 );
[408]1229
[625]1230    // take the VSL lock
[640]1231    remote_queuelock_acquire( vsl_lock_xp );
[625]1232
[611]1233    // scan the VSL to delete all registered vsegs
[625]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 )
[1]1240        {
[625]1241        // save extended pointer on next item in xlist
1242        next_xp = hal_remote_l64( iter_xp );
[409]1243
[625]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
[611]1248        // delete vseg and release physical pages
[625]1249        vmm_remove_vseg( process , vseg );
[409]1250
[443]1251#if( DEBUG_VMM_DESTROY & 1 )
1252if( DEBUG_VMM_DESTROY < cycle )
[611]1253printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n",
[443]1254__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1255#endif
1256
[1]1257        }
1258
[625]1259    // release the VSL lock
[640]1260    remote_queuelock_release( vsl_lock_xp );
[625]1261
1262    // remove all registered MMAP vsegs
1263    // from zombi_lists in MMAP allocator
[1]1264    uint32_t i;
1265    for( i = 0 ; i<32 ; i++ )
1266    {
[625]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 ) )
[1]1272            {
[625]1273                    vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
1274            vseg    = GET_PTR( vseg_xp );
[443]1275
1276#if( DEBUG_VMM_DESTROY & 1 )
1277if( DEBUG_VMM_DESTROY < cycle )
[595]1278printk("\n[%s] found zombi vseg / vpn_base %x / vpn_size %d\n",
[443]1279__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1280#endif
[611]1281            // clean vseg descriptor
1282            vseg->vmm = NULL;
1283
[625]1284            // remove vseg from  zombi_list
[611]1285            xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
1286
1287                    // release vseg descriptor
[1]1288            vseg_free( vseg );
[443]1289
1290#if( DEBUG_VMM_DESTROY & 1 )
1291if( DEBUG_VMM_DESTROY < cycle )
[595]1292printk("\n[%s] zombi vseg released / vpn_base %x / vpn_size %d\n",
[443]1293__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
1294#endif
[1]1295            }
1296    }
1297
[409]1298    // release memory allocated to the GPT itself
[1]1299    hal_gpt_destroy( &vmm->gpt );
1300
[438]1301#if DEBUG_VMM_DESTROY
[433]1302cycle = (uint32_t)hal_get_cycles();
[438]1303if( DEBUG_VMM_DESTROY < cycle )
[595]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 );
[433]1306#endif
[416]1307
[204]1308}  // end vmm_destroy()
1309
[1]1310/////////////////////////////////////////////////
1311vseg_t * vmm_check_conflict( process_t * process,
[21]1312                             vpn_t       vpn_base,
[1]1313                             vpn_t       vpn_size )
1314{
1315    vmm_t        * vmm = &process->vmm;
[408]1316
1317    // scan the VSL
[1]1318        vseg_t       * vseg;
[408]1319    xptr_t         iter_xp;
1320    xptr_t         vseg_xp;
1321    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[1]1322
[408]1323        XLIST_FOREACH( root_xp , iter_xp )
[1]1324        {
[408]1325                vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1326        vseg    = GET_PTR( vseg_xp );
[204]1327
[21]1328                if( ((vpn_base + vpn_size) > vseg->vpn_base) &&
1329             (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg;
[1]1330        }
1331    return NULL;
1332
[204]1333}  // end vmm_check_conflict()
1334
[407]1335////////////////////////////////////////////////
1336vseg_t * vmm_create_vseg( process_t   * process,
1337                              vseg_type_t   type,
[635]1338                          intptr_t      base,         // ltid for VSEG_TYPE_STACK
[407]1339                              uint32_t      size,
1340                          uint32_t      file_offset,
1341                          uint32_t      file_size,
1342                          xptr_t        mapper_xp,
1343                          cxy_t         cxy )
[1]1344{
1345    vseg_t     * vseg;          // created vseg pointer
[204]1346    vpn_t        vpn_base;      // first page index
[595]1347    vpn_t        vpn_size;      // number of pages covered by vseg
[1]1348        error_t      error;
1349
[640]1350#if DEBUG_VMM_CREATE_VSEG
1351thread_t * this  = CURRENT_THREAD;
1352uint32_t   cycle;
1353#endif
1354
[635]1355#if (DEBUG_VMM_CREATE_VSEG & 1)
[640]1356cycle = (uint32_t)hal_get_cycles();
[438]1357if( DEBUG_VMM_CREATE_VSEG < cycle )
[635]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 );
[433]1361#endif
[21]1362
[407]1363    // get pointer on VMM
1364        vmm_t * vmm    = &process->vmm;
[21]1365
[204]1366    // compute base, size, vpn_base, vpn_size, depending on vseg type
[407]1367    // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs
[595]1368
[1]1369    if( type == VSEG_TYPE_STACK )
1370    {
1371        // get vpn_base and vpn_size from STACK allocator
[625]1372        vmm_stack_alloc( vmm , base , &vpn_base , &vpn_size );
[1]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    }
[595]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    }
[21]1404    else if( (type == VSEG_TYPE_ANON) ||
[1]1405             (type == VSEG_TYPE_REMOTE) )
1406    {
[595]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       
[1]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
[595]1420        // set vseg base (always aligned for ANON or REMOTE)
[1]1421        base = vpn_base << CONFIG_PPM_PAGE_SHIFT;
1422    }
[623]1423    else    // VSEG_TYPE_DATA, VSEG_TYPE_CODE or KERNEL vseg
[1]1424    {
[204]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;
[1]1430    }
1431
1432    // check collisions
1433    vseg = vmm_check_conflict( process , vpn_base , vpn_size );
[624]1434
[1]1435    if( vseg != NULL )
1436    {
[614]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",
[407]1439        __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size );
[1]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",
[407]1448        __FUNCTION__ , process->pid );
[1]1449        return NULL;
1450        }
1451
[635]1452#if (DEBUG_VMM_CREATE_VSEG & 1)
[614]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
[1]1458    // initialize vseg descriptor
[407]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 );
[1]1469
[625]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
[640]1474    remote_queuelock_acquire( lock_xp );
[625]1475
[408]1476    // attach vseg to VSL
[611]1477        vmm_attach_vseg_to_vsl( vmm , vseg );
[1]1478
[625]1479    // release the VSL lock
[640]1480    remote_queuelock_release( lock_xp );
[625]1481
[438]1482#if DEBUG_VMM_CREATE_VSEG
[433]1483cycle = (uint32_t)hal_get_cycles();
[640]1484// if( DEBUG_VMM_CREATE_VSEG < cycle )
1485if( type == VSEG_TYPE_REMOTE )
[635]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 );
[433]1489#endif
[21]1490
[1]1491        return vseg;
1492
[406]1493}  // vmm_create_vseg()
1494
[640]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;
[625]1509
[640]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
[625]1588//////////////////////////////////////////
1589void vmm_remove_vseg( process_t * process,
1590                      vseg_t    * vseg )
[1]1591{
[625]1592    uint32_t    vseg_type;  // vseg type
[21]1593    vpn_t       vpn;        // VPN of current PTE
1594    vpn_t       vpn_min;    // VPN of first PTE
[1]1595    vpn_t       vpn_max;    // VPN of last PTE (excluded)
[409]1596    ppn_t       ppn;        // current PTE ppn value
1597    uint32_t    attr;       // current PTE attributes
[1]1598
[625]1599// check arguments
1600assert( (process != NULL), "process argument is NULL" );
1601assert( (vseg    != NULL), "vseg argument is NULL" );
[409]1602
[625]1603    // get pointers on local process VMM
[640]1604    vmm_t * vmm = &process->vmm;
[611]1605
[629]1606    // build extended pointer on GPT
[640]1607    xptr_t gpt_xp = XPTR( local_cxy , &vmm->gpt );
[629]1608
[623]1609    // get relevant vseg infos
[624]1610    vseg_type = vseg->type;
1611    vpn_min   = vseg->vpn_base;
1612    vpn_max   = vpn_min + vseg->vpn_size;
[623]1613
[625]1614#if DEBUG_VMM_REMOVE_VSEG
1615uint32_t   cycle = (uint32_t)hal_get_cycles();
1616thread_t * this  = CURRENT_THREAD;
[640]1617#endif
1618
1619#if (DEBUG_VMM_REMOVE_VSEG & 1 )
[625]1620if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1621printk("\n[%s] thread[%x,%x] enters / process %x / type %s / base %x / cycle %d\n",
[625]1622__FUNCTION__, this->process->pid, this->trdid, 
1623process->pid, vseg_type_str(vseg->type), vseg->min, cycle );
1624#endif
1625
[640]1626    // loop on PTEs in GPT to unmap all mapped PTE
[1]1627        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
1628    {
[625]1629        // get ppn and attr
[629]1630        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[409]1631
[625]1632        if( attr & GPT_MAPPED )  // PTE is mapped
[409]1633        { 
[437]1634
[625]1635#if( DEBUG_VMM_REMOVE_VSEG & 1 )
1636if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1637printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / type %s\n",
[640]1638__FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) );
[437]1639#endif
[585]1640            // unmap GPT entry in local GPT
[629]1641            hal_gpt_reset_pte( gpt_xp , vpn );
[409]1642
[640]1643            // release physical page when required
1644            vmm_ppn_release( process , vseg , ppn );
[409]1645        }
[1]1646    }
[433]1647
[625]1648    // remove vseg from VSL
[611]1649    vmm_detach_vseg_from_vsl( vmm , vseg );
1650
[625]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
[433]1674cycle = (uint32_t)hal_get_cycles();
[625]1675if( DEBUG_VMM_REMOVE_VSEG < cycle )
[641]1676printk("\n[%s] thread[%x,%x] exit / process %x / type %s / base %x / cycle %d\n",
[625]1677__FUNCTION__, this->process->pid, this->trdid, 
1678process->pid, vseg_type_str(vseg->type), vseg->min, cycle );
[433]1679#endif
1680
[625]1681}  // end vmm_remove_vseg()
[1]1682
[611]1683/////////////////////////////////////////////
[640]1684void vmm_resize_vseg( process_t * process,
1685                      vseg_t    * vseg,
1686                      intptr_t    new_base,
1687                      intptr_t    new_size )
[406]1688{
[640]1689    vpn_t     vpn;
1690    ppn_t     ppn;
1691    uint32_t  attr;
[406]1692
[640]1693// check arguments
1694assert( (process != NULL), "process argument is NULL" );
1695assert( (vseg    != NULL), "vseg argument is NULL" );
[406]1696
[623]1697#if DEBUG_VMM_RESIZE_VSEG
1698uint32_t   cycle = (uint32_t)hal_get_cycles();
1699thread_t * this  = CURRENT_THREAD;
[640]1700#endif
1701
1702#if (DEBUG_VMM_RESIZE_VSEG & 1)
[623]1703if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]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 );
[623]1707#endif
1708
[640]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;
[1]1712
[640]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;
[1]1718
[640]1719    // build extended pointer on GPT
1720    xptr_t gpt_xp = XPTR( local_cxy , &process->vmm.gpt );
[1]1721
[640]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++ )
[623]1724    {
[640]1725        // get ppn and attr
1726        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[21]1727
[640]1728        if( attr & GPT_MAPPED )  // PTE is mapped
1729        { 
[623]1730
1731#if( DEBUG_VMM_RESIZE_VSEG & 1 )
1732if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]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) );
[623]1735#endif
[640]1736            // unmap GPT entry
1737            hal_gpt_reset_pte( gpt_xp , vpn );
[623]1738
[640]1739            // release physical page when required
1740            vmm_ppn_release( process , vseg , ppn );
1741        }
[1]1742    }
[640]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++ )
[1]1746    {
[640]1747        // get ppn and attr
1748        hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn );
[623]1749
[640]1750        if( attr & GPT_MAPPED )  // PTE is mapped
1751        { 
1752
[641]1753#if( DEBUG_VMM_RESIZE_VSEG & 1 )
[623]1754if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]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) );
[623]1757#endif
[640]1758            // unmap GPT entry in local GPT
1759            hal_gpt_reset_pte( gpt_xp , vpn );
[406]1760
[640]1761            // release physical page when required
1762            vmm_ppn_release( process , vseg , ppn );
1763        }
[1]1764    }
[623]1765
[640]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();
[623]1774if( DEBUG_VMM_RESIZE_VSEG < cycle )
[640]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 );
[623]1778#endif
[406]1779
[640]1780}  // end vmm_resize_vseg
[623]1781
[640]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;
[623]1800
[640]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 );
[623]1804
[640]1805    // build extended pointer on VSL root
1806    xptr_t root_xp = XPTR( vmm_cxy , &vmm_ptr->vsegs_root );
[406]1807
[640]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 );
[406]1813
[640]1814        min = hal_remote_l32( XPTR( vmm_cxy , &vseg->min ) );
1815        max = hal_remote_l32( XPTR( vmm_cxy , &vseg->max ) );
[407]1816
[640]1817        // return success when match
1818        if( (vaddr >= min) && (vaddr < max) ) return vseg;
[1]1819    }
1820
[640]1821    // return failure
1822    return NULL;
[1]1823
[640]1824}  // end vmm_vseg_from_vaddr()
[1]1825
1826///////////////////////////////////////////
[388]1827error_t  vmm_get_vseg( process_t * process,
[394]1828                       intptr_t    vaddr,
[388]1829                       vseg_t   ** found_vseg )
[1]1830{
[640]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
[1]1835
[640]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 );
[1]1841
[440]1842    // try to get vseg from local VMM
[640]1843    loc_vseg = vmm_vseg_from_vaddr( XPTR( local_cxy, &process->vmm ) , vaddr );
[440]1844
[640]1845    if (loc_vseg == NULL)   // vseg not found => access reference VSL
1846    {
[388]1847        // get extended pointer on reference process
1848        xptr_t ref_xp = process->ref_xp;
[1]1849
[640]1850        // get cluster and local pointer on reference process
[388]1851        cxy_t       ref_cxy = GET_CXY( ref_xp );
[433]1852        process_t * ref_ptr = GET_PTR( ref_xp );
[388]1853
[640]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 );
[388]1859
[640]1860        // try to get vseg from reference VMM
1861        ref_vseg = vmm_vseg_from_vaddr( XPTR( ref_cxy , &ref_ptr->vmm ) , vaddr );
[388]1862
[640]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 );
[388]1867
[640]1868            // release reference VSL lock
1869            remote_queuelock_release( ref_lock_xp );
[388]1870
[640]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();
[625]1877
[640]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 );
[595]1882
[640]1883                // release reference VSL & local VSL locks
1884                remote_queuelock_release( ref_lock_xp );
1885                remote_queuelock_release( loc_lock_xp );
[388]1886
[640]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    }
[388]1914}  // end vmm_get_vseg()
1915
[407]1916//////////////////////////////////////////////////////////////////////////////////////
1917// This static function compute the target cluster to allocate a physical page
[632]1918// for a given <vpn> in a given <vseg>, allocates the page and returns an extended
1919// pointer on the allocated page descriptor.
[407]1920// The vseg cannot have the FILE type.
1921//////////////////////////////////////////////////////////////////////////////////////
[640]1922// @ vseg   : local pointer on vseg.
1923// @ vpn    : unmapped vpn.
1924// @ return an extended pointer on the allocated page
1925//////////////////////////////////////////////////////////////////////////////////////
[407]1926static xptr_t vmm_page_allocate( vseg_t * vseg,
1927                                 vpn_t    vpn )
1928{
[433]1929
[632]1930#if DEBUG_VMM_PAGE_ALLOCATE
[619]1931uint32_t   cycle   = (uint32_t)hal_get_cycles();
1932thread_t * this    = CURRENT_THREAD;
[632]1933if( DEBUG_VMM_PAGE_ALLOCATE < cycle )
[595]1934printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n",
1935__FUNCTION__ , this->process->pid, this->trdid, vpn, cycle );
[433]1936#endif
1937
[632]1938    xptr_t       page_xp;
[407]1939    cxy_t        page_cxy;
[635]1940    page_t     * page_ptr;
[577]1941    uint32_t     index;
[407]1942
[577]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;
[407]1947
[567]1948// check vseg type
1949assert( ( type != VSEG_TYPE_FILE ) , "illegal vseg type\n" );
[407]1950
1951    if( flags & VSEG_DISTRIB )    // distributed => cxy depends on vpn LSB
1952    {
[577]1953        index    = vpn & ((x_size * y_size) - 1);
1954        page_cxy = HAL_CXY_FROM_XY( (index / y_size) , (index % y_size) );
[561]1955
[577]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();
[561]1960        }
[407]1961    }
1962    else                          // other cases => cxy specified in vseg
1963    {
[561]1964        page_cxy = vseg->cxy;
[407]1965    }
1966
[635]1967    // allocate one small physical page from target cluster
1968    page_ptr = ppm_remote_alloc_pages( page_cxy , 0 );
[407]1969
[635]1970    page_xp = XPTR( page_cxy , page_ptr );
1971
[632]1972#if DEBUG_VMM_PAGE_ALLOCATE
[595]1973cycle = (uint32_t)hal_get_cycles();
[632]1974if( DEBUG_VMM_PAGE_ALLOCATE < cycle )
[635]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 );
[433]1977#endif
1978
[632]1979    return page_xp;
[407]1980
1981}  // end vmm_page_allocate() 
1982
[313]1983////////////////////////////////////////
1984error_t vmm_get_one_ppn( vseg_t * vseg,
1985                         vpn_t    vpn,
1986                         ppn_t  * ppn )
1987{
1988    error_t    error;
[407]1989    xptr_t     page_xp;           // extended pointer on physical page descriptor
[606]1990    uint32_t   page_id;           // missing page index in vseg mapper
[406]1991    uint32_t   type;              // vseg type;
[313]1992
[406]1993    type      = vseg->type;
[606]1994    page_id   = vpn - vseg->vpn_base;
[313]1995
[438]1996#if DEBUG_VMM_GET_ONE_PPN
[595]1997uint32_t   cycle = (uint32_t)hal_get_cycles();
1998thread_t * this  = CURRENT_THREAD;
[635]1999if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
[606]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 );
[433]2002#endif
[313]2003
[406]2004    // FILE type : get the physical page from the file mapper
[313]2005    if( type == VSEG_TYPE_FILE )
2006    {
[406]2007        // get extended pointer on mapper
[407]2008        xptr_t mapper_xp = vseg->mapper_xp;
[313]2009
[567]2010assert( (mapper_xp != XPTR_NULL),
2011"mapper not defined for a FILE vseg\n" );
[406]2012       
[606]2013        // get extended pointer on page descriptor
2014        page_xp = mapper_remote_get_page( mapper_xp , page_id );
[406]2015
[606]2016        if ( page_xp == XPTR_NULL ) return EINVAL;
[313]2017    }
2018
[406]2019    // Other types : allocate a physical page from target cluster,
[407]2020    // as defined by vseg type and vpn value
[313]2021    else
2022    {
[433]2023        // allocate one physical page
[407]2024        page_xp = vmm_page_allocate( vseg , vpn );
[406]2025
[635]2026        if( page_xp == XPTR_NULL ) return -1;
[313]2027
[406]2028        // initialise missing page from .elf file mapper for DATA and CODE types
[440]2029        // the vseg->mapper_xp field is an extended pointer on the .elf file mapper
[313]2030        if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) )
2031        {
[406]2032            // get extended pointer on mapper
2033            xptr_t     mapper_xp = vseg->mapper_xp;
[313]2034
[567]2035assert( (mapper_xp != XPTR_NULL),
2036"mapper not defined for a CODE or DATA vseg\n" );
[406]2037       
2038            // compute missing page offset in vseg
[606]2039            uint32_t offset = page_id << CONFIG_PPM_PAGE_SHIFT;
[406]2040
[313]2041            // compute missing page offset in .elf file
[406]2042            uint32_t elf_offset = vseg->file_offset + offset;
[313]2043
[438]2044#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[635]2045if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
[595]2046printk("\n[%s] thread[%x,%x] for vpn = %x / elf_offset = %x\n",
2047__FUNCTION__, this->process->pid, this->trdid, vpn, elf_offset );
[433]2048#endif
[406]2049            // compute extended pointer on page base
[407]2050            xptr_t base_xp  = ppm_page2base( page_xp );
[313]2051
[406]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
[313]2056            {
[406]2057
[438]2058#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[635]2059if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
[595]2060printk("\n[%s] thread[%x,%x] for vpn  %x / fully in BSS\n",
2061__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]2062#endif
[407]2063                if( GET_CXY( page_xp ) == local_cxy )
[313]2064                {
[315]2065                    memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE );
[313]2066                }
2067                else
2068                {
[315]2069                   hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE );       
[313]2070                }
2071            }
[406]2072            else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) )  // fully in  mapper
[315]2073            {
[406]2074
[438]2075#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[635]2076if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
[595]2077printk("\n[%s] thread[%x,%x] for vpn  %x / fully in mapper\n",
2078__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]2079#endif
[606]2080                error = mapper_move_kernel( mapper_xp,
2081                                            true,             // to_buffer
2082                                            elf_offset,
2083                                            base_xp,
2084                                            CONFIG_PPM_PAGE_SIZE ); 
[313]2085                if( error ) return EINVAL;
2086            }
[406]2087            else  // both in mapper and in BSS :
2088                  // - (file_size - offset)             bytes from mapper
2089                  // - (page_size + offset - file_size) bytes from BSS
[313]2090            {
[406]2091
[438]2092#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[635]2093if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
[610]2094printk("\n[%s] thread[%x,%x] for vpn  %x / both mapper & BSS\n"
[433]2095"      %d bytes from mapper / %d bytes from BSS\n",
[595]2096__FUNCTION__, this->process->pid, this->trdid, vpn,
[407]2097file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size  );
[433]2098#endif
[313]2099                // initialize mapper part
[606]2100                error = mapper_move_kernel( mapper_xp,
2101                                            true,         // to buffer
2102                                            elf_offset,
2103                                            base_xp,
2104                                            file_size - offset ); 
[313]2105                if( error ) return EINVAL;
2106
2107                // initialize BSS part
[407]2108                if( GET_CXY( page_xp ) == local_cxy )
[313]2109                {
[406]2110                    memset( GET_PTR( base_xp ) + file_size - offset , 0 , 
2111                            offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]2112                }
2113                else
2114                {
[406]2115                   hal_remote_memset( base_xp + file_size - offset , 0 , 
2116                                      offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]2117                }
2118            }   
2119        }  // end initialisation for CODE or DATA types   
2120    } 
2121
2122    // return ppn
[407]2123    *ppn = ppm_page2ppn( page_xp );
[406]2124
[438]2125#if DEBUG_VMM_GET_ONE_PPN
[595]2126cycle = (uint32_t)hal_get_cycles();
[635]2127if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) )
2128printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n",
[595]2129__FUNCTION__ , this->process->pid, this->trdid , vpn , *ppn, cycle );
[433]2130#endif
[406]2131
[313]2132    return 0;
2133
2134}  // end vmm_get_one_ppn()
2135
[585]2136///////////////////////////////////////////////////
2137error_t vmm_handle_page_fault( process_t * process,
2138                               vpn_t       vpn )
[1]2139{
[585]2140    vseg_t         * vseg;            // vseg containing vpn
[629]2141    uint32_t         attr;            // PTE_ATTR value
2142    ppn_t            ppn;             // PTE_PPN value
[585]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
[1]2150
[629]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
[625]2157#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2158if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[625]2159printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n",
[629]2160__FUNCTION__, this->process->pid, this->trdid, vpn, start_cycle );
[625]2161#endif
2162
[629]2163#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
[635]2164if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
2165hal_vmm_display( this->process , true );
[629]2166#endif
2167
[585]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    {
[629]2174        printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in registered vseg\n",
2175        __FUNCTION__ , vpn , process->pid, this->trdid );
[585]2176       
2177        return EXCP_USER_ERROR;
2178    }
2179
[635]2180#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2181if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[634]2182printk("\n[%s] thread[%x,%x] found vseg %s\n",
2183__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) );
[433]2184#endif
[406]2185
[629]2186    // build extended pointer on local GPT
2187    local_gpt_xp  = XPTR( local_cxy , &process->vmm.gpt );
2188
[632]2189    // lock PTE in local GPT and get current PPN and attributes
[629]2190    error = hal_gpt_lock_pte( local_gpt_xp,
2191                              vpn,
2192                              &attr,
2193                              &ppn );
2194    if( error )
[438]2195    {
[629]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    }
[407]2201
[635]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",
[634]2205__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy );
[632]2206#endif
2207
2208    // handle page fault only if local PTE still unmapped after lock
[629]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 );
[407]2214
[630]2215        /////////////// private vseg or (local == reference)
2216        /////////////// => access only the local GPT
[629]2217        if( (vseg->type == VSEG_TYPE_STACK) ||
2218            (vseg->type == VSEG_TYPE_CODE)  ||
2219            (ref_cxy    == local_cxy ) )
2220        {
[632]2221
[635]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() );
[632]2227#endif
2228            // allocate and initialise a physical page
[629]2229            error = vmm_get_one_ppn( vseg , vpn , &ppn );
[407]2230
[585]2231            if( error )
[408]2232            {
[629]2233                printk("\n[ERROR] in %s : no physical page / process = %x / vpn = %x\n",
[408]2234                __FUNCTION__ , process->pid , vpn );
[1]2235
[629]2236                // unlock PTE in local GPT
2237                hal_gpt_unlock_pte( local_gpt_xp , vpn );
[406]2238
[585]2239                return EXCP_KERNEL_PANIC;
[407]2240            }
2241
[629]2242            // define attr from vseg flags
[632]2243            attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE;
[629]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;
[407]2248
[629]2249            // set PTE to local GPT
[632]2250            // it unlocks this PTE
[629]2251            hal_gpt_set_pte( local_gpt_xp,
2252                             vpn,
2253                             attr,
2254                             ppn );
[585]2255
[629]2256#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2257uint32_t end_cycle = (uint32_t)hal_get_cycles();
[641]2258uint32_t cost      = end_cycle - start_cycle;
[629]2259#endif
[585]2260
2261#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2262if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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 );
[585]2265#endif
2266
[629]2267#if CONFIG_INSTRUMENTATION_PGFAULTS
2268this->info.local_pgfault_nr++;
[641]2269this->info.local_pgfault_cost += cost;
2270if( cost > this->info.local_pgfault_max ) this->info.local_pgfault_max = cost;
[629]2271#endif
2272            return EXCP_NON_FATAL;
[585]2273
[629]2274        }   // end local GPT access
[585]2275
[630]2276        /////////////////// public vseg and (local != reference)
2277        /////////////////// => access ref GPT to update local GPT
[629]2278        else                               
2279        {
[632]2280
[635]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() );
[632]2286#endif
[629]2287            // build extended pointer on reference GPT
2288            ref_gpt_xp = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
[585]2289
[632]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            }
[1]2305
[635]2306#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2307if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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
[585]2313            {
[629]2314                // update local GPT from reference GPT values
[632]2315                // this unlocks the PTE in local GPT
[629]2316                hal_gpt_set_pte( local_gpt_xp,
2317                                 vpn,
2318                                 ref_attr,
2319                                 ref_ppn );
[585]2320
[635]2321#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2322if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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                             
[635]2330#if (DEBUG_VMM_HANDLE_PAGE_FAULT &1)
2331if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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
[629]2336#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2337uint32_t end_cycle = (uint32_t)hal_get_cycles();
[641]2338uint32_t cost      = end_cycle - start_cycle;
[629]2339#endif
2340
[585]2341#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2342if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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 );
[433]2345#endif
[406]2346
[629]2347#if CONFIG_INSTRUMENTATION_PGFAULTS
2348this->info.false_pgfault_nr++;
[641]2349this->info.false_pgfault_cost += cost;
2350if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost;
[629]2351#endif
2352                return EXCP_NON_FATAL;
2353            }
[632]2354            else                            // true page fault
[629]2355            {
[585]2356                // allocate and initialise a physical page depending on the vseg type
[629]2357                error = vmm_get_one_ppn( vseg , vpn , &ppn );
[1]2358
[585]2359                if( error )
2360                {
2361                    printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
2362                    __FUNCTION__ , process->pid , vpn );
[313]2363
[632]2364                    // unlock PTE in local GPT and in reference GPT
[629]2365                    hal_gpt_unlock_pte( local_gpt_xp , vpn );
[632]2366                    hal_gpt_unlock_pte( ref_gpt_xp   , vpn );
[585]2367                   
[629]2368                    return EXCP_KERNEL_PANIC;
[585]2369                }
[1]2370
[629]2371                // define attr from vseg flags
[632]2372                attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE;
[629]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;
[585]2377
[635]2378#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2379if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]2380printk("\n[%s] thread[%x,%x] build a new PTE for a true pgfault\n",
2381__FUNCTION__, this->process->pid, this->trdid );
2382#endif
[629]2383                // set PTE in reference GPT
[632]2384                // this unlock the PTE
[629]2385                hal_gpt_set_pte( ref_gpt_xp,
2386                                 vpn,
2387                                 attr,
2388                                 ppn );
2389
[635]2390#if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1)
2391if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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
[629]2396                // set PTE in local GPT
[632]2397                // this unlock the PTE
[629]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();
[641]2405uint32_t cost      = end_cycle - start_cycle;
[629]2406#endif
2407
[440]2408#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2409if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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 );
[435]2412#endif
[629]2413
2414#if CONFIG_INSTRUMENTATION_PGFAULTS
2415this->info.global_pgfault_nr++;
[641]2416this->info.global_pgfault_cost += cost;
2417if( cost > this->info.global_pgfault_max ) this->info.global_pgfault_max = cost;
[629]2418#endif
2419                return EXCP_NON_FATAL;
2420            }
[585]2421        }
2422    }
[629]2423    else   // page has been locally mapped by another concurrent thread
2424    {
[632]2425        // unlock the PTE in local GPT
[629]2426        hal_gpt_unlock_pte( local_gpt_xp , vpn );
2427
[632]2428#if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT)
2429uint32_t end_cycle = (uint32_t)hal_get_cycles();
[641]2430uint32_t cost      = end_cycle - start_cycle;
[632]2431#endif
2432
2433#if DEBUG_VMM_HANDLE_PAGE_FAULT
[635]2434if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) )
[632]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++;
[641]2441this->info.false_pgfault_cost += cost;
2442if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost;
[632]2443#endif
[629]2444        return EXCP_NON_FATAL;
2445    }
2446
[585]2447}   // end vmm_handle_page_fault()
[435]2448
[585]2449////////////////////////////////////////////
2450error_t vmm_handle_cow( process_t * process,
2451                        vpn_t       vpn )
2452{
2453    vseg_t         * vseg;            // vseg containing vpn
[629]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
[585]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
[629]2461    cxy_t            ref_cxy;         // reference process cluster
2462    process_t      * ref_ptr;         // local pointer on reference process
[585]2463    error_t          error;
[1]2464
[629]2465    thread_t * this  = CURRENT_THREAD;
[625]2466
[585]2467#if DEBUG_VMM_HANDLE_COW
[629]2468uint32_t   cycle = (uint32_t)hal_get_cycles();
[640]2469if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[595]2470printk("\n[%s] thread[%x,%x] enter for vpn %x / core[%x,%d] / cycle %d\n",
[619]2471__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
[629]2472#endif
2473
[635]2474#if ((DEBUG_VMM_HANDLE_COW & 3) == 3 )
[640]2475hal_vmm_display( XPTR( local_cxy , process ) , true );
[585]2476#endif
2477
2478    // get local vseg
2479    error = vmm_get_vseg( process, 
2480                          (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT,
2481                          &vseg );
[440]2482    if( error )
[1]2483    {
[629]2484        printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in a registered vseg\n",
[625]2485        __FUNCTION__, vpn, process->pid, this->trdid );
[585]2486
[629]2487        return EXCP_USER_ERROR;
[440]2488    }
[407]2489
[629]2490#if DEBUG_VMM_HANDLE_COW
[640]2491if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[629]2492printk("\n[%s] thread[%x,%x] get vseg %s\n",
2493__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) );
[619]2494#endif
2495
[629]2496    // get reference process cluster and local pointer
[585]2497    ref_cxy = GET_CXY( process->ref_xp );
2498    ref_ptr = GET_PTR( process->ref_xp );
[407]2499
[629]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
[585]2503    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[440]2504    {
[629]2505        gpt_cxy = local_cxy;
2506        gpt_ptr = &process->vmm.gpt;
2507        gpt_xp  = XPTR( gpt_cxy , gpt_ptr );
[1]2508    }
[440]2509    else
[1]2510    {
[629]2511        gpt_cxy = ref_cxy;
2512        gpt_ptr = &ref_ptr->vmm.gpt;
2513        gpt_xp  = XPTR( gpt_cxy , gpt_ptr );
[1]2514    }
2515
[629]2516    // lock target PTE in relevant GPT (local or reference)
[632]2517    // and get current PTE value
[629]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    }
[441]2529
[629]2530#if DEBUG_VMM_HANDLE_COW
[640]2531if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]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
[629]2536    // return user error if COW attribute not set or PTE2 unmapped
2537    if( ((old_attr & GPT_COW) == 0) || ((old_attr & GPT_MAPPED) == 0) )
[585]2538    {
[629]2539        hal_gpt_unlock_pte( gpt_xp , vpn );
[407]2540
[629]2541        return EXCP_USER_ERROR;
[407]2542    }
2543
[619]2544    // get pointers on physical page descriptor
[585]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 );
[435]2548
[585]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 );
[407]2552
[585]2553    // take lock protecting "forks" counter
2554    remote_busylock_acquire( forks_lock_xp );
[407]2555
[585]2556    // get number of pending forks from page descriptor
2557    uint32_t forks = hal_remote_l32( forks_xp );
[441]2558
[629]2559#if DEBUG_VMM_HANDLE_COW
[640]2560if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]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
[585]2565    if( forks )        // pending fork => allocate a new page, and copy old to new
2566    {
[619]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
[629]2573        // allocate a new physical page depending on vseg type
[585]2574        page_xp = vmm_page_allocate( vseg , vpn );
[619]2575
[585]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 );
[441]2580
[629]2581            hal_gpt_unlock_pte( gpt_xp , vpn ); 
[441]2582
[585]2583            return EXCP_KERNEL_PANIC;
2584        }
[441]2585
[585]2586        // compute allocated page PPN
2587        new_ppn = ppm_page2ppn( page_xp );
[441]2588
[629]2589#if DEBUG_VMM_HANDLE_COW
[640]2590if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]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
[585]2595        // copy old page content to new page
[619]2596        hal_remote_memcpy( ppm_ppn2base( new_ppn ),
2597                           ppm_ppn2base( old_ppn ),
2598                           CONFIG_PPM_PAGE_SIZE );
[441]2599
[629]2600#if DEBUG_VMM_HANDLE_COW
[640]2601if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[619]2602printk("\n[%s] thread[%x,%x] copied old page to new page\n",
2603__FUNCTION__, this->process->pid, this->trdid );
[585]2604#endif
[440]2605
[585]2606    }             
2607    else               // no pending fork => keep the existing page
2608    {
[619]2609        // release lock protecting "forks" counter
2610        remote_busylock_release( forks_lock_xp );
[1]2611
[585]2612#if(DEBUG_VMM_HANDLE_COW & 1)
[640]2613if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[635]2614printk("\n[%s] thread[%x,%x] no pending forks / keep existing PPN %x\n",
[619]2615__FUNCTION__, this->process->pid, this->trdid, old_ppn );
[585]2616#endif
2617        new_ppn = old_ppn;
2618    }
[1]2619
[629]2620    // build new_attr : set WRITABLE, reset COW, reset LOCKED
2621    new_attr = (((old_attr | GPT_WRITABLE) & (~GPT_COW)) & (~GPT_LOCKED));
[585]2622
[635]2623#if(DEBUG_VMM_HANDLE_COW & 1)
[640]2624if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[635]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
[629]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
[585]2632    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[1]2633    {
[635]2634        // set new PTE in local gpt
[585]2635        hal_gpt_set_pte( gpt_xp,
2636                         vpn,
2637                         new_attr,
2638                         new_ppn );
[1]2639    }
[585]2640    else
[1]2641    {
[640]2642        // set new PTE in all GPT copies
2643        vmm_global_update_pte( process,
2644                               vpn,
2645                               new_attr,
2646                               new_ppn );
[1]2647    }
2648
[585]2649#if DEBUG_VMM_HANDLE_COW
2650cycle = (uint32_t)hal_get_cycles();
[640]2651if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) )
[595]2652printk("\n[%s] thread[%x,%x] exit for vpn %x / core[%x,%d] / cycle %d\n",
[619]2653__FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
[585]2654#endif
[313]2655
[635]2656#if ((DEBUG_VMM_HANDLE_COW & 3) == 3)
[640]2657hal_vmm_display( XPTR( local_cxy , process ) , true );
[635]2658#endif
2659
[585]2660     return EXCP_NON_FATAL;
[1]2661
[585]2662}   // end vmm_handle_cow()
2663
Note: See TracBrowser for help on using the repository browser.