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

Last change on this file since 618 was 617, checked in by alain, 5 years ago

cosmetic

File size: 73.4 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)
5 *           Mohamed Lamine Karaoui (2015)
[595]6 *           Alain Greiner (2016,2017,2018)
[21]7 *
[1]8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
[14]26#include <kernel_config.h>
[457]27#include <hal_kernel_types.h>
[1]28#include <hal_special.h>
29#include <hal_gpt.h>
[409]30#include <hal_vmm.h>
[577]31#include <hal_macros.h>
[1]32#include <printk.h>
[23]33#include <memcpy.h>
[567]34#include <remote_rwlock.h>
35#include <remote_queuelock.h>
[1]36#include <list.h>
[408]37#include <xlist.h>
[1]38#include <bits.h>
39#include <process.h>
40#include <thread.h>
41#include <vseg.h>
42#include <cluster.h>
43#include <scheduler.h>
44#include <vfs.h>
45#include <mapper.h>
46#include <page.h>
47#include <kmem.h>
48#include <vmm.h>
[585]49#include <hal_exception.h>
[1]50
51//////////////////////////////////////////////////////////////////////////////////
52//   Extern global variables
53//////////////////////////////////////////////////////////////////////////////////
54
[567]55extern  process_t  process_zero;      // allocated in cluster.c
[1]56
[415]57///////////////////////////////////////
58error_t vmm_init( process_t * process )
[21]59{
[1]60    error_t   error;
61    vseg_t  * vseg_kentry;
62    vseg_t  * vseg_args;
63    vseg_t  * vseg_envs;
64    intptr_t  base;
65    intptr_t  size;
[614]66    uint32_t  i;
[1]67
[438]68#if DEBUG_VMM_INIT
[567]69thread_t * this = CURRENT_THREAD;
[433]70uint32_t cycle = (uint32_t)hal_get_cycles();
[438]71if( DEBUG_VMM_INIT )
[614]72printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", 
73__FUNCTION__ , this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]74#endif
[204]75
[1]76    // get pointer on VMM
77    vmm_t   * vmm = &process->vmm;
78
[407]79    // initialize local list of vsegs
80    vmm->vsegs_nr = 0;
[408]81        xlist_root_init( XPTR( local_cxy , &vmm->vsegs_root ) );
[580]82        remote_rwlock_init( XPTR( local_cxy , &vmm->vsegs_lock ) , LOCK_VMM_VSL );
[407]83
[567]84assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) 
85<= CONFIG_VMM_ELF_BASE) , "UTILS zone too small\n" );
[21]86
[567]87assert( (CONFIG_THREADS_MAX_PER_CLUSTER <= 32) ,
88"no more than 32 threads per cluster for a single process\n");
[1]89
[567]90assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREADS_MAX_PER_CLUSTER) <=
91(CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) ,
92"STACK zone too small\n");
[1]93
[409]94    // register kentry vseg in VSL
[406]95    base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT;
[1]96    size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]97
[407]98    vseg_kentry = vmm_create_vseg( process,
99                                   VSEG_TYPE_CODE,
100                                   base,
101                                   size,
102                                   0,             // file_offset unused
103                                   0,             // file_size unused
104                                   XPTR_NULL,     // mapper_xp unused
105                                   local_cxy );
[204]106
[415]107    if( vseg_kentry == NULL )
108    {
109        printk("\n[ERROR] in %s : cannot register kentry vseg\n", __FUNCTION__ );
110        return -1;
111    }
[204]112
[406]113    vmm->kent_vpn_base = base;
[1]114
[409]115    // register args vseg in VSL
[406]116    base = (CONFIG_VMM_KENTRY_BASE + 
117            CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT;
[1]118    size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]119
[407]120    vseg_args = vmm_create_vseg( process,
121                                 VSEG_TYPE_DATA,
122                                 base,
123                                 size,
124                                 0,             // file_offset unused
125                                 0,             // file_size unused
126                                 XPTR_NULL,     // mapper_xp unused
127                                 local_cxy );
[204]128
[415]129    if( vseg_args == NULL )
130    {
131        printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ );
132        return -1;
133    }
[204]134
[406]135    vmm->args_vpn_base = base;
[1]136
[409]137    // register the envs vseg in VSL
[406]138    base = (CONFIG_VMM_KENTRY_BASE + 
139            CONFIG_VMM_KENTRY_SIZE +
140            CONFIG_VMM_ARGS_SIZE   ) << CONFIG_PPM_PAGE_SHIFT;
[1]141    size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT;
[406]142
[407]143    vseg_envs = vmm_create_vseg( process,
144                                 VSEG_TYPE_DATA,
145                                 base,
146                                 size,
147                                 0,             // file_offset unused
148                                 0,             // file_size unused
149                                 XPTR_NULL,     // mapper_xp unused
150                                 local_cxy );
[204]151
[415]152    if( vseg_envs == NULL )
153    {
154        printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ );
155        return -1;
156    }
[204]157
[406]158    vmm->envs_vpn_base = base;
[1]159
[409]160    // create GPT (empty)
[1]161    error = hal_gpt_create( &vmm->gpt );
162
[415]163    if( error ) 
164    printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ );
[204]165
[585]166    // initialize GPT lock
167    remote_rwlock_init( XPTR( local_cxy , &vmm->gpt_lock ) , LOCK_VMM_GPT );
168
169    // architecture specic GPT initialisation
[409]170    // (For TSAR, identity map the kentry_vseg)
171    error = hal_vmm_init( vmm );
172
[415]173    if( error ) 
174    printk("\n[ERROR] in %s : cannot initialize GPT\n", __FUNCTION__ );
[409]175
[1]176    // initialize STACK allocator
177    vmm->stack_mgr.bitmap   = 0;
178    vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE;
[567]179    busylock_init( &vmm->stack_mgr.lock , LOCK_VMM_STACK );
[1]180
181    // initialize MMAP allocator
[407]182    vmm->mmap_mgr.vpn_base        = CONFIG_VMM_HEAP_BASE;
183    vmm->mmap_mgr.vpn_size        = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE;
184    vmm->mmap_mgr.first_free_vpn  = CONFIG_VMM_HEAP_BASE;
[567]185    busylock_init( &vmm->mmap_mgr.lock , LOCK_VMM_MMAP );
[1]186    for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] );
187
[21]188    // initialize instrumentation counters
[409]189        vmm->pgfault_nr = 0;
[1]190
[124]191    hal_fence();
[1]192
[438]193#if DEBUG_VMM_INIT
[433]194cycle = (uint32_t)hal_get_cycles();
[438]195if( DEBUG_VMM_INIT )
[614]196printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", 
197__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]198#endif
[204]199
[415]200    return 0;
201
[204]202}  // end vmm_init()
203
[407]204//////////////////////////////////////
205void vmm_display( process_t * process,
206                  bool_t      mapping )
207{
208    vmm_t * vmm = &process->vmm;
209    gpt_t * gpt = &vmm->gpt;
210
[457]211    printk("\n***** VSL and GPT(%x) for process %x in cluster %x\n\n",
212    process->vmm.gpt.ptr , process->pid , local_cxy );
[407]213
[585]214    // get lock protecting the VSL and the GPT
[567]215    remote_rwlock_rd_acquire( XPTR( local_cxy , &vmm->vsegs_lock ) );
[585]216    remote_rwlock_rd_acquire( XPTR( local_cxy , &vmm->gpt_lock ) );
[407]217
218    // scan the list of vsegs
[408]219    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
220    xptr_t         iter_xp;
221    xptr_t         vseg_xp;
[407]222    vseg_t       * vseg;
[408]223    XLIST_FOREACH( root_xp , iter_xp )
[407]224    {
[408]225        vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]226        vseg    = GET_PTR( vseg_xp );
[408]227
[407]228        printk(" - %s : base = %X / size = %X / npages = %d\n",
229        vseg_type_str( vseg->type ) , vseg->min , vseg->max - vseg->min , vseg->vpn_size );
230
231        if( mapping )
232        {
233            vpn_t    vpn;
234            ppn_t    ppn;
235            uint32_t attr;
236            vpn_t    base = vseg->vpn_base;
237            vpn_t    size = vseg->vpn_size;
238            for( vpn = base ; vpn < (base+size) ; vpn++ )
239            {
[585]240                hal_gpt_get_pte( XPTR( local_cxy , gpt ) , vpn , &attr , &ppn );
[407]241                if( attr & GPT_MAPPED )
242                {
243                    printk("    . vpn = %X / attr = %X / ppn = %X\n", vpn , attr , ppn );
244                }
245            }
246        }
247    }
248
[585]249    // release the locks
[567]250    remote_rwlock_rd_release( XPTR( local_cxy , &vmm->vsegs_lock ) );
[585]251    remote_rwlock_rd_release( XPTR( local_cxy , &vmm->gpt_lock ) );
[407]252
[408]253}  // vmm_display()
254
[611]255//////////////////////////////////////////
256void vmm_attach_vseg_to_vsl( vmm_t  * vmm,
257                             vseg_t * vseg )
[567]258{
259    // build extended pointer on rwlock protecting VSL
260    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
261
262    // get rwlock in write mode
263    remote_rwlock_wr_acquire( lock_xp );
264
265    // update vseg descriptor
266    vseg->vmm = vmm;
267
268    // add vseg in vmm list
269    xlist_add_last( XPTR( local_cxy , &vmm->vsegs_root ),
270                    XPTR( local_cxy , &vseg->xlist ) );
271
272    // release rwlock in write mode
273    remote_rwlock_wr_release( lock_xp );
274}
275
[611]276////////////////////////////////////////////
277void vmm_detach_vseg_from_vsl( vmm_t  * vmm,
278                               vseg_t * vseg )
[567]279{
[611]280    // get vseg type
281    uint32_t type = vseg->type;
282
[567]283    // build extended pointer on rwlock protecting VSL
284    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
285
286    // get rwlock in write mode
287    remote_rwlock_wr_acquire( lock_xp );
288
289    // update vseg descriptor
290    vseg->vmm = NULL;
291
[611]292    // remove vseg from VSL
[567]293    xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
294
295    // release rwlock in write mode
296    remote_rwlock_wr_release( lock_xp );
297
[611]298    // release the stack slot to VMM stack allocator if STACK type
299    if( type == VSEG_TYPE_STACK )
300    {
301        // get pointer on stack allocator
302        stack_mgr_t * mgr = &vmm->stack_mgr;
303
304        // compute slot index
305        uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE);
306
307        // update stacks_bitmap
308        busylock_acquire( &mgr->lock );
309        bitmap_clear( &mgr->bitmap , index );
310        busylock_release( &mgr->lock );
311    }
312
313    // release the vseg to VMM mmap allocator if MMAP type
314    if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) )
315    {
316        // get pointer on mmap allocator
317        mmap_mgr_t * mgr = &vmm->mmap_mgr;
318
319        // compute zombi_list index
320        uint32_t index = bits_log2( vseg->vpn_size );
321
322        // update zombi_list
323        busylock_acquire( &mgr->lock );
324        list_add_first( &mgr->zombi_list[index] , &vseg->zlist );
325        busylock_release( &mgr->lock );
326    }
327
328    // release physical memory allocated for vseg descriptor if no MMAP type
329    if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) )
330    {
331        vseg_free( vseg );
332    }
333
334}  // end vmm_remove_vseg_from_vsl()
335
[595]336////////////////////////////////////////////////
[433]337void vmm_global_update_pte( process_t * process,
338                            vpn_t       vpn,
339                            uint32_t    attr,
340                            ppn_t       ppn )
[23]341{
[408]342    xlist_entry_t * process_root_ptr;
343    xptr_t          process_root_xp;
344    xptr_t          process_iter_xp;
[23]345
[408]346    xptr_t          remote_process_xp;
347    cxy_t           remote_process_cxy;
348    process_t     * remote_process_ptr;
349    xptr_t          remote_gpt_xp;
[23]350
[408]351    pid_t           pid;
352    cxy_t           owner_cxy;
353    lpid_t          owner_lpid;
[23]354
[438]355#if DEBUG_VMM_UPDATE_PTE
[433]356uint32_t cycle = (uint32_t)hal_get_cycles();
[595]357thread_t * this = CURRENT_THREAD;
[438]358if( DEBUG_VMM_UPDATE_PTE < cycle )
[595]359printk("\n[%s] thread[%x,%x] enter for process %x / vpn %x / cycle %d\n",
360__FUNCTION__, this->process->pid, this->trdid, process->pid , vpn , cycle );
[433]361#endif
362
[567]363// check cluster is reference
[585]364assert( (GET_CXY( process->ref_xp ) == local_cxy) , "not called in reference cluster\n");
[433]365
[408]366    // get extended pointer on root of process copies xlist in owner cluster
367    pid              = process->pid;
368    owner_cxy        = CXY_FROM_PID( pid );
369    owner_lpid       = LPID_FROM_PID( pid );
370    process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid];
371    process_root_xp  = XPTR( owner_cxy , process_root_ptr );
[23]372
[408]373    // loop on destination process copies
374    XLIST_FOREACH( process_root_xp , process_iter_xp )
375    {
376        // get cluster and local pointer on remote process
377        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
[433]378        remote_process_ptr = GET_PTR( remote_process_xp );
[408]379        remote_process_cxy = GET_CXY( remote_process_xp );
[407]380
[438]381#if (DEBUG_VMM_UPDATE_PTE & 0x1)
382if( DEBUG_VMM_UPDATE_PTE < cycle )
[595]383printk("\n[%s] threadr[%x,%x] handling vpn %x for process %x in cluster %x\n",
384__FUNCTION__, this->process->pid, this->trdid, vpn, process->pid, remote_process_cxy );
[433]385#endif
386
[408]387        // get extended pointer on remote gpt
388        remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt );
389
[433]390        // update remote GPT
391        hal_gpt_update_pte( remote_gpt_xp, vpn, attr, ppn );
[408]392    } 
393
[438]394#if DEBUG_VMM_UPDATE_PTE
[433]395cycle = (uint32_t)hal_get_cycles();
[438]396if( DEBUG_VMM_UPDATE_PTE < cycle )
[595]397printk("\n[%s] thread[%x,%x] exit for process %x / vpn %x / cycle %d\n",
398__FUNCTION__, this->process->pid, this->trdid, process->pid , vpn , cycle );
[433]399#endif
400
401}  // end vmm_global_update_pte()
402
[408]403///////////////////////////////////////
404void vmm_set_cow( process_t * process )
405{
406    vmm_t         * vmm;
407
408    xlist_entry_t * process_root_ptr;
409    xptr_t          process_root_xp;
410    xptr_t          process_iter_xp;
411
412    xptr_t          remote_process_xp;
413    cxy_t           remote_process_cxy;
414    process_t     * remote_process_ptr;
415    xptr_t          remote_gpt_xp;
416
417    xptr_t          vseg_root_xp;
418    xptr_t          vseg_iter_xp;
419
420    xptr_t          vseg_xp;
421    vseg_t        * vseg;
422
423    pid_t           pid;
424    cxy_t           owner_cxy;
425    lpid_t          owner_lpid;
426
[438]427#if DEBUG_VMM_SET_COW
[595]428uint32_t   cycle = (uint32_t)hal_get_cycles();
429thread_t * this  = CURRENT_THREAD;
[438]430if( DEBUG_VMM_SET_COW < cycle )
[595]431printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n",
432__FUNCTION__, this->process->pid, this->trdid, process->pid , cycle );
[433]433#endif
[408]434
[567]435// check cluster is reference
436assert( (GET_CXY( process->ref_xp ) == local_cxy) ,
437"local cluster is not process reference cluster\n");
[408]438
439    // get pointer on reference VMM
440    vmm = &process->vmm;
441
442    // get extended pointer on root of process copies xlist in owner cluster
443    pid              = process->pid;
444    owner_cxy        = CXY_FROM_PID( pid );
445    owner_lpid       = LPID_FROM_PID( pid );
446    process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid];
447    process_root_xp  = XPTR( owner_cxy , process_root_ptr );
448
449    // get extended pointer on root of vsegs xlist from reference VMM
450    vseg_root_xp  = XPTR( local_cxy , &vmm->vsegs_root ); 
451
452    // loop on destination process copies
453    XLIST_FOREACH( process_root_xp , process_iter_xp )
454    {
455        // get cluster and local pointer on remote process
456        remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list );
[433]457        remote_process_ptr = GET_PTR( remote_process_xp );
[408]458        remote_process_cxy = GET_CXY( remote_process_xp );
459
[595]460#if (DEBUG_VMM_SET_COW & 1)
[438]461if( DEBUG_VMM_SET_COW < cycle )
[595]462printk("\n[%s] thread[%x,%x] handling process %x in cluster %x\n",
463__FUNCTION__, this->process->pid, this->trdid, process->pid , remote_process_cxy );
[433]464#endif
[408]465
466        // get extended pointer on remote gpt
467        remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt );
468
469        // loop on vsegs in (local) reference process VSL
470        XLIST_FOREACH( vseg_root_xp , vseg_iter_xp )
471        {
472            // get pointer on vseg
473            vseg_xp  = XLIST_ELEMENT( vseg_iter_xp , vseg_t , xlist );
[433]474            vseg     = GET_PTR( vseg_xp );
[408]475
[567]476assert( (GET_CXY( vseg_xp ) == local_cxy) ,
477"all vsegs in reference VSL must be local\n" );
[408]478
479            // get vseg type, base and size
480            uint32_t type     = vseg->type;
481            vpn_t    vpn_base = vseg->vpn_base;
482            vpn_t    vpn_size = vseg->vpn_size;
483
[595]484#if (DEBUG_VMM_SET_COW & 1)
[438]485if( DEBUG_VMM_SET_COW < cycle )
[595]486printk("\n[%s] thread[%x,%x] handling vseg %s / vpn_base = %x / vpn_size = %x\n",
487__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size );
[433]488#endif
489            // only DATA, ANON and REMOTE vsegs
[408]490            if( (type == VSEG_TYPE_DATA)  ||
491                (type == VSEG_TYPE_ANON)  ||
492                (type == VSEG_TYPE_REMOTE) )
493            {
[433]494                vpn_t      vpn;
495                uint32_t   attr;
496                ppn_t      ppn;
497                xptr_t     page_xp;
498                cxy_t      page_cxy;
499                page_t   * page_ptr;
500                xptr_t     forks_xp;
[469]501                xptr_t     lock_xp;
[433]502
503                // update flags in remote GPT
504                hal_gpt_set_cow( remote_gpt_xp,
505                                 vpn_base,
506                                 vpn_size ); 
507
508                // atomically increment pending forks counter in physical pages,
509                // for all vseg pages that are mapped in reference cluster
510                if( remote_process_cxy == local_cxy )
511                {
512                    // scan all pages in vseg
513                    for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
514                    {
515                        // get page attributes and PPN from reference GPT
[585]516                        hal_gpt_get_pte( remote_gpt_xp , vpn , &attr , &ppn ); 
[433]517
518                        // atomically update pending forks counter if page is mapped
519                        if( attr & GPT_MAPPED )
520                        {
[469]521                            // get pointers and cluster on page descriptor
[433]522                            page_xp  = ppm_ppn2page( ppn );
523                            page_cxy = GET_CXY( page_xp );
524                            page_ptr = GET_PTR( page_xp );
[469]525
526                            // get extended pointers on "forks" and "lock"
[433]527                            forks_xp = XPTR( page_cxy , &page_ptr->forks );
[469]528                            lock_xp  = XPTR( page_cxy , &page_ptr->lock );
529
[567]530                            // take lock protecting "forks" counter
531                            remote_busylock_acquire( lock_xp );
532
[469]533                            // increment "forks"
[433]534                            hal_remote_atomic_add( forks_xp , 1 );
[567]535
536                            // release lock protecting "forks" counter
537                            remote_busylock_release( lock_xp );
[433]538                        }
539                    }   // end loop on vpn
540                }   // end if local
541            }   // end if vseg type
542        }   // end loop on vsegs
[408]543    }   // end loop on process copies
544 
[438]545#if DEBUG_VMM_SET_COW
[433]546cycle = (uint32_t)hal_get_cycles();
[438]547if( DEBUG_VMM_SET_COW < cycle )
[595]548printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n",
549__FUNCTION__, this->process->pid, this->trdid, process->pid , cycle );
[433]550#endif
[408]551
552}  // end vmm_set-cow()
553
554/////////////////////////////////////////////////
555error_t vmm_fork_copy( process_t * child_process,
556                       xptr_t      parent_process_xp )
557{
558    error_t     error;
559    cxy_t       parent_cxy;
560    process_t * parent_process;
561    vmm_t     * parent_vmm;
562    xptr_t      parent_lock_xp;
563    vmm_t     * child_vmm;
564    xptr_t      iter_xp;
565    xptr_t      parent_vseg_xp;
566    vseg_t    * parent_vseg;
567    vseg_t    * child_vseg;
568    uint32_t    type;
569    bool_t      cow;
570    vpn_t       vpn;           
571    vpn_t       vpn_base;
572    vpn_t       vpn_size;
[469]573    xptr_t      page_xp;        // extended pointer on page descriptor
[408]574    page_t    * page_ptr;
575    cxy_t       page_cxy;
[469]576    xptr_t      forks_xp;       // extended pointer on forks counter in page descriptor
577    xptr_t      lock_xp;        // extended pointer on lock protecting the forks counter
[408]578    xptr_t      parent_root_xp;
579    bool_t      mapped; 
580    ppn_t       ppn;
581
[438]582#if DEBUG_VMM_FORK_COPY
[433]583uint32_t cycle = (uint32_t)hal_get_cycles();
[595]584thread_t * this = CURRENT_THREAD;
[438]585if( DEBUG_VMM_FORK_COPY < cycle )
[595]586printk("\n[%s] thread %x enter / cycle %d\n",
587__FUNCTION__ , this->process->pid, this->trdid, cycle );
[433]588#endif
[408]589
590    // get parent process cluster and local pointer
591    parent_cxy     = GET_CXY( parent_process_xp );
[433]592    parent_process = GET_PTR( parent_process_xp );
[408]593
594    // get local pointers on parent and child VMM
595    parent_vmm = &parent_process->vmm; 
596    child_vmm  = &child_process->vmm;
597
598    // get extended pointer on lock protecting the parent VSL
599    parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsegs_lock );
600
601    // initialize the lock protecting the child VSL
[567]602    remote_rwlock_init( XPTR( local_cxy , &child_vmm->vsegs_lock ), LOCK_VMM_STACK );
[408]603
604    // initialize the child VSL as empty
605    xlist_root_init( XPTR( local_cxy, &child_vmm->vsegs_root ) );
606    child_vmm->vsegs_nr = 0;
607
[415]608    // create child GPT
[408]609    error = hal_gpt_create( &child_vmm->gpt );
[415]610
[407]611    if( error )
612    {
[408]613        printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ );
614        return -1;
[407]615    }
616
[408]617    // build extended pointer on parent VSL
618    parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root );
619
[567]620    // take the lock protecting the parent VSL in read mode
621    remote_rwlock_rd_acquire( parent_lock_xp );
[415]622
[408]623    // loop on parent VSL xlist
624    XLIST_FOREACH( parent_root_xp , iter_xp )
[23]625    {
[408]626        // get local and extended pointers on current parent vseg
627        parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]628        parent_vseg    = GET_PTR( parent_vseg_xp );
[23]629
[408]630        // get vseg type
[567]631        type = hal_remote_l32( XPTR( parent_cxy , &parent_vseg->type ) );
[408]632       
[438]633#if DEBUG_VMM_FORK_COPY
[433]634cycle = (uint32_t)hal_get_cycles();
[438]635if( DEBUG_VMM_FORK_COPY < cycle )
[595]636printk("\n[%s] thread[%x,%x] found parent vseg %s / vpn_base = %x / cycle %d\n",
637__FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type),
[567]638hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]639#endif
[23]640
[408]641        // all parent vsegs - but STACK - must be copied in child VSL
642        if( type != VSEG_TYPE_STACK )
[23]643        {
[408]644            // allocate memory for a new child vseg
645            child_vseg = vseg_alloc();
646            if( child_vseg == NULL )   // release all allocated vsegs
[23]647            {
[408]648                vmm_destroy( child_process );
649                printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ );
650                return -1;
[23]651            }
652
[408]653            // copy parent vseg to child vseg
654            vseg_init_from_ref( child_vseg , parent_vseg_xp );
[23]655
[408]656            // register child vseg in child VSL
[611]657            vmm_attach_vseg_to_vsl( child_vmm , child_vseg );
[407]658
[438]659#if DEBUG_VMM_FORK_COPY
[433]660cycle = (uint32_t)hal_get_cycles();
[438]661if( DEBUG_VMM_FORK_COPY < cycle )
[595]662printk("\n[%s] thread[%x,%x] copied vseg %s / vpn_base = %x to child VSL / cycle %d\n",
663__FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type),
[567]664hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle );
[433]665#endif
[23]666
[408]667            // copy DATA, MMAP, REMOTE, FILE parent GPT entries to child GPT
668            if( type != VSEG_TYPE_CODE )
669            {
670                // activate the COW for DATA, MMAP, REMOTE vsegs only
671                cow = ( type != VSEG_TYPE_FILE );
[23]672
[408]673                vpn_base = child_vseg->vpn_base;
674                vpn_size = child_vseg->vpn_size;
[23]675
[408]676                // scan pages in parent vseg
677                for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ )
678                {
679                    error = hal_gpt_pte_copy( &child_vmm->gpt,
680                                              XPTR( parent_cxy , &parent_vmm->gpt ),
681                                              vpn,
682                                              cow,
683                                              &ppn,
684                                              &mapped );
685                    if( error )
686                    {
687                        vmm_destroy( child_process );
688                        printk("\n[ERROR] in %s : cannot copy GPT\n", __FUNCTION__ );
689                        return -1;
690                    }
691
[433]692                    // increment pending forks counter in page if mapped
[408]693                    if( mapped )
694                    {
[469]695                        // get pointers and cluster on page descriptor
696                        page_xp  = ppm_ppn2page( ppn );
[408]697                        page_cxy = GET_CXY( page_xp );
[433]698                        page_ptr = GET_PTR( page_xp );
[408]699
[469]700                        // get extended pointers on "forks" and "lock"
701                        forks_xp = XPTR( page_cxy , &page_ptr->forks );
702                        lock_xp  = XPTR( page_cxy , &page_ptr->lock );
703
[567]704                        // get lock protecting "forks" counter
705                        remote_busylock_acquire( lock_xp );
706
[469]707                        // increment "forks"
708                        hal_remote_atomic_add( forks_xp , 1 );
709
[567]710                        // release lock protecting "forks" counter
711                        remote_busylock_release( lock_xp );
712
[438]713#if DEBUG_VMM_FORK_COPY
[433]714cycle = (uint32_t)hal_get_cycles();
[438]715if( DEBUG_VMM_FORK_COPY < cycle )
[595]716printk("\n[%s] thread[%x,%x] copied vpn %x to child GPT / cycle %d\n",
717__FUNCTION__ , this->process->pid, this->trdid , vpn , cycle );
[433]718#endif
[408]719                    }
720                }
721            }   // end if no code & no stack
722        }   // end if no stack
723    }   // end loop on vsegs
724
[567]725    // release the parent VSL lock in read mode
726    remote_rwlock_rd_release( parent_lock_xp );
[408]727
[415]728    // initialize child GPT (architecture specic)
729    // => For TSAR, identity map the kentry_vseg
730    error = hal_vmm_init( child_vmm );
731
732    if( error )
733    {
734        printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ );
735        return -1;
736    }
737
[408]738    // initialize the child VMM STACK allocator
739    child_vmm->stack_mgr.bitmap   = 0;
740    child_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE;
741
742    // initialize the child VMM MMAP allocator
[23]743    uint32_t i;
[408]744    child_vmm->mmap_mgr.vpn_base        = CONFIG_VMM_HEAP_BASE;
745    child_vmm->mmap_mgr.vpn_size        = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE;
746    child_vmm->mmap_mgr.first_free_vpn  = CONFIG_VMM_HEAP_BASE;
747    for( i = 0 ; i < 32 ; i++ ) list_root_init( &child_vmm->mmap_mgr.zombi_list[i] );
[23]748
[178]749    // initialize instrumentation counters
[408]750        child_vmm->pgfault_nr    = 0;
[23]751
[408]752    // copy base addresses from parent VMM to child VMM
753    child_vmm->kent_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->kent_vpn_base));
754    child_vmm->args_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->args_vpn_base));
755    child_vmm->envs_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->envs_vpn_base));
756    child_vmm->heap_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->heap_vpn_base));
757    child_vmm->code_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->code_vpn_base));
758    child_vmm->data_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->data_vpn_base));
[23]759
[408]760    child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point));
[23]761
[124]762    hal_fence();
[23]763
[438]764#if DEBUG_VMM_FORK_COPY
[433]765cycle = (uint32_t)hal_get_cycles();
[438]766if( DEBUG_VMM_FORK_COPY < cycle )
[595]767printk("\n[%s] thread[%x,%x] exit successfully / cycle %d\n",
768__FUNCTION__ , this->process->pid, this->trdid , cycle );
[433]769#endif
770
[23]771    return 0;
772
[408]773}  // vmm_fork_copy()
[204]774
[1]775///////////////////////////////////////
776void vmm_destroy( process_t * process )
777{
[408]778    xptr_t   vseg_xp;
[1]779        vseg_t * vseg;
780
[438]781#if DEBUG_VMM_DESTROY
[433]782uint32_t cycle = (uint32_t)hal_get_cycles();
[595]783thread_t * this = CURRENT_THREAD;
[438]784if( DEBUG_VMM_DESTROY < cycle )
[595]785printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
786__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle );
[433]787#endif
[416]788
[438]789#if (DEBUG_VMM_DESTROY & 1 )
[443]790if( DEBUG_VMM_DESTROY < cycle )
[437]791vmm_display( process , true );
792#endif
793
[433]794    // get pointer on local VMM
[1]795    vmm_t  * vmm = &process->vmm;
796
[408]797    // get extended pointer on VSL root and VSL lock
798    xptr_t   root_xp = XPTR( local_cxy , &vmm->vsegs_root );
799
[611]800    // scan the VSL to delete all registered vsegs
801    // (don't use a FOREACH for item deletion in xlist)
[408]802        while( !xlist_is_empty( root_xp ) )
[1]803        {
[409]804        // get pointer on first vseg in VSL
[567]805                vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist );
[433]806        vseg    = GET_PTR( vseg_xp );
[409]807
[611]808        // delete vseg and release physical pages
809        vmm_delete_vseg( process->pid , vseg->min );
[409]810
[443]811#if( DEBUG_VMM_DESTROY & 1 )
812if( DEBUG_VMM_DESTROY < cycle )
[611]813printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n",
[443]814__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
815#endif
816
[1]817        }
818
819    // remove all vsegs from zombi_lists in MMAP allocator
820    uint32_t i;
821    for( i = 0 ; i<32 ; i++ )
822    {
823            while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) )
824            {
[408]825                    vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , zlist );
[443]826
827#if( DEBUG_VMM_DESTROY & 1 )
828if( DEBUG_VMM_DESTROY < cycle )
[595]829printk("\n[%s] found zombi vseg / vpn_base %x / vpn_size %d\n",
[443]830__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
831#endif
[611]832            // clean vseg descriptor
833            vseg->vmm = NULL;
834
835            // remove vseg from  xlist
836            xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
837
838                    // release vseg descriptor
[1]839            vseg_free( vseg );
[443]840
841#if( DEBUG_VMM_DESTROY & 1 )
842if( DEBUG_VMM_DESTROY < cycle )
[595]843printk("\n[%s] zombi vseg released / vpn_base %x / vpn_size %d\n",
[443]844__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
845#endif
[1]846            }
847    }
848
[409]849    // release memory allocated to the GPT itself
[1]850    hal_gpt_destroy( &vmm->gpt );
851
[438]852#if DEBUG_VMM_DESTROY
[433]853cycle = (uint32_t)hal_get_cycles();
[438]854if( DEBUG_VMM_DESTROY < cycle )
[595]855printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
856__FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle );
[433]857#endif
[416]858
[204]859}  // end vmm_destroy()
860
[1]861/////////////////////////////////////////////////
862vseg_t * vmm_check_conflict( process_t * process,
[21]863                             vpn_t       vpn_base,
[1]864                             vpn_t       vpn_size )
865{
866    vmm_t        * vmm = &process->vmm;
[408]867
868    // scan the VSL
[1]869        vseg_t       * vseg;
[408]870    xptr_t         iter_xp;
871    xptr_t         vseg_xp;
872    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[1]873
[408]874        XLIST_FOREACH( root_xp , iter_xp )
[1]875        {
[408]876                vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]877        vseg    = GET_PTR( vseg_xp );
[204]878
[21]879                if( ((vpn_base + vpn_size) > vseg->vpn_base) &&
880             (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg;
[1]881        }
882    return NULL;
883
[204]884}  // end vmm_check_conflict()
885
[1]886////////////////////////////////////////////////////////////////////////////////////////////
887// This static function is called by the vmm_create_vseg() function, and implements
888// the VMM stack_vseg specific allocator.
889////////////////////////////////////////////////////////////////////////////////////////////
890// @ vmm      : pointer on VMM.
[21]891// @ vpn_base : (return value) first allocated page
[1]892// @ vpn_size : (return value) number of allocated pages
893////////////////////////////////////////////////////////////////////////////////////////////
894static error_t vmm_stack_alloc( vmm_t * vmm,
895                                vpn_t * vpn_base,
896                                vpn_t * vpn_size )
897{
898    // get stack allocator pointer
899    stack_mgr_t * mgr = &vmm->stack_mgr;
900
901    // get lock on stack allocator
[567]902    busylock_acquire( &mgr->lock );
[1]903
904    // get first free slot index in bitmap
905    int32_t index = bitmap_ffc( &mgr->bitmap , 4 );
[179]906    if( (index < 0) || (index > 31) )
907    {
[567]908        busylock_release( &mgr->lock );
909        return 0xFFFFFFFF;
[179]910    }
[1]911
912    // update bitmap
913    bitmap_set( &mgr->bitmap , index );
[21]914
[1]915    // release lock on stack allocator
[567]916    busylock_release( &mgr->lock );
[1]917
[21]918    // returns vpn_base, vpn_size (one page non allocated)
[1]919    *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1;
920    *vpn_size = CONFIG_VMM_STACK_SIZE - 1;
921    return 0;
922
[204]923} // end vmm_stack_alloc()
924
[1]925////////////////////////////////////////////////////////////////////////////////////////////
926// This static function is called by the vmm_create_vseg() function, and implements
927// the VMM MMAP specific allocator.
928////////////////////////////////////////////////////////////////////////////////////////////
929// @ vmm      : [in] pointer on VMM.
930// @ npages   : [in] requested number of pages.
[21]931// @ vpn_base : [out] first allocated page.
[1]932// @ vpn_size : [out] actual number of allocated pages.
933////////////////////////////////////////////////////////////////////////////////////////////
934static error_t vmm_mmap_alloc( vmm_t * vmm,
935                               vpn_t   npages,
936                               vpn_t * vpn_base,
937                               vpn_t * vpn_size )
938{
939    uint32_t   index;
940    vseg_t   * vseg;
941    vpn_t      base;
942    vpn_t      size;
[21]943    vpn_t      free;
[1]944
[614]945#if DEBUG_VMM_MMAP_ALLOC
946thread_t * this = CURRENT_THREAD;
947uint32_t cycle = (uint32_t)hal_get_cycles();
948if( DEBUG_VMM_MMAP_ALLOC < cycle )
949printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
950__FUNCTION__, this->process->pid, this->trdid, cycle );
951#endif
952
953    // vseg size must be power of 2
[1]954    // compute actual size and index in zombi_list array
955    size  = POW2_ROUNDUP( npages );
956    index = bits_log2( size );
957
958    // get mmap allocator pointer
959    mmap_mgr_t * mgr = &vmm->mmap_mgr;
960
961    // get lock on mmap allocator
[567]962    busylock_acquire( &mgr->lock );
[1]963
964    // get vseg from zombi_list or from mmap zone
965    if( list_is_empty( &mgr->zombi_list[index] ) )     // from mmap zone
966    {
967        // check overflow
968        free = mgr->first_free_vpn;
[614]969        if( (free + size) > mgr->vpn_size ) return -1;
[1]970
[614]971        // update MMAP allocator
[1]972        mgr->first_free_vpn += size;
973
974        // compute base
975        base = free;
976    }
977    else                                             // from zombi_list
978    {
979        // get pointer on zombi vseg from zombi_list
[408]980        vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , zlist );
[1]981
982        // remove vseg from free-list
[408]983        list_unlink( &vseg->zlist );
[1]984
985        // compute base
986        base = vseg->vpn_base;
[21]987    }
988
[1]989    // release lock on mmap allocator
[567]990    busylock_release( &mgr->lock );
[1]991
[614]992#if DEBUG_VMM_MMAP_ALLOC
993cycle = (uint32_t)hal_get_cycles();
994if( DEBUG_VMM_DESTROY < cycle )
995printk("\n[%s] thread[%x,%x] exit / vpn_base %x / vpn_size %x / cycle %d\n",
996__FUNCTION__, this->process->pid, this->trdid, base, size, cycle );
997#endif
998
[1]999    // returns vpn_base, vpn_size
1000    *vpn_base = base;
1001    *vpn_size = size;
1002    return 0;
1003
[204]1004}  // end vmm_mmap_alloc()
1005
[407]1006////////////////////////////////////////////////
1007vseg_t * vmm_create_vseg( process_t   * process,
1008                              vseg_type_t   type,
1009                          intptr_t      base,
1010                              uint32_t      size,
1011                          uint32_t      file_offset,
1012                          uint32_t      file_size,
1013                          xptr_t        mapper_xp,
1014                          cxy_t         cxy )
[1]1015{
1016    vseg_t     * vseg;          // created vseg pointer
[204]1017    vpn_t        vpn_base;      // first page index
[595]1018    vpn_t        vpn_size;      // number of pages covered by vseg
[1]1019        error_t      error;
1020
[438]1021#if DEBUG_VMM_CREATE_VSEG
[595]1022thread_t * this  = CURRENT_THREAD;
1023uint32_t   cycle = (uint32_t)hal_get_cycles();
[438]1024if( DEBUG_VMM_CREATE_VSEG < cycle )
[614]1025printk("\n[%s] thread[%x,%x] enter for process %x / %s / cxy %x / cycle %d\n",
1026__FUNCTION__, this->process->pid, this->trdid, process->pid, vseg_type_str(type), cxy, cycle );
[433]1027#endif
[21]1028
[407]1029    // get pointer on VMM
1030        vmm_t * vmm    = &process->vmm;
[21]1031
[204]1032    // compute base, size, vpn_base, vpn_size, depending on vseg type
[407]1033    // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs
[595]1034
[1]1035    if( type == VSEG_TYPE_STACK )
1036    {
1037        // get vpn_base and vpn_size from STACK allocator
1038        error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size );
1039        if( error )
1040        {
[407]1041            printk("\n[ERROR] in %s : no space for stack vseg / process %x in cluster %x\n",
1042            __FUNCTION__ , process->pid , local_cxy );
[1]1043            return NULL;
1044        }
1045
1046        // compute vseg base and size from vpn_base and vpn_size
1047        base = vpn_base << CONFIG_PPM_PAGE_SHIFT;
1048        size = vpn_size << CONFIG_PPM_PAGE_SHIFT;
1049    }
[595]1050    else if( type == VSEG_TYPE_FILE )
1051    {
1052        // compute page index (in mapper) for first byte
1053        vpn_t    vpn_min    = file_offset >> CONFIG_PPM_PAGE_SHIFT;
1054
1055        // compute page index (in mapper) for last byte
1056        vpn_t    vpn_max    = (file_offset + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
1057
1058        // compute offset in first page
1059        uint32_t offset = file_offset & CONFIG_PPM_PAGE_MASK;
1060
1061        // compute number of pages required in virtual space
1062        vpn_t    npages      = vpn_max - vpn_min + 1;
1063
1064        // get vpn_base and vpn_size from MMAP allocator
1065        error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size );
1066        if( error )
1067        {
1068            printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n",
1069                   __FUNCTION__ , process->pid , local_cxy );
1070            return NULL;
1071        }
1072
1073        // set the vseg base (not always aligned for FILE)
1074        base = (vpn_base << CONFIG_PPM_PAGE_SHIFT) + offset; 
1075    }
[21]1076    else if( (type == VSEG_TYPE_ANON) ||
[1]1077             (type == VSEG_TYPE_REMOTE) )
1078    {
[595]1079        // compute number of required pages in virtual space
1080        vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
1081        if( size & CONFIG_PPM_PAGE_MASK) npages++;
1082       
[1]1083        // get vpn_base and vpn_size from MMAP allocator
1084        error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size );
1085        if( error )
1086        {
1087            printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n",
1088                   __FUNCTION__ , process->pid , local_cxy );
1089            return NULL;
1090        }
1091
[595]1092        // set vseg base (always aligned for ANON or REMOTE)
[1]1093        base = vpn_base << CONFIG_PPM_PAGE_SHIFT;
1094    }
[595]1095    else    // VSEG_TYPE_DATA or VSEG_TYPE_CODE
[1]1096    {
[204]1097        uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT;
1098        uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
1099
1100        vpn_base = vpn_min;
1101            vpn_size = vpn_max - vpn_min + 1;
[1]1102    }
1103
1104    // check collisions
1105    vseg = vmm_check_conflict( process , vpn_base , vpn_size );
1106    if( vseg != NULL )
1107    {
[614]1108        printk("\n[ERROR] in %s for process %x : new vseg [vpn_base %x / vpn_size %x]\n"
1109               "  overlap existing vseg [vpn_base %x / vpn_size %x]\n",
[407]1110        __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size );
[1]1111        return NULL;
1112    }
1113
1114    // allocate physical memory for vseg descriptor
1115        vseg = vseg_alloc();
1116        if( vseg == NULL )
1117        {
1118            printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n",
[407]1119        __FUNCTION__ , process->pid );
[1]1120        return NULL;
1121        }
1122
[614]1123#if DEBUG_VMM_CREATE_VSEG
1124if( DEBUG_VMM_CREATE_VSEG < cycle )
1125printk("\n[%s] thread[%x,%x] : base %x / size %x / vpn_base %x / vpn_size %x\n",
1126__FUNCTION__, this->process->pid, this->trdid, base, size, vpn_base, vpn_size );
1127#endif
1128
[1]1129    // initialize vseg descriptor
[407]1130        vseg_init( vseg,
1131               type,
1132               base,
1133               size,
1134               vpn_base,
1135               vpn_size,
1136               file_offset,
1137               file_size,
1138               mapper_xp,
1139               cxy );
[1]1140
[408]1141    // attach vseg to VSL
[611]1142        vmm_attach_vseg_to_vsl( vmm , vseg );
[1]1143
[438]1144#if DEBUG_VMM_CREATE_VSEG
[433]1145cycle = (uint32_t)hal_get_cycles();
[438]1146if( DEBUG_VMM_CREATE_VSEG < cycle )
[595]1147printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / cycle %d\n",
1148__FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), cxy, cycle );
[433]1149#endif
[21]1150
[1]1151        return vseg;
1152
[406]1153}  // vmm_create_vseg()
1154
[611]1155///////////////////////////////////
1156void vmm_delete_vseg( pid_t    pid,
1157                      intptr_t vaddr )
[1]1158{
[611]1159    process_t * process;    // local pointer on local process
1160    vmm_t     * vmm;        // local pointer on local process VMM
1161    vseg_t    * vseg;       // local pointer on local vseg containing vaddr
1162    gpt_t     * gpt;        // local pointer on local process GPT
[21]1163    vpn_t       vpn;        // VPN of current PTE
1164    vpn_t       vpn_min;    // VPN of first PTE
[1]1165    vpn_t       vpn_max;    // VPN of last PTE (excluded)
[409]1166    ppn_t       ppn;        // current PTE ppn value
1167    uint32_t    attr;       // current PTE attributes
1168    kmem_req_t  req;        // request to release memory
1169    xptr_t      page_xp;    // extended pointer on page descriptor
1170    cxy_t       page_cxy;   // page descriptor cluster
1171    page_t    * page_ptr;   // page descriptor pointer
[433]1172    xptr_t      forks_xp;   // extended pointer on pending forks counter
[469]1173    xptr_t      lock_xp;    // extended pointer on lock protecting forks counter
1174    uint32_t    forks;      // actual number of pendinf forks
[1]1175
[611]1176#if DEBUG_VMM_DELETE_VSEG
[595]1177uint32_t   cycle = (uint32_t)hal_get_cycles();
1178thread_t * this  = CURRENT_THREAD;
[611]1179if( DEBUG_VMM_DELETE_VSEG < cycle )
1180printk("\n[%s] thread[%x,%x] enter / process %x / vaddr %x / cycle %d\n",
1181__FUNCTION__, this->process->pid, this->trdid, pid, vaddr, cycle );
[433]1182#endif
[409]1183
[611]1184    // get local pointer on local process descriptor
1185    process = cluster_get_local_process_from_pid( pid );
[1]1186
[611]1187    if( process == NULL ) return;
1188
1189    // get pointers on local process VMM an GPT
1190    vmm = &process->vmm;
1191    gpt = &process->vmm.gpt;
1192
1193    // get local pointer on vseg containing vaddr
1194    vseg = vmm_vseg_from_vaddr( vmm , vaddr );
1195
1196    if( vseg == NULL ) return;
1197
1198    // loop to invalidate all vseg PTEs in GPT
[1]1199    vpn_min = vseg->vpn_base;
1200    vpn_max = vpn_min + vseg->vpn_size;
1201        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
1202    {
[409]1203        // get GPT entry
[585]1204        hal_gpt_get_pte( XPTR( local_cxy , gpt ) , vpn , &attr , &ppn );
[409]1205
1206        if( attr & GPT_MAPPED )  // entry is mapped
1207        { 
[437]1208
[611]1209#if( DEBUG_VMM_DELETE_VSEG & 1 )
1210if( DEBUG_VMM_DELETE_VSEG < cycle )
1211printk("- unmap vpn %x / ppn %x / vseg %s \n" , vpn , ppn, vseg_type_str(vseg->type) );
[437]1212#endif
1213
[567]1214// check small page
[585]1215assert( (attr & GPT_SMALL) , "an user vseg must use small pages" );
[409]1216
[585]1217            // unmap GPT entry in local GPT
[409]1218            hal_gpt_reset_pte( gpt , vpn );
1219
[433]1220            // handle pending forks counter if
1221            // 1) not identity mapped
[567]1222            // 2) reference cluster
[433]1223            if( ((vseg->flags & VSEG_IDENT)  == 0) &&
1224                (GET_CXY( process->ref_xp ) == local_cxy) )
[409]1225            {
[433]1226                // get extended pointer on physical page descriptor
[409]1227                page_xp  = ppm_ppn2page( ppn );
1228                page_cxy = GET_CXY( page_xp );
[433]1229                page_ptr = GET_PTR( page_xp );
[409]1230
[469]1231                // get extended pointers on forks and lock fields
1232                forks_xp = XPTR( page_cxy , &page_ptr->forks );
1233                lock_xp  = XPTR( page_cxy , &page_ptr->lock );
[433]1234
1235                // get pending forks counter
[567]1236                forks = hal_remote_l32( forks_xp );
[433]1237               
[469]1238                if( forks )  // decrement pending forks counter
[409]1239                {
[433]1240                    hal_remote_atomic_add( forks_xp , -1 );
1241                } 
1242                else         // release physical page to relevant cluster
[409]1243                {
[433]1244                    if( page_cxy == local_cxy )   // local cluster
1245                    {
1246                        req.type = KMEM_PAGE;
1247                        req.ptr  = page_ptr; 
1248                        kmem_free( &req );
1249                    }
1250                    else                          // remote cluster
1251                    {
1252                        rpc_pmem_release_pages_client( page_cxy , page_ptr );
1253                    }
[611]1254
1255#if( DEBUG_VMM_DELETE_VSEG & 1 )
1256if( DEBUG_VMM_DELETE_VSEG < cycle )
1257printk("- release ppn %x\n", ppn );
1258#endif
[409]1259                }
1260            }
1261        }
[1]1262    }
[433]1263
[611]1264    // remove vseg from VSL and release vseg descriptor (if not MMAP)
1265    vmm_detach_vseg_from_vsl( vmm , vseg );
1266
1267#if DEBUG_VMM_DELETE_VSEG
[433]1268cycle = (uint32_t)hal_get_cycles();
[611]1269if( DEBUG_VMM_DELETE_VSEG < cycle )
[595]1270printk("\n[%s] thread[%x,%x] exit / process %x / vseg %s / base %x / cycle %d\n",
[611]1271__FUNCTION__, this->process->pid, this->trdid, pid, vseg_type_str(vseg->type), vaddr, cycle );
[433]1272#endif
1273
[611]1274}  // end vmm_delete_vseg()
[1]1275
[611]1276/////////////////////////////////////////////
1277vseg_t * vmm_vseg_from_vaddr( vmm_t    * vmm,
1278                              intptr_t   vaddr )
[406]1279{
[408]1280    xptr_t   iter_xp;
1281    xptr_t   vseg_xp;
1282    vseg_t * vseg;
[406]1283
[408]1284    // get extended pointers on VSL lock and root
1285    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
1286    xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[406]1287
[408]1288    // get lock protecting the VSL
[567]1289    remote_rwlock_rd_acquire( lock_xp );
[408]1290
1291    // scan the list of vsegs in VSL
1292    XLIST_FOREACH( root_xp , iter_xp )
[406]1293    {
[408]1294        vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1295        vseg    = GET_PTR( vseg_xp );
[595]1296
[408]1297        if( (vaddr >= vseg->min) && (vaddr < vseg->max) )
[595]1298        { 
[408]1299            // return success
[567]1300            remote_rwlock_rd_release( lock_xp );
[408]1301            return vseg;
1302        }
[406]1303    }
1304
[408]1305    // return failure
[567]1306    remote_rwlock_rd_release( lock_xp );
[408]1307    return NULL;
[406]1308
[595]1309}  // end vmm_vseg_from_vaddr()
[406]1310
[1]1311/////////////////////////////////////////////
1312error_t vmm_resize_vseg( process_t * process,
1313                         intptr_t    base,
1314                         intptr_t    size )
1315{
[406]1316    error_t   error;
1317    vseg_t  * new;
1318    vpn_t     vpn_min;
1319    vpn_t     vpn_max;
[1]1320
1321    // get pointer on process VMM
1322    vmm_t * vmm = &process->vmm;
1323
1324    intptr_t addr_min = base;
1325        intptr_t addr_max = base + size;
1326
1327    // get pointer on vseg
[595]1328        vseg_t * vseg = vmm_vseg_from_vaddr( vmm , base );
[1]1329
1330        if( vseg == NULL)  return EINVAL;
[21]1331
[408]1332    // get extended pointer on VSL lock
1333    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
[21]1334
[408]1335    // get lock protecting VSL
[567]1336        remote_rwlock_wr_acquire( lock_xp );
[408]1337
[611]1338        if( (vseg->min > addr_min) || (vseg->max < addr_max) )        // not included in vseg
[1]1339    {
[611]1340        error = -1;
[1]1341    }
[611]1342        else if( (vseg->min == addr_min) && (vseg->max == addr_max) )  // vseg must be deleted
[1]1343    {
[611]1344        vmm_delete_vseg( process->pid , vseg->min );
[1]1345        error = 0;
1346    }
[611]1347        else if( vseg->min == addr_min )                               // vseg must be resized
[1]1348    {
[406]1349        // update vseg base address
1350        vseg->min = addr_max;
1351
1352        // update vpn_base and vpn_size
1353        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1354        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1355        vseg->vpn_base = vpn_min;
1356        vseg->vpn_size = vpn_max - vpn_min + 1;
1357        error = 0;
[1]1358    }
[611]1359        else if( vseg->max == addr_max )                              // vseg must be resized
[1]1360    {
[406]1361        // update vseg max address
1362        vseg->max = addr_min;
1363
1364        // update vpn_base and vpn_size
1365        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1366        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1367        vseg->vpn_base = vpn_min;
1368        vseg->vpn_size = vpn_max - vpn_min + 1;
1369        error = 0;
[1]1370    }
[611]1371    else                                                          // vseg cut in three regions
[1]1372    {
[406]1373        // resize existing vseg
1374        vseg->max = addr_min;
1375
1376        // update vpn_base and vpn_size
1377        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1378        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1379        vseg->vpn_base = vpn_min;
1380        vseg->vpn_size = vpn_max - vpn_min + 1;
1381
1382        // create new vseg
[407]1383        new = vmm_create_vseg( process, 
1384                               vseg->type,
1385                               addr_min, 
1386                               (vseg->max - addr_max),
1387                               vseg->file_offset,
1388                               vseg->file_size,
1389                               vseg->mapper_xp,
1390                               vseg->cxy ); 
1391
[406]1392        if( new == NULL ) error = EINVAL;
1393        else              error = 0;
[1]1394    }
1395
1396    // release VMM lock
[567]1397        remote_rwlock_wr_release( lock_xp );
[1]1398
1399        return error;
1400
[406]1401}  // vmm_resize_vseg()
1402
[1]1403///////////////////////////////////////////
[388]1404error_t  vmm_get_vseg( process_t * process,
[394]1405                       intptr_t    vaddr,
[388]1406                       vseg_t   ** found_vseg )
[1]1407{
[595]1408    xptr_t    vseg_xp;
1409    vseg_t  * vseg;
1410    vmm_t   * vmm;
1411    error_t   error;
[1]1412
[440]1413    // get pointer on local VMM
1414    vmm = &process->vmm;
[1]1415
[440]1416    // try to get vseg from local VMM
[595]1417    vseg = vmm_vseg_from_vaddr( vmm , vaddr );
[440]1418
[388]1419    if( vseg == NULL )   // vseg not found in local cluster => try to get it from ref
1420        {
1421        // get extended pointer on reference process
1422        xptr_t ref_xp = process->ref_xp;
[1]1423
[388]1424        // get cluster and local pointer on reference process
1425        cxy_t       ref_cxy = GET_CXY( ref_xp );
[433]1426        process_t * ref_ptr = GET_PTR( ref_xp );
[388]1427
1428        if( local_cxy == ref_cxy )  return -1;   // local cluster is the reference
1429
1430        // get extended pointer on reference vseg
[394]1431        rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error );
[388]1432           
[440]1433        if( error )   return -1;                // vseg not found => illegal user vaddr
[388]1434       
1435        // allocate a vseg in local cluster
1436        vseg = vseg_alloc();
1437
[440]1438        if( vseg == NULL ) return -1;           // cannot allocate a local vseg
[388]1439
1440        // initialise local vseg from reference
1441        vseg_init_from_ref( vseg , vseg_xp );
1442
[611]1443        // register local vseg in local VSL
1444        vmm_attach_vseg_to_vsl( vmm , vseg );
[388]1445    }   
[595]1446
[388]1447    // success
1448    *found_vseg = vseg;
[394]1449    return 0;
[388]1450
1451}  // end vmm_get_vseg()
1452
[407]1453//////////////////////////////////////////////////////////////////////////////////////
1454// This static function compute the target cluster to allocate a physical page
1455// for a given <vpn> in a given <vseg>, allocates the page (with an RPC if required)
1456// and returns an extended pointer on the allocated page descriptor.
[585]1457// It can be called by a thread running in any cluster.
[407]1458// The vseg cannot have the FILE type.
1459//////////////////////////////////////////////////////////////////////////////////////
1460static xptr_t vmm_page_allocate( vseg_t * vseg,
1461                                 vpn_t    vpn )
1462{
[433]1463
[438]1464#if DEBUG_VMM_ALLOCATE_PAGE
[595]1465uint32_t   cycle = (uint32_t)hal_get_cycles();
1466thread_t * this  = CURRENT_THREAD;
[438]1467if( DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() )
[595]1468printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n",
1469__FUNCTION__ , this->process->pid, this->trdid, vpn, cycle );
[433]1470#endif
1471
[407]1472    page_t     * page_ptr;
1473    cxy_t        page_cxy;
1474    kmem_req_t   req;
[577]1475    uint32_t     index;
[407]1476
[577]1477    uint32_t     type   = vseg->type;
1478    uint32_t     flags  = vseg->flags;
1479    uint32_t     x_size = LOCAL_CLUSTER->x_size;
1480    uint32_t     y_size = LOCAL_CLUSTER->y_size;
[407]1481
[567]1482// check vseg type
1483assert( ( type != VSEG_TYPE_FILE ) , "illegal vseg type\n" );
[407]1484
1485    if( flags & VSEG_DISTRIB )    // distributed => cxy depends on vpn LSB
1486    {
[577]1487        index    = vpn & ((x_size * y_size) - 1);
1488        page_cxy = HAL_CXY_FROM_XY( (index / y_size) , (index % y_size) );
[561]1489
[577]1490        // If the cluster selected from VPN's LSBs is empty, we select one randomly
1491        if ( cluster_is_active( page_cxy ) == false )
1492        {
1493            page_cxy = cluster_random_select();
[561]1494        }
[407]1495    }
1496    else                          // other cases => cxy specified in vseg
1497    {
[561]1498        page_cxy = vseg->cxy;
[407]1499    }
1500
1501    // allocate a physical page from target cluster
1502    if( page_cxy == local_cxy )  // target cluster is the local cluster
1503    {
1504        req.type  = KMEM_PAGE;
1505        req.size  = 0;
1506        req.flags = AF_NONE;
1507        page_ptr  = (page_t *)kmem_alloc( &req );
1508    }
1509    else                           // target cluster is not the local cluster
1510    {
1511        rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr );
1512    }
1513
[438]1514#if DEBUG_VMM_ALLOCATE_PAGE
[595]1515cycle = (uint32_t)hal_get_cycles();
[438]1516if( DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() )
[595]1517printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n",
1518__FUNCTION__ , this->process->pid, this->trdid, vpn,
1519ppm_page2ppn( XPTR( page_cxy , page_ptr ) , cycle );
[433]1520#endif
1521
[407]1522    if( page_ptr == NULL ) return XPTR_NULL;
1523    else                   return XPTR( page_cxy , page_ptr );
1524
1525}  // end vmm_page_allocate() 
1526
[313]1527////////////////////////////////////////
1528error_t vmm_get_one_ppn( vseg_t * vseg,
1529                         vpn_t    vpn,
1530                         ppn_t  * ppn )
1531{
1532    error_t    error;
[407]1533    xptr_t     page_xp;           // extended pointer on physical page descriptor
[606]1534    uint32_t   page_id;           // missing page index in vseg mapper
[406]1535    uint32_t   type;              // vseg type;
[313]1536
[406]1537    type      = vseg->type;
[606]1538    page_id   = vpn - vseg->vpn_base;
[313]1539
[438]1540#if DEBUG_VMM_GET_ONE_PPN
[595]1541uint32_t   cycle = (uint32_t)hal_get_cycles();
1542thread_t * this  = CURRENT_THREAD;
1543if( DEBUG_VMM_GET_ONE_PPN < cycle )
[606]1544printk("\n[%s] thread[%x,%x] enter for vpn %x / type %s / page_id  %d / cycle %d\n",
1545__FUNCTION__, this->process->pid, this->trdid, vpn, vseg_type_str(type), page_id, cycle );
[433]1546#endif
[313]1547
[406]1548    // FILE type : get the physical page from the file mapper
[313]1549    if( type == VSEG_TYPE_FILE )
1550    {
[406]1551        // get extended pointer on mapper
[407]1552        xptr_t mapper_xp = vseg->mapper_xp;
[313]1553
[567]1554assert( (mapper_xp != XPTR_NULL),
1555"mapper not defined for a FILE vseg\n" );
[406]1556       
[606]1557        // get extended pointer on page descriptor
1558        page_xp = mapper_remote_get_page( mapper_xp , page_id );
[406]1559
[606]1560        if ( page_xp == XPTR_NULL ) return EINVAL;
[313]1561    }
1562
[406]1563    // Other types : allocate a physical page from target cluster,
[407]1564    // as defined by vseg type and vpn value
[313]1565    else
1566    {
[433]1567        // allocate one physical page
[407]1568        page_xp = vmm_page_allocate( vseg , vpn );
[406]1569
[407]1570        if( page_xp == XPTR_NULL ) return ENOMEM;
[313]1571
[406]1572        // initialise missing page from .elf file mapper for DATA and CODE types
[440]1573        // the vseg->mapper_xp field is an extended pointer on the .elf file mapper
[313]1574        if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) )
1575        {
[406]1576            // get extended pointer on mapper
1577            xptr_t     mapper_xp = vseg->mapper_xp;
[313]1578
[567]1579assert( (mapper_xp != XPTR_NULL),
1580"mapper not defined for a CODE or DATA vseg\n" );
[406]1581       
1582            // compute missing page offset in vseg
[606]1583            uint32_t offset = page_id << CONFIG_PPM_PAGE_SHIFT;
[406]1584
[313]1585            // compute missing page offset in .elf file
[406]1586            uint32_t elf_offset = vseg->file_offset + offset;
[313]1587
[438]1588#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[469]1589if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[595]1590printk("\n[%s] thread[%x,%x] for vpn = %x / elf_offset = %x\n",
1591__FUNCTION__, this->process->pid, this->trdid, vpn, elf_offset );
[433]1592#endif
[406]1593            // compute extended pointer on page base
[407]1594            xptr_t base_xp  = ppm_page2base( page_xp );
[313]1595
[406]1596            // file_size (in .elf mapper) can be smaller than vseg_size (BSS)
1597            uint32_t file_size = vseg->file_size;
1598
1599            if( file_size < offset )                 // missing page fully in  BSS
[313]1600            {
[406]1601
[438]1602#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[469]1603if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[595]1604printk("\n[%s] thread[%x,%x] for vpn  %x / fully in BSS\n",
1605__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]1606#endif
[407]1607                if( GET_CXY( page_xp ) == local_cxy )
[313]1608                {
[315]1609                    memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE );
[313]1610                }
1611                else
1612                {
[315]1613                   hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE );       
[313]1614                }
1615            }
[406]1616            else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) )  // fully in  mapper
[315]1617            {
[406]1618
[438]1619#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[469]1620if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[595]1621printk("\n[%s] thread[%x,%x] for vpn  %x / fully in mapper\n",
1622__FUNCTION__, this->process->pid, this->trdid, vpn );
[433]1623#endif
[606]1624                error = mapper_move_kernel( mapper_xp,
1625                                            true,             // to_buffer
1626                                            elf_offset,
1627                                            base_xp,
1628                                            CONFIG_PPM_PAGE_SIZE ); 
[313]1629                if( error ) return EINVAL;
1630            }
[406]1631            else  // both in mapper and in BSS :
1632                  // - (file_size - offset)             bytes from mapper
1633                  // - (page_size + offset - file_size) bytes from BSS
[313]1634            {
[406]1635
[438]1636#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
[469]1637if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[610]1638printk("\n[%s] thread[%x,%x] for vpn  %x / both mapper & BSS\n"
[433]1639"      %d bytes from mapper / %d bytes from BSS\n",
[595]1640__FUNCTION__, this->process->pid, this->trdid, vpn,
[407]1641file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size  );
[433]1642#endif
[313]1643                // initialize mapper part
[606]1644                error = mapper_move_kernel( mapper_xp,
1645                                            true,         // to buffer
1646                                            elf_offset,
1647                                            base_xp,
1648                                            file_size - offset ); 
[313]1649                if( error ) return EINVAL;
1650
1651                // initialize BSS part
[407]1652                if( GET_CXY( page_xp ) == local_cxy )
[313]1653                {
[406]1654                    memset( GET_PTR( base_xp ) + file_size - offset , 0 , 
1655                            offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]1656                }
1657                else
1658                {
[406]1659                   hal_remote_memset( base_xp + file_size - offset , 0 , 
1660                                      offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]1661                }
1662            }   
1663        }  // end initialisation for CODE or DATA types   
1664    } 
1665
1666    // return ppn
[407]1667    *ppn = ppm_page2ppn( page_xp );
[406]1668
[438]1669#if DEBUG_VMM_GET_ONE_PPN
[595]1670cycle = (uint32_t)hal_get_cycles();
1671if( DEBUG_VMM_GET_ONE_PPN < cycle )
1672printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle\n",
1673__FUNCTION__ , this->process->pid, this->trdid , vpn , *ppn, cycle );
[433]1674#endif
[406]1675
[313]1676    return 0;
1677
1678}  // end vmm_get_one_ppn()
1679
[585]1680///////////////////////////////////////////////////
1681error_t vmm_handle_page_fault( process_t * process,
1682                               vpn_t       vpn )
[1]1683{
[585]1684    vseg_t         * vseg;            // vseg containing vpn
1685    uint32_t         new_attr;        // new PTE_ATTR value
1686    ppn_t            new_ppn;         // new PTE_PPN value
1687    uint32_t         ref_attr;        // PTE_ATTR value in reference GPT
1688    ppn_t            ref_ppn;         // PTE_PPN value in reference GPT
1689    cxy_t            ref_cxy;         // reference cluster for missing vpn
1690    process_t      * ref_ptr;         // reference process for missing vpn
1691    xptr_t           local_gpt_xp;    // extended pointer on local GPT
1692    xptr_t           local_lock_xp;   // extended pointer on local GPT lock
1693    xptr_t           ref_gpt_xp;      // extended pointer on reference GPT
1694    xptr_t           ref_lock_xp;     // extended pointer on reference GPT lock
1695    error_t          error;           // value returned by called functions
[1]1696
[585]1697    // get local vseg (access to reference VSL can be required)
1698    error = vmm_get_vseg( process, 
1699                          (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT,
1700                          &vseg );
1701    if( error )
1702    {
[595]1703        printk("\n[ERROR] in %s : vpn %x in process %x not in a registered vseg\n",
[585]1704        __FUNCTION__ , vpn , process->pid );
1705       
1706        return EXCP_USER_ERROR;
1707    }
1708
1709 #if DEBUG_VMM_HANDLE_PAGE_FAULT
1710uint32_t   cycle = (uint32_t)hal_get_cycles();
[567]1711thread_t * this  = CURRENT_THREAD;
[585]1712if( DEBUG_VMM_HANDLE_PAGE_FAULT < cycle )
[595]1713printk("\n[%s] threadr[%x,%x] enter for vpn %x / %s / cycle %d\n",
[585]1714__FUNCTION__, this->process->pid, this->trdid, vpn, vseg_type_str(vseg->type), cycle );
[433]1715#endif
[406]1716
[585]1717    //////////////// private vseg => access only the local GPT
1718    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[438]1719    {
[585]1720        // build extended pointer on local GPT and local GPT lock
1721        local_gpt_xp  = XPTR( local_cxy , &process->vmm.gpt );
1722        local_lock_xp = XPTR( local_cxy , &process->vmm.gpt_lock );
[407]1723
[585]1724        // take local GPT lock in write mode
1725        remote_rwlock_wr_acquire( local_lock_xp );
[407]1726
[585]1727        // check VPN still unmapped in local GPT
[595]1728
[585]1729        // do nothing if VPN has been mapped by a a concurrent page_fault
1730        hal_gpt_get_pte( local_gpt_xp,
1731                         vpn,
1732                         &new_attr,
1733                         &new_ppn );
[407]1734
[585]1735        if( (new_attr & GPT_MAPPED) == 0 )       // VPN still unmapped
1736        { 
1737            // allocate and initialise a physical page depending on the vseg type
1738            error = vmm_get_one_ppn( vseg , vpn , &new_ppn );
[407]1739
[585]1740            if( error )
[408]1741            {
1742                printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
1743                __FUNCTION__ , process->pid , vpn );
[1]1744
[585]1745                // release local GPT lock in write mode
1746                remote_rwlock_wr_release( local_lock_xp );
[406]1747
[585]1748                return EXCP_KERNEL_PANIC;
[407]1749            }
1750
[408]1751            // define new_attr from vseg flags
[407]1752            new_attr = GPT_MAPPED | GPT_SMALL;
1753            if( vseg->flags & VSEG_USER  ) new_attr |= GPT_USER;
1754            if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE;
1755            if( vseg->flags & VSEG_EXEC  ) new_attr |= GPT_EXECUTABLE;
1756            if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE;
1757
[585]1758            // set PTE (PPN & attribute) to local GPT
1759            error = hal_gpt_set_pte( local_gpt_xp,
[408]1760                                     vpn,
1761                                     new_attr,
1762                                     new_ppn );
[585]1763            if ( error )
[407]1764            {
[585]1765                printk("\n[ERROR] in %s : cannot update local GPT / process %x / vpn = %x\n",
[407]1766                __FUNCTION__ , process->pid , vpn );
[585]1767
1768                // release local GPT lock in write mode
1769                remote_rwlock_wr_release( local_lock_xp );
1770
1771                return EXCP_KERNEL_PANIC;
[407]1772            }
1773        }
[585]1774
1775        // release local GPT lock in write mode
1776        remote_rwlock_wr_release( local_lock_xp );
1777
1778#if DEBUG_VMM_HANDLE_PAGE_FAULT
1779cycle = (uint32_t)hal_get_cycles();
1780if( DEBUG_VMM_HANDLE_PAGE_FAULT < cycle )
[595]1781printk("\n[%s] private page fault handled / vpn %x / ppn %x / attr %x / cycle %d\n",
[585]1782__FUNCTION__, vpn, new_ppn, new_attr, cycle );
1783#endif
1784        return EXCP_NON_FATAL;
1785
1786    }   // end local GPT access
1787
1788    //////////// public vseg => access reference GPT
1789    else                               
1790    {
1791        // get reference process cluster and local pointer
1792        ref_cxy = GET_CXY( process->ref_xp );
1793        ref_ptr = GET_PTR( process->ref_xp );
1794
1795        // build extended pointer on reference GPT and reference GPT lock
1796        ref_gpt_xp  = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
1797        ref_lock_xp = XPTR( ref_cxy , &ref_ptr->vmm.gpt_lock );
1798
1799        // build extended pointer on local GPT and local GPT lock
1800        local_gpt_xp  = XPTR( local_cxy , &process->vmm.gpt );
1801        local_lock_xp = XPTR( local_cxy , &process->vmm.gpt_lock );
1802
1803        // take reference GPT lock in read mode
1804        remote_rwlock_rd_acquire( ref_lock_xp );
1805
1806        // get directly PPN & attributes from reference GPT
1807        // this can avoids a costly RPC for a false page fault
1808        hal_gpt_get_pte( ref_gpt_xp,
1809                         vpn,
1810                         &ref_attr,
1811                         &ref_ppn );
1812
1813        // release reference GPT lock in read mode
1814        remote_rwlock_rd_release( ref_lock_xp );
1815
1816        if( ref_attr & GPT_MAPPED )        // false page fault => update local GPT
[1]1817        {
[585]1818            // take local GPT lock in write mode
1819            remote_rwlock_wr_acquire( local_lock_xp );
1820           
1821            // check VPN still unmapped in local GPT
1822            hal_gpt_get_pte( local_gpt_xp,
1823                             vpn,
1824                             &new_attr,
1825                             &new_ppn );
[1]1826
[585]1827            if( (new_attr & GPT_MAPPED) == 0 )       // VPN still unmapped
1828            { 
1829                // update local GPT from reference GPT
1830                error = hal_gpt_set_pte( local_gpt_xp,
1831                                         vpn,
1832                                         ref_attr,
1833                                         ref_ppn );
1834                if( error )
1835                {
[595]1836                    printk("\n[ERROR] in %s : cannot update local GPT / process %x / vpn %x\n",
[585]1837                    __FUNCTION__ , process->pid , vpn );
1838
1839                    // release local GPT lock in write mode
1840                    remote_rwlock_wr_release( local_lock_xp );
1841           
1842                    return EXCP_KERNEL_PANIC;
1843                }
1844            }
1845            else    // VPN has been mapped by a a concurrent page_fault
1846            {
1847                // keep PTE from local GPT
1848                ref_attr = new_attr;
1849                ref_ppn  = new_ppn;
1850            }
1851
1852            // release local GPT lock in write mode
1853            remote_rwlock_wr_release( local_lock_xp );
1854           
1855#if DEBUG_VMM_HANDLE_PAGE_FAULT
[433]1856cycle = (uint32_t)hal_get_cycles();
[585]1857if( DEBUG_VMM_HANDLE_PAGE_FAULT < cycle )
[595]1858printk("\n[%s] false page fault handled / vpn %x / ppn %x / attr %x / cycle %d\n",
[585]1859__FUNCTION__, vpn, ref_ppn, ref_attr, cycle );
[433]1860#endif
[585]1861            return EXCP_NON_FATAL;
1862        }
1863        else                            // true page fault => update reference GPT
1864        {
1865            // take reference GPT lock in write mode
1866            remote_rwlock_wr_acquire( ref_lock_xp );
1867           
1868            // check VPN still unmapped in reference GPT
1869            // do nothing if VPN has been mapped by a a concurrent page_fault
1870            hal_gpt_get_pte( ref_gpt_xp,
1871                             vpn,
1872                             &ref_attr,
1873                             &ref_ppn );
[406]1874
[585]1875            if( (ref_attr & GPT_MAPPED) == 0 )       // VPN actually unmapped
1876            { 
1877                // allocate and initialise a physical page depending on the vseg type
1878                error = vmm_get_one_ppn( vseg , vpn , &new_ppn );
[1]1879
[585]1880                if( error )
1881                {
1882                    printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
1883                    __FUNCTION__ , process->pid , vpn );
[313]1884
[585]1885                   // release reference GPT lock in write mode
1886                   remote_rwlock_wr_release( ref_lock_xp );
1887                   
1888                   return EXCP_KERNEL_PANIC;
1889                }
[1]1890
[585]1891                // define new_attr from vseg flags
1892                new_attr = GPT_MAPPED | GPT_SMALL;
1893                if( vseg->flags & VSEG_USER  ) new_attr |= GPT_USER;
1894                if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE;
1895                if( vseg->flags & VSEG_EXEC  ) new_attr |= GPT_EXECUTABLE;
1896                if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE;
[440]1897
[585]1898                // update reference GPT
1899                error = hal_gpt_set_pte( ref_gpt_xp,
1900                                         vpn,
1901                                         new_attr,
1902                                         new_ppn );
1903
1904                // update local GPT (protected by reference GPT lock)
1905                error |= hal_gpt_set_pte( local_gpt_xp,
1906                                          vpn,
1907                                          new_attr,
1908                                          new_ppn );
1909
1910                if( error )
1911                {
1912                    printk("\n[ERROR] in %s : cannot update GPT / process %x / vpn = %x\n",
1913                    __FUNCTION__ , process->pid , vpn );
1914
1915                    // release reference GPT lock in write mode
1916                    remote_rwlock_wr_release( ref_lock_xp );
1917
1918                    return EXCP_KERNEL_PANIC;
1919                }
1920            }
1921
1922            // release reference GPT lock in write mode
1923            remote_rwlock_wr_release( ref_lock_xp );
1924
[440]1925#if DEBUG_VMM_HANDLE_PAGE_FAULT
[585]1926cycle = (uint32_t)hal_get_cycles();
[469]1927if( DEBUG_VMM_HANDLE_PAGE_FAULT < cycle )
[595]1928printk("\n[%s] true page fault handled / vpn %x / ppn %x / attr %x / cycle %d\n",
[585]1929__FUNCTION__, vpn, new_ppn, new_attr, cycle );
[435]1930#endif
[585]1931            return EXCP_NON_FATAL;
1932        }
1933    }
1934}   // end vmm_handle_page_fault()
[435]1935
[585]1936////////////////////////////////////////////
1937error_t vmm_handle_cow( process_t * process,
1938                        vpn_t       vpn )
1939{
1940    vseg_t         * vseg;            // vseg containing vpn
1941    cxy_t            ref_cxy;         // reference cluster for missing vpn
1942    process_t      * ref_ptr;         // reference process for missing vpn
1943    xptr_t           gpt_xp;          // extended pointer on GPT
1944    xptr_t           gpt_lock_xp;     // extended pointer on GPT lock
1945    uint32_t         old_attr;        // current PTE_ATTR value
1946    ppn_t            old_ppn;         // current PTE_PPN value
1947    uint32_t         new_attr;        // new PTE_ATTR value
1948    ppn_t            new_ppn;         // new PTE_PPN value
1949    error_t          error;
[1]1950
[585]1951#if DEBUG_VMM_HANDLE_COW
1952uint32_t   cycle = (uint32_t)hal_get_cycles();
1953thread_t * this  = CURRENT_THREAD;
1954if( DEBUG_VMM_HANDLE_COW < cycle )
[595]1955printk("\n[%s] thread[%x,%x] enter for vpn %x / core[%x,%d] / cycle %d\n",
[585]1956__FUNCTION__, process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
1957#endif
1958
[610]1959    // access local GPT to get GPT_COW flag
1960    bool_t cow = hal_gpt_pte_is_cow( &(process->vmm.gpt), vpn );
1961
1962    if( cow == false ) return EXCP_USER_ERROR;
1963
[585]1964    // get local vseg
1965    error = vmm_get_vseg( process, 
1966                          (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT,
1967                          &vseg );
[440]1968    if( error )
[1]1969    {
[595]1970        printk("\n[PANIC] in %s : vpn %x in process %x not in a registered vseg\n",
[585]1971        __FUNCTION__, vpn, process->pid );
1972
1973        return EXCP_KERNEL_PANIC;
[440]1974    }
[407]1975
[585]1976    // get reference GPT cluster and local pointer
1977    ref_cxy = GET_CXY( process->ref_xp );
1978    ref_ptr = GET_PTR( process->ref_xp );
[407]1979
[610]1980    // build relevant extended pointers on  relevant GPT and  GPT lock
[585]1981    // - access local GPT for a private vseg 
1982    // - access reference GPT for a public vseg
1983    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[440]1984    {
[585]1985        gpt_xp      = XPTR( local_cxy , &process->vmm.gpt );
1986        gpt_lock_xp = XPTR( local_cxy , &process->vmm.gpt_lock );
[1]1987    }
[440]1988    else
[1]1989    {
[585]1990        gpt_xp      = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
1991        gpt_lock_xp = XPTR( ref_cxy , &ref_ptr->vmm.gpt_lock );
[1]1992    }
1993
[585]1994    // take GPT lock in write mode
1995    remote_rwlock_wr_acquire( gpt_lock_xp );
[441]1996
[585]1997    // get current PTE from reference GPT
1998    hal_gpt_get_pte( gpt_xp,
1999                     vpn,
2000                     &old_attr,
2001                     &old_ppn );
[441]2002
[585]2003    // the PTE must be mapped for a COW
2004    if( (old_attr & GPT_MAPPED) == 0 )
2005    {
2006        printk("\n[PANIC] in %s : VPN %x in process %x unmapped\n",
2007        __FUNCTION__, vpn, process->pid );
[407]2008
[585]2009        // release GPT lock in write mode
2010        remote_rwlock_wr_acquire( gpt_lock_xp );
[407]2011
[585]2012        return EXCP_KERNEL_PANIC;
[407]2013    }
2014
[585]2015    // get extended pointer, cluster and local pointer on physical page descriptor
2016    xptr_t   page_xp  = ppm_ppn2page( old_ppn );
2017    cxy_t    page_cxy = GET_CXY( page_xp );
2018    page_t * page_ptr = GET_PTR( page_xp );
[435]2019
[585]2020    // get extended pointers on forks and lock field in page descriptor
2021    xptr_t forks_xp       = XPTR( page_cxy , &page_ptr->forks );
2022    xptr_t forks_lock_xp  = XPTR( page_cxy , &page_ptr->lock );
[407]2023
[585]2024    // take lock protecting "forks" counter
2025    remote_busylock_acquire( forks_lock_xp );
[407]2026
[585]2027    // get number of pending forks from page descriptor
2028    uint32_t forks = hal_remote_l32( forks_xp );
[441]2029
[585]2030    if( forks )        // pending fork => allocate a new page, and copy old to new
2031    {
2032        // allocate a new physical page
2033        page_xp = vmm_page_allocate( vseg , vpn );
2034        if( page_xp == XPTR_NULL ) 
2035        {
2036            printk("\n[PANIC] in %s : no memory for vpn %x in process %x\n",
2037            __FUNCTION__ , vpn, process->pid );
[441]2038
[585]2039            // release GPT lock in write mode
2040            remote_rwlock_wr_acquire( gpt_lock_xp );
[441]2041
[585]2042            // release lock protecting "forks" counter
2043            remote_busylock_release( forks_lock_xp );
[441]2044
[585]2045            return EXCP_KERNEL_PANIC;
2046        }
[441]2047
[585]2048        // compute allocated page PPN
2049        new_ppn = ppm_page2ppn( page_xp );
[441]2050
[585]2051        // copy old page content to new page
2052        xptr_t  old_base_xp = ppm_ppn2base( old_ppn );
2053        xptr_t  new_base_xp = ppm_ppn2base( new_ppn );
2054        memcpy( GET_PTR( new_base_xp ),
2055                GET_PTR( old_base_xp ),
2056                CONFIG_PPM_PAGE_SIZE );
[441]2057
[585]2058        // decrement pending forks counter in page descriptor
2059        hal_remote_atomic_add( forks_xp , -1 );
[441]2060
[585]2061#if(DEBUG_VMM_HANDLE_COW & 1)
2062if( DEBUG_VMM_HANDLE_COW < cycle )
[595]2063printk("\n[%s] thread[%x,%x] : pending forks => allocate a new PPN %x\n",
[585]2064__FUNCTION__, process->pid, this->trdid, new_ppn );
2065#endif
[440]2066
[585]2067    }             
2068    else               // no pending fork => keep the existing page
2069    {
[1]2070
[585]2071#if(DEBUG_VMM_HANDLE_COW & 1)
2072if( DEBUG_VMM_HANDLE_COW < cycle )
[595]2073printk("\n[%s] thread[%x,%x]  no pending forks => keep existing PPN %x\n",
[585]2074__FUNCTION__, process->pid, this->trdid, new_ppn );
2075#endif
2076        new_ppn = old_ppn;
2077    }
[1]2078
[585]2079    // release lock protecting "forks" counter
2080    remote_busylock_release( forks_lock_xp );
2081
2082    // build new_attr : reset COW and set WRITABLE,
2083    new_attr = (old_attr | GPT_WRITABLE) & (~GPT_COW);
2084
2085    // update the relevan GPT
2086    // - private vseg => update local GPT
2087    // - public vseg => update all GPT copies
2088    if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) )
[1]2089    {
[585]2090        hal_gpt_set_pte( gpt_xp,
2091                         vpn,
2092                         new_attr,
2093                         new_ppn );
[1]2094    }
[585]2095    else
[1]2096    {
[585]2097        if( ref_cxy == local_cxy )                  // reference cluster is local
2098        {
2099            vmm_global_update_pte( process,
2100                                   vpn,
2101                                   new_attr,
2102                                   new_ppn );
2103        }
2104        else                                        // reference cluster is remote
2105        {
2106            rpc_vmm_global_update_pte_client( ref_cxy,
2107                                              ref_ptr,
2108                                              vpn,
2109                                              new_attr,
2110                                              new_ppn );
2111        }
[1]2112    }
2113
[585]2114    // release GPT lock in write mode
2115    remote_rwlock_wr_release( gpt_lock_xp );
[21]2116
[585]2117#if DEBUG_VMM_HANDLE_COW
2118cycle = (uint32_t)hal_get_cycles();
2119if( DEBUG_VMM_HANDLE_COW < cycle )
[595]2120printk("\n[%s] thread[%x,%x] exit for vpn %x / core[%x,%d] / cycle %d\n",
[585]2121__FUNCTION__, process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle );
2122#endif
[313]2123
[585]2124     return EXCP_NON_FATAL;
[1]2125
[585]2126}   // end vmm_handle_cow()
2127
Note: See TracBrowser for help on using the repository browser.