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

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

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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