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

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

Fix a bug in rpc_vmm_get_pte_client() function (bad RPC index).

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