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

Last change on this file since 443 was 443, checked in by alain, 6 years ago

Fix few bugs whike debugging the sort multi-thread application.

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