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

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

Fix a bug in scheduler related to RPC blocking.

File size: 66.7 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{
[429]200    assert( (process->ref_xp == XPTR( local_cxy , process )) , __FUNCTION__,
201    "this function must be executed in reference cluster" );
202
[407]203    vmm_t * vmm = &process->vmm;
204    gpt_t * gpt = &vmm->gpt;
205
206    printk("\n***** VSL and GPT for process %x\n\n", 
207    process->pid );
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 )
[433]671printk("\n[DBG] %s : thread %x enter for process %x / cycle %d\n",
672__FUNCTION__ , CURRENT_THREAD , process->pid , cycle );
673#endif
[416]674
[438]675#if (DEBUG_VMM_DESTROY & 1 )
[437]676vmm_display( process , true );
677#endif
678
[433]679    // get pointer on local VMM
[1]680    vmm_t  * vmm = &process->vmm;
681
[408]682    // get extended pointer on VSL root and VSL lock
683    xptr_t   root_xp = XPTR( local_cxy , &vmm->vsegs_root );
684        xptr_t   lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
685
[1]686    // get lock protecting vseg list
[408]687        remote_rwlock_wr_lock( lock_xp );
[1]688
[409]689    // remove all user vsegs registered in VSL
[408]690        while( !xlist_is_empty( root_xp ) )
[1]691        {
[409]692        // get pointer on first vseg in VSL
[408]693                vseg_xp = XLIST_FIRST_ELEMENT( root_xp , vseg_t , xlist );
[433]694        vseg    = GET_PTR( vseg_xp );
[409]695
[438]696#if( DEBUG_VMM_DESTROY & 1 )
697if( DEBUG_VMM_DESTROY < cycle )
[437]698printk("\n[DBG] %s : %s / vpn_base %x / vpn_size %d\n",
699__FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size );
700#endif
701
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 );
710        }
711
[433]712    // release lock protecting VSL
[408]713        remote_rwlock_wr_unlock( lock_xp );
[1]714
715    // remove all vsegs from zombi_lists in MMAP allocator
716    uint32_t i;
717    for( i = 0 ; i<32 ; i++ )
718    {
719            while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) )
720            {
[408]721                    vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , zlist );
[1]722                    vseg_detach( vmm , vseg );
723            vseg_free( vseg );
724            }
725    }
726
[409]727    // release memory allocated to the GPT itself
[1]728    hal_gpt_destroy( &vmm->gpt );
729
[438]730#if DEBUG_VMM_DESTROY
[433]731cycle = (uint32_t)hal_get_cycles();
[438]732if( DEBUG_VMM_DESTROY < cycle )
[433]733printk("\n[DBG] %s : thread %x exit / cycle %d\n",
734__FUNCTION__ , CURRENT_THREAD , cycle );
735#endif
[416]736
[204]737}  // end vmm_destroy()
738
[1]739/////////////////////////////////////////////////
740vseg_t * vmm_check_conflict( process_t * process,
[21]741                             vpn_t       vpn_base,
[1]742                             vpn_t       vpn_size )
743{
744    vmm_t        * vmm = &process->vmm;
[408]745
746    // scan the VSL
[1]747        vseg_t       * vseg;
[408]748    xptr_t         iter_xp;
749    xptr_t         vseg_xp;
750    xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[1]751
[408]752        XLIST_FOREACH( root_xp , iter_xp )
[1]753        {
[408]754                vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]755        vseg    = GET_PTR( vseg_xp );
[204]756
[21]757                if( ((vpn_base + vpn_size) > vseg->vpn_base) &&
758             (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg;
[1]759        }
760    return NULL;
761
[204]762}  // end vmm_check_conflict()
763
[1]764////////////////////////////////////////////////////////////////////////////////////////////
765// This static function is called by the vmm_create_vseg() function, and implements
766// the VMM stack_vseg specific allocator.
767////////////////////////////////////////////////////////////////////////////////////////////
768// @ vmm      : pointer on VMM.
[21]769// @ vpn_base : (return value) first allocated page
[1]770// @ vpn_size : (return value) number of allocated pages
771////////////////////////////////////////////////////////////////////////////////////////////
772static error_t vmm_stack_alloc( vmm_t * vmm,
773                                vpn_t * vpn_base,
774                                vpn_t * vpn_size )
775{
776    // get stack allocator pointer
777    stack_mgr_t * mgr = &vmm->stack_mgr;
778
779    // get lock on stack allocator
780    spinlock_lock( &mgr->lock );
781
782    // get first free slot index in bitmap
783    int32_t index = bitmap_ffc( &mgr->bitmap , 4 );
[179]784    if( (index < 0) || (index > 31) )
785    {
786        spinlock_unlock( &mgr->lock );
787        return ENOMEM;
788    }
[1]789
790    // update bitmap
791    bitmap_set( &mgr->bitmap , index );
[21]792
[1]793    // release lock on stack allocator
794    spinlock_unlock( &mgr->lock );
795
[21]796    // returns vpn_base, vpn_size (one page non allocated)
[1]797    *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1;
798    *vpn_size = CONFIG_VMM_STACK_SIZE - 1;
799    return 0;
800
[204]801} // end vmm_stack_alloc()
802
[1]803////////////////////////////////////////////////////////////////////////////////////////////
804// This static function is called by the vmm_create_vseg() function, and implements
805// the VMM MMAP specific allocator.
806////////////////////////////////////////////////////////////////////////////////////////////
807// @ vmm      : [in] pointer on VMM.
808// @ npages   : [in] requested number of pages.
[21]809// @ vpn_base : [out] first allocated page.
[1]810// @ vpn_size : [out] actual number of allocated pages.
811////////////////////////////////////////////////////////////////////////////////////////////
812static error_t vmm_mmap_alloc( vmm_t * vmm,
813                               vpn_t   npages,
814                               vpn_t * vpn_base,
815                               vpn_t * vpn_size )
816{
817    uint32_t   index;
818    vseg_t   * vseg;
819    vpn_t      base;
820    vpn_t      size;
[21]821    vpn_t      free;
[1]822
[21]823    // mmap vseg size must be power of 2
[1]824    // compute actual size and index in zombi_list array
825    size  = POW2_ROUNDUP( npages );
826    index = bits_log2( size );
827
828    // get mmap allocator pointer
829    mmap_mgr_t * mgr = &vmm->mmap_mgr;
830
831    // get lock on mmap allocator
832    spinlock_lock( &mgr->lock );
833
834    // get vseg from zombi_list or from mmap zone
835    if( list_is_empty( &mgr->zombi_list[index] ) )     // from mmap zone
836    {
837        // check overflow
838        free = mgr->first_free_vpn;
839        if( (free + size) > mgr->vpn_size ) return ENOMEM;
840
841        // update STACK allocator
842        mgr->first_free_vpn += size;
843
844        // compute base
845        base = free;
846    }
847    else                                             // from zombi_list
848    {
849        // get pointer on zombi vseg from zombi_list
[408]850        vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , zlist );
[1]851
852        // remove vseg from free-list
[408]853        list_unlink( &vseg->zlist );
[1]854
855        // compute base
856        base = vseg->vpn_base;
[21]857    }
858
[1]859    // release lock on mmap allocator
860    spinlock_unlock( &mgr->lock );
861
862    // returns vpn_base, vpn_size
863    *vpn_base = base;
864    *vpn_size = size;
865    return 0;
866
[204]867}  // end vmm_mmap_alloc()
868
[407]869////////////////////////////////////////////////
870vseg_t * vmm_create_vseg( process_t   * process,
871                              vseg_type_t   type,
872                          intptr_t      base,
873                              uint32_t      size,
874                          uint32_t      file_offset,
875                          uint32_t      file_size,
876                          xptr_t        mapper_xp,
877                          cxy_t         cxy )
[1]878{
879    vseg_t     * vseg;          // created vseg pointer
[204]880    vpn_t        vpn_base;      // first page index
[1]881    vpn_t        vpn_size;      // number of pages
882        error_t      error;
883
[438]884#if DEBUG_VMM_CREATE_VSEG
[433]885uint32_t cycle = (uint32_t)hal_get_cycles();
[438]886if( DEBUG_VMM_CREATE_VSEG < cycle )
[433]887printk("\n[DBG] %s : thread %x enter / process %x / base %x / size %x / %s / cxy %x / cycle %d\n",
888__FUNCTION__, CURRENT_THREAD, process->pid, base, size, vseg_type_str(type), cxy, cycle );
889#endif
[21]890
[407]891    // get pointer on VMM
892        vmm_t * vmm    = &process->vmm;
[21]893
[204]894    // compute base, size, vpn_base, vpn_size, depending on vseg type
[407]895    // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs
[1]896    if( type == VSEG_TYPE_STACK )
897    {
898        // get vpn_base and vpn_size from STACK allocator
899        error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size );
900        if( error )
901        {
[407]902            printk("\n[ERROR] in %s : no space for stack vseg / process %x in cluster %x\n",
903            __FUNCTION__ , process->pid , local_cxy );
[1]904            return NULL;
905        }
906
907        // compute vseg base and size from vpn_base and vpn_size
908        base = vpn_base << CONFIG_PPM_PAGE_SHIFT;
909        size = vpn_size << CONFIG_PPM_PAGE_SHIFT;
910    }
[21]911    else if( (type == VSEG_TYPE_ANON) ||
912             (type == VSEG_TYPE_FILE) ||
[1]913             (type == VSEG_TYPE_REMOTE) )
914    {
915        // get vpn_base and vpn_size from MMAP allocator
916        vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT;
917        error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size );
918        if( error )
919        {
920            printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n",
921                   __FUNCTION__ , process->pid , local_cxy );
922            return NULL;
923        }
924
925        // compute vseg base and size from vpn_base and vpn_size
926        base = vpn_base << CONFIG_PPM_PAGE_SHIFT;
927        size = vpn_size << CONFIG_PPM_PAGE_SHIFT;
928    }
929    else
930    {
[204]931        uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT;
932        uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT;
933
934        vpn_base = vpn_min;
935            vpn_size = vpn_max - vpn_min + 1;
[1]936    }
937
938    // check collisions
939    vseg = vmm_check_conflict( process , vpn_base , vpn_size );
940    if( vseg != NULL )
941    {
[21]942        printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n"
[1]943               "  overlap existing vseg [vpn_base = %x / vpn_size = %x]\n",
[407]944        __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size );
[1]945        return NULL;
946    }
947
948    // allocate physical memory for vseg descriptor
949        vseg = vseg_alloc();
950        if( vseg == NULL )
951        {
952            printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n",
[407]953        __FUNCTION__ , process->pid );
[1]954        return NULL;
955        }
956
957    // initialize vseg descriptor
[407]958        vseg_init( vseg,
959               type,
960               base,
961               size,
962               vpn_base,
963               vpn_size,
964               file_offset,
965               file_size,
966               mapper_xp,
967               cxy );
[1]968
[408]969    // attach vseg to VSL
970    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
971        remote_rwlock_wr_lock( lock_xp );
[1]972        vseg_attach( vmm , vseg );
[408]973        remote_rwlock_wr_unlock( lock_xp );
[1]974
[438]975#if DEBUG_VMM_CREATE_VSEG
[433]976cycle = (uint32_t)hal_get_cycles();
[438]977if( DEBUG_VMM_CREATE_VSEG < cycle )
[433]978printk("\n[DBG] %s : thread %x exit / process %x / %s / cxy %x / cycle %d\n",
979__FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str(type), cxy, cycle );
980#endif
[21]981
[1]982        return vseg;
983
[406]984}  // vmm_create_vseg()
985
[1]986/////////////////////////////////////
987void vmm_remove_vseg( vseg_t * vseg )
988{
989    // get pointers on calling process and VMM
990    thread_t   * this    = CURRENT_THREAD;
991    process_t  * process = this->process;
992    vmm_t      * vmm     = &this->process->vmm;
993    uint32_t     type    = vseg->type;
994
[408]995    // detach vseg from VSL
996    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
997        remote_rwlock_wr_lock( lock_xp );
998        vseg_detach( &process->vmm , vseg );
999        remote_rwlock_wr_unlock( lock_xp );
[1]1000
1001    // release the stack slot to VMM stack allocator if STACK type
1002    if( type == VSEG_TYPE_STACK )
1003    {
1004        // get pointer on stack allocator
1005        stack_mgr_t * mgr = &vmm->stack_mgr;
1006
1007        // compute slot index
1008        uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE);
1009
1010        // update stacks_bitmap
1011        spinlock_lock( &mgr->lock );
1012        bitmap_clear( &mgr->bitmap , index );
1013        spinlock_unlock( &mgr->lock );
1014    }
1015
1016    // release the vseg to VMM mmap allocator if MMAP type
1017    if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) )
1018    {
1019        // get pointer on mmap allocator
1020        mmap_mgr_t * mgr = &vmm->mmap_mgr;
1021
1022        // compute zombi_list index
1023        uint32_t index = bits_log2( vseg->vpn_size );
1024
1025        // update zombi_list
1026        spinlock_lock( &mgr->lock );
[408]1027        list_add_first( &mgr->zombi_list[index] , &vseg->zlist );
[1]1028        spinlock_unlock( &mgr->lock );
1029    }
1030
1031    // release physical memory allocated for vseg descriptor if no MMAP type
1032    if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) )
1033    {
1034        vseg_free( vseg );
1035    }
[407]1036}  // end vmm_remove_vseg()
[1]1037
[68]1038//////////////////////////////////////////////
1039error_t vmm_map_kernel_vseg( vseg_t    * vseg,
1040                             uint32_t    attr )
[1]1041{
1042    vpn_t       vpn;        // VPN of PTE to be set
1043    vpn_t       vpn_min;    // VPN of first PTE to be set
1044    vpn_t       vpn_max;    // VPN of last PTE to be set (excluded)
1045        ppn_t       ppn;        // PPN of allocated physical page
1046        uint32_t    order;      // ln( number of small pages for one single PTE )
1047        page_t    * page;
1048    error_t     error;
1049
[68]1050    // check vseg type : must be a kernel vseg
[1]1051    uint32_t type = vseg->type;
[68]1052    assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)),
1053            __FUNCTION__ , "not a kernel vseg\n" );
[1]1054
1055    // get pointer on page table
1056    gpt_t * gpt = &process_zero.vmm.gpt;
1057
[21]1058    // define number of small pages per PTE
[1]1059        if( attr & GPT_SMALL ) order = 0;   // 1 small page
1060        else                   order = 9;   // 512 small pages
1061
1062    // loop on pages in vseg
1063    vpn_min = vseg->vpn_base;
1064    vpn_max = vpn_min + vseg->vpn_size;
1065        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
1066        {
[68]1067        // allocate a physical page from local PPM
[1]1068            kmem_req_t req;
1069            req.type  = KMEM_PAGE;
1070            req.size  = order;
1071            req.flags = AF_KERNEL | AF_ZERO;
1072            page      = (page_t *)kmem_alloc( &req );
[21]1073                if( page == NULL )
[1]1074        {
1075            printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ );
1076            return ENOMEM;
1077        }
1078
1079        // set page table entry
[315]1080        ppn = ppm_page2ppn( XPTR( local_cxy , page ) );
[408]1081        error = hal_gpt_set_pte( gpt,
1082                                 vpn,
1083                                 attr,
1084                                 ppn );
[21]1085                if( error )
[1]1086        {
1087            printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ );
1088            return ENOMEM;
1089        }
1090        }
[21]1091
[1]1092        return 0;
1093
[408]1094}  // end vmm_map_kernel_vseg()
1095
[1]1096/////////////////////////////////////////
1097void vmm_unmap_vseg( process_t * process,
1098                     vseg_t    * vseg )
1099{
[21]1100    vpn_t       vpn;        // VPN of current PTE
1101    vpn_t       vpn_min;    // VPN of first PTE
[1]1102    vpn_t       vpn_max;    // VPN of last PTE (excluded)
[409]1103    ppn_t       ppn;        // current PTE ppn value
1104    uint32_t    attr;       // current PTE attributes
1105    kmem_req_t  req;        // request to release memory
1106    xptr_t      page_xp;    // extended pointer on page descriptor
1107    cxy_t       page_cxy;   // page descriptor cluster
1108    page_t    * page_ptr;   // page descriptor pointer
[433]1109    xptr_t      forks_xp;   // extended pointer on pending forks counter
1110    uint32_t    count;      // actual number of pendinf forks
[1]1111
[438]1112#if DEBUG_VMM_UNMAP_VSEG
[433]1113uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1114if( DEBUG_VMM_UNMAP_VSEG < cycle )
[433]1115printk("\n[DBG] %s : thread %x enter / process %x / vseg %s / base %x / cycle %d\n",
1116__FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str( vseg->type ), vseg->vpn_base, cycle );
1117#endif
[409]1118
[433]1119    // get pointer on local GPT
[1]1120    gpt_t     * gpt = &process->vmm.gpt;
1121
1122    // loop on pages in vseg
1123    vpn_min = vseg->vpn_base;
1124    vpn_max = vpn_min + vseg->vpn_size;
1125        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
1126    {
[409]1127        // get GPT entry
1128        hal_gpt_get_pte( gpt , vpn , &attr , &ppn );
1129
1130        if( attr & GPT_MAPPED )  // entry is mapped
1131        { 
[437]1132
[438]1133#if( DEBUG_VMM_UNMAP_VSEG & 1 )
1134if( DEBUG_VMM_UNMAP_VSEG < cycle )
[437]1135printk("- vpn %x / ppn %x\n" , vpn , ppn );
1136#endif
1137
[409]1138            // check small page
1139            assert( (attr & GPT_SMALL) , __FUNCTION__ ,
1140            "an user vseg must use small pages" );
1141
[433]1142            // unmap GPT entry in all GPT copies
[409]1143            hal_gpt_reset_pte( gpt , vpn );
1144
[433]1145            // handle pending forks counter if
1146            // 1) not identity mapped
1147            // 2) running in reference cluster
1148            if( ((vseg->flags & VSEG_IDENT)  == 0) &&
1149                (GET_CXY( process->ref_xp ) == local_cxy) )
[409]1150            {
[433]1151                // get extended pointer on physical page descriptor
[409]1152                page_xp  = ppm_ppn2page( ppn );
1153                page_cxy = GET_CXY( page_xp );
[433]1154                page_ptr = GET_PTR( page_xp );
[409]1155
[433]1156                // FIXME lock the physical page
1157
1158                // get pending forks counter
[437]1159                count = hal_remote_lw( XPTR( page_cxy , &page_ptr->forks ) );
[433]1160               
1161                if( count )  // decrement pending forks counter
[409]1162                {
[437]1163                    forks_xp = XPTR( page_cxy , &page_ptr->forks );
[433]1164                    hal_remote_atomic_add( forks_xp , -1 );
1165                } 
1166                else         // release physical page to relevant cluster
[409]1167                {
[433]1168                    if( page_cxy == local_cxy )   // local cluster
1169                    {
1170                        req.type = KMEM_PAGE;
1171                        req.ptr  = page_ptr; 
1172                        kmem_free( &req );
1173                    }
1174                    else                          // remote cluster
1175                    {
1176                        rpc_pmem_release_pages_client( page_cxy , page_ptr );
1177                    }
[409]1178                }
[433]1179
1180                // FIXME unlock the physical page
[409]1181            }
1182        }
[1]1183    }
[433]1184
[438]1185#if DEBUG_VMM_UNMAP_VSEG
[433]1186cycle = (uint32_t)hal_get_cycles();
[438]1187if( DEBUG_VMM_UNMAP_VSEG < cycle )
[433]1188printk("\n[DBG] %s : thread %x exit / process %x / vseg %s / base %x / cycle %d\n",
1189__FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str( vseg->type ), vseg->vpn_base, cycle );
1190#endif
1191
[409]1192}  // end vmm_unmap_vseg()
[1]1193
[407]1194//////////////////////////////////////////////////////////////////////////////////////////
[406]1195// This low-level static function is called by the vmm_get_vseg() and vmm_resize_vseg()
1196// functions.  It scan the list of registered vsegs to find the unique vseg containing
1197// a given virtual address.
[407]1198//////////////////////////////////////////////////////////////////////////////////////////
[406]1199// @ vmm     : pointer on the process VMM.
1200// @ vaddr   : virtual address.
1201// @ return vseg pointer if success / return NULL if not found.
[407]1202//////////////////////////////////////////////////////////////////////////////////////////
[406]1203static vseg_t * vseg_from_vaddr( vmm_t    * vmm,
1204                                 intptr_t   vaddr )
1205{
[408]1206    xptr_t   iter_xp;
1207    xptr_t   vseg_xp;
1208    vseg_t * vseg;
[406]1209
[408]1210    // get extended pointers on VSL lock and root
1211    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
1212    xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root );
[406]1213
[408]1214    // get lock protecting the VSL
1215    remote_rwlock_rd_lock( lock_xp );
1216
1217    // scan the list of vsegs in VSL
1218    XLIST_FOREACH( root_xp , iter_xp )
[406]1219    {
[408]1220        vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist );
[433]1221        vseg    = GET_PTR( vseg_xp );
[408]1222        if( (vaddr >= vseg->min) && (vaddr < vseg->max) )
1223        {
1224            // return success
1225            remote_rwlock_rd_unlock( lock_xp );
1226            return vseg;
1227        }
[406]1228    }
1229
[408]1230    // return failure
1231    remote_rwlock_rd_unlock( lock_xp );
1232    return NULL;
[406]1233
[408]1234}  // end vseg_from_vaddr()
[406]1235
[1]1236/////////////////////////////////////////////
1237error_t vmm_resize_vseg( process_t * process,
1238                         intptr_t    base,
1239                         intptr_t    size )
1240{
[406]1241    error_t   error;
1242    vseg_t  * new;
1243    vpn_t     vpn_min;
1244    vpn_t     vpn_max;
[1]1245
1246    // get pointer on process VMM
1247    vmm_t * vmm = &process->vmm;
1248
1249    intptr_t addr_min = base;
1250        intptr_t addr_max = base + size;
1251
1252    // get pointer on vseg
[406]1253        vseg_t * vseg = vseg_from_vaddr( vmm , base );
[1]1254
1255        if( vseg == NULL)  return EINVAL;
[21]1256
[408]1257    // get extended pointer on VSL lock
1258    xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock );
[21]1259
[408]1260    // get lock protecting VSL
1261        remote_rwlock_wr_lock( lock_xp );
1262
[1]1263        if( (vseg->min > addr_min) || (vseg->max < addr_max) )   // region not included in vseg
1264    {
1265        error = EINVAL;
1266    }
1267        else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed
1268    {
1269        vmm_remove_vseg( vseg );
1270        error = 0;
1271    }
[406]1272        else if( vseg->min == addr_min )                         // vseg must be resized
[1]1273    {
[406]1274        // update vseg base address
1275        vseg->min = addr_max;
1276
1277        // update vpn_base and vpn_size
1278        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1279        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1280        vseg->vpn_base = vpn_min;
1281        vseg->vpn_size = vpn_max - vpn_min + 1;
1282        error = 0;
[1]1283    }
[406]1284        else if( vseg->max == addr_max )                          // vseg must be resized
[1]1285    {
[406]1286        // update vseg max address
1287        vseg->max = addr_min;
1288
1289        // update vpn_base and vpn_size
1290        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1291        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1292        vseg->vpn_base = vpn_min;
1293        vseg->vpn_size = vpn_max - vpn_min + 1;
1294        error = 0;
[1]1295    }
[406]1296    else                                                      // vseg cut in three regions
[1]1297    {
[406]1298        // resize existing vseg
1299        vseg->max = addr_min;
1300
1301        // update vpn_base and vpn_size
1302        vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT;
1303        vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT;
1304        vseg->vpn_base = vpn_min;
1305        vseg->vpn_size = vpn_max - vpn_min + 1;
1306
1307        // create new vseg
[407]1308        new = vmm_create_vseg( process, 
1309                               vseg->type,
1310                               addr_min, 
1311                               (vseg->max - addr_max),
1312                               vseg->file_offset,
1313                               vseg->file_size,
1314                               vseg->mapper_xp,
1315                               vseg->cxy ); 
1316
[406]1317        if( new == NULL ) error = EINVAL;
1318        else              error = 0;
[1]1319    }
1320
1321    // release VMM lock
[408]1322        remote_rwlock_wr_unlock( lock_xp );
[1]1323
1324        return error;
1325
[406]1326}  // vmm_resize_vseg()
1327
[1]1328///////////////////////////////////////////
[388]1329error_t  vmm_get_vseg( process_t * process,
[394]1330                       intptr_t    vaddr,
[388]1331                       vseg_t   ** found_vseg )
[1]1332{
[406]1333    vmm_t  * vmm = &process->vmm;
[1]1334
[406]1335    // get vseg from vaddr
1336    vseg_t * vseg = vseg_from_vaddr( vmm , vaddr );
[1]1337
[388]1338    if( vseg == NULL )   // vseg not found in local cluster => try to get it from ref
1339        {
1340        // get extended pointer on reference process
1341        xptr_t ref_xp = process->ref_xp;
[1]1342
[388]1343        // get cluster and local pointer on reference process
1344        cxy_t       ref_cxy = GET_CXY( ref_xp );
[433]1345        process_t * ref_ptr = GET_PTR( ref_xp );
[388]1346
1347        if( local_cxy == ref_cxy )  return -1;   // local cluster is the reference
1348
1349        // get extended pointer on reference vseg
1350        xptr_t   vseg_xp;
1351        error_t  error;
[406]1352
[394]1353        rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error );
[388]1354           
1355        if( error )   return -1;       // vseg not found => illegal user vaddr
1356       
1357        // allocate a vseg in local cluster
1358        vseg = vseg_alloc();
1359
[406]1360        if( vseg == NULL ) return -1;
[388]1361
1362        // initialise local vseg from reference
1363        vseg_init_from_ref( vseg , vseg_xp );
1364
1365        // register local vseg in local VMM
[406]1366        vseg_attach( &process->vmm , vseg );
[388]1367    }   
1368   
1369    // success
1370    *found_vseg = vseg;
[394]1371    return 0;
[388]1372
1373}  // end vmm_get_vseg()
1374
[407]1375//////////////////////////////////////////////////////////////////////////////////////
1376// This static function compute the target cluster to allocate a physical page
1377// for a given <vpn> in a given <vseg>, allocates the page (with an RPC if required)
1378// and returns an extended pointer on the allocated page descriptor.
1379// The vseg cannot have the FILE type.
1380//////////////////////////////////////////////////////////////////////////////////////
1381static xptr_t vmm_page_allocate( vseg_t * vseg,
1382                                 vpn_t    vpn )
1383{
[433]1384
[438]1385#if DEBUG_VMM_ALLOCATE_PAGE
1386if( DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() )
[433]1387printk("\n[DBG] in %s : thread %x enter for vpn %x\n",
1388__FUNCTION__ , CURRENT_THREAD, vpn );
1389#endif
1390
[407]1391    // compute target cluster
1392    page_t     * page_ptr;
1393    cxy_t        page_cxy;
1394    kmem_req_t   req;
1395
1396    uint32_t     type  = vseg->type;
1397    uint32_t     flags = vseg->flags;
1398
1399    assert( ( type != VSEG_TYPE_FILE ) , __FUNCTION__ , "illegal vseg type\n" );
1400
1401    if( flags & VSEG_DISTRIB )    // distributed => cxy depends on vpn LSB
1402    {
1403        uint32_t x_size  = LOCAL_CLUSTER->x_size;
1404        uint32_t y_size  = LOCAL_CLUSTER->y_size;
1405        uint32_t y_width = LOCAL_CLUSTER->y_width;
1406        uint32_t index   = vpn & ((x_size * y_size) - 1);
1407        uint32_t x       = index / y_size;
1408        uint32_t y       = index % y_size;
1409        page_cxy         = (x<<y_width) + y;
1410    }
1411    else                          // other cases => cxy specified in vseg
1412    {
1413        page_cxy         = vseg->cxy;
1414    }
1415
1416    // allocate a physical page from target cluster
1417    if( page_cxy == local_cxy )  // target cluster is the local cluster
1418    {
1419        req.type  = KMEM_PAGE;
1420        req.size  = 0;
1421        req.flags = AF_NONE;
1422        page_ptr  = (page_t *)kmem_alloc( &req );
1423    }
1424    else                           // target cluster is not the local cluster
1425    {
1426        rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr );
1427    }
1428
[438]1429#if DEBUG_VMM_ALLOCATE_PAGE
1430if( DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() )
[433]1431printk("\n[DBG] in %s : thread %x exit for vpn = %d / ppn = %x\n",
1432__FUNCTION__ , CURRENT_THREAD, vpn, ppm_page2ppn( XPTR( page_cxy , page_ptr ) ) );
1433#endif
1434
[407]1435    if( page_ptr == NULL ) return XPTR_NULL;
1436    else                   return XPTR( page_cxy , page_ptr );
1437
1438}  // end vmm_page_allocate() 
1439
[313]1440////////////////////////////////////////
1441error_t vmm_get_one_ppn( vseg_t * vseg,
1442                         vpn_t    vpn,
1443                         ppn_t  * ppn )
1444{
1445    error_t    error;
[407]1446    xptr_t     page_xp;           // extended pointer on physical page descriptor
[313]1447    page_t   * page_ptr;          // local pointer on physical page descriptor
[406]1448    uint32_t   index;             // missing page index in vseg mapper
1449    uint32_t   type;              // vseg type;
[313]1450
[406]1451    type      = vseg->type;
1452    index     = vpn - vseg->vpn_base;
[313]1453
[438]1454#if DEBUG_VMM_GET_ONE_PPN
1455if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1456printk("\n[DBG] %s : thread %x enter for vpn = %x / type = %s / index = %d\n",
1457__FUNCTION__, CURRENT_THREAD, vpn, vseg_type_str(type), index );
1458#endif
[313]1459
[406]1460    // FILE type : get the physical page from the file mapper
[313]1461    if( type == VSEG_TYPE_FILE )
1462    {
[406]1463        // get extended pointer on mapper
[407]1464        xptr_t mapper_xp = vseg->mapper_xp;
[313]1465
[406]1466        assert( (mapper_xp != XPTR_NULL), __FUNCTION__,
1467        "mapper not defined for a FILE vseg\n" );
1468       
1469        // get mapper cluster and local pointer
1470        cxy_t      mapper_cxy = GET_CXY( mapper_xp );
[433]1471        mapper_t * mapper_ptr = GET_PTR( mapper_xp );
[406]1472
[313]1473        // get page descriptor from mapper
1474        if( mapper_cxy == local_cxy )             // mapper is local
1475        {
1476            page_ptr = mapper_get_page( mapper_ptr , index );
1477        }
1478        else                                      // mapper is remote
1479        {
1480            rpc_mapper_get_page_client( mapper_cxy , mapper_ptr , index , &page_ptr );
1481        }
1482
1483        if ( page_ptr == NULL ) return EINVAL;
1484
[407]1485        page_xp = XPTR( mapper_cxy , page_ptr );
[313]1486    }
1487
[406]1488    // Other types : allocate a physical page from target cluster,
[407]1489    // as defined by vseg type and vpn value
[313]1490    else
1491    {
[433]1492        // allocate one physical page
[407]1493        page_xp = vmm_page_allocate( vseg , vpn );
[406]1494
[407]1495        if( page_xp == XPTR_NULL ) return ENOMEM;
[313]1496
[406]1497        // initialise missing page from .elf file mapper for DATA and CODE types
[433]1498        // (the vseg->mapper_xp field is an extended pointer on the .elf file mapper)
[313]1499        if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) )
1500        {
[406]1501            // get extended pointer on mapper
1502            xptr_t     mapper_xp = vseg->mapper_xp;
[313]1503
[406]1504            assert( (mapper_xp != XPTR_NULL), __FUNCTION__,
1505            "mapper not defined for a CODE or DATA vseg\n" );
1506       
1507            // get mapper cluster and local pointer
1508            cxy_t      mapper_cxy = GET_CXY( mapper_xp );
[433]1509            mapper_t * mapper_ptr = GET_PTR( mapper_xp );
[406]1510
1511            // compute missing page offset in vseg
1512            uint32_t offset = index << CONFIG_PPM_PAGE_SHIFT;
1513
[313]1514            // compute missing page offset in .elf file
[406]1515            uint32_t elf_offset = vseg->file_offset + offset;
[313]1516
[438]1517#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
1518if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1519printk("\n[DBG] %s : thread %x for vpn = %x / elf_offset = %x\n",
1520__FUNCTION__, CURRENT_THREAD, vpn, elf_offset );
1521#endif
[313]1522
[406]1523            // compute extended pointer on page base
[407]1524            xptr_t base_xp  = ppm_page2base( page_xp );
[313]1525
[406]1526            // file_size (in .elf mapper) can be smaller than vseg_size (BSS)
1527            uint32_t file_size = vseg->file_size;
1528
1529            if( file_size < offset )                 // missing page fully in  BSS
[313]1530            {
[406]1531
[438]1532#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
1533if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1534printk("\n[DBG] %s : thread%x for vpn = %x / fully in BSS\n",
1535__FUNCTION__, CURRENT_THREAD, vpn );
1536#endif
1537
[407]1538                if( GET_CXY( page_xp ) == local_cxy )
[313]1539                {
[315]1540                    memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE );
[313]1541                }
1542                else
1543                {
[315]1544                   hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE );       
[313]1545                }
1546            }
[406]1547            else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) )  // fully in  mapper
[315]1548            {
[406]1549
[438]1550#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
1551if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1552printk("\n[DBG] %s : thread %x, for vpn = %x / fully in mapper\n",
1553__FUNCTION__, CURRENT_THREAD, vpn );
1554#endif
[407]1555
[313]1556                if( mapper_cxy == local_cxy ) 
1557                {
1558                    error = mapper_move_kernel( mapper_ptr,
1559                                                true,             // to_buffer
[406]1560                                                elf_offset,
[313]1561                                                base_xp,
[315]1562                                                CONFIG_PPM_PAGE_SIZE ); 
[313]1563                }
1564                else 
1565                {
1566                    rpc_mapper_move_buffer_client( mapper_cxy,
1567                                                   mapper_ptr,
1568                                                   true,         // to buffer
1569                                                   false,        // kernel buffer
[406]1570                                                   elf_offset,
1571                                                   base_xp,
[315]1572                                                   CONFIG_PPM_PAGE_SIZE,
[313]1573                                                   &error );
1574                }
1575                if( error ) return EINVAL;
1576            }
[406]1577            else  // both in mapper and in BSS :
1578                  // - (file_size - offset)             bytes from mapper
1579                  // - (page_size + offset - file_size) bytes from BSS
[313]1580            {
[406]1581
[438]1582#if (DEBUG_VMM_GET_ONE_PPN & 0x1)
1583if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1584printk("\n[DBG] %s : thread %x for vpn = %x / both mapper & BSS\n"
1585"      %d bytes from mapper / %d bytes from BSS\n",
1586__FUNCTION__, CURRENT_THREAD, vpn,
[407]1587file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size  );
[433]1588#endif
[313]1589                // initialize mapper part
[315]1590                if( mapper_cxy == local_cxy )
[313]1591                {
1592                    error = mapper_move_kernel( mapper_ptr,
[315]1593                                                true,         // to buffer
[406]1594                                                elf_offset,
[313]1595                                                base_xp,
[406]1596                                                file_size - offset ); 
[313]1597                }
[315]1598                else                               
[313]1599                {
1600                    rpc_mapper_move_buffer_client( mapper_cxy,
1601                                                   mapper_ptr,
[315]1602                                                   true,         // to buffer
[313]1603                                                   false,        // kernel buffer
[406]1604                                                   elf_offset,
1605                                                   base_xp,
1606                                                   file_size - offset, 
[313]1607                                                   &error );
1608                }
1609                if( error ) return EINVAL;
1610
1611                // initialize BSS part
[407]1612                if( GET_CXY( page_xp ) == local_cxy )
[313]1613                {
[406]1614                    memset( GET_PTR( base_xp ) + file_size - offset , 0 , 
1615                            offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]1616                }
1617                else
1618                {
[406]1619                   hal_remote_memset( base_xp + file_size - offset , 0 , 
1620                                      offset + CONFIG_PPM_PAGE_SIZE - file_size );
[313]1621                }
1622            }   
1623        }  // end initialisation for CODE or DATA types   
1624    } 
1625
1626    // return ppn
[407]1627    *ppn = ppm_page2ppn( page_xp );
[406]1628
[438]1629#if DEBUG_VMM_GET_ONE_PPN
1630if( DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() )
[433]1631printk("\n[DBG] %s : thread %x exit for vpn = %x / ppn = %x\n",
1632__FUNCTION__ , CURRENT_THREAD , vpn , *ppn );
1633#endif
[406]1634
[313]1635    return 0;
1636
1637}  // end vmm_get_one_ppn()
1638
[1]1639/////////////////////////////////////////
1640error_t vmm_get_pte( process_t * process,
1641                     vpn_t       vpn,
[407]1642                     bool_t      cow,
1643                     uint32_t  * attr,
1644                     ppn_t     * ppn )
[1]1645{
[433]1646    vseg_t  * vseg;       // vseg containing VPN
[407]1647    ppn_t     old_ppn;    // current PTE_PPN
1648    uint32_t  old_attr;   // current PTE_ATTR
1649    ppn_t     new_ppn;    // new PTE_PPN
1650    uint32_t  new_attr;   // new PTE_ATTR
[1]1651    error_t   error;
1652
[178]1653    // this function must be called by a thread running in the reference cluster
[68]1654    assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ ,
[406]1655    "not called in the reference cluster\n" );
[1]1656
[438]1657#if DEBUG_VMM_GET_PTE
[433]1658uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1659if( DEBUG_VMM_GET_PTE < cycle )
1660printk("\n[DBG] %s : thread %x enter / vpn %x / process %x / cow %d / cycle %d\n",
[433]1661__FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cow , cycle );
1662#endif
[406]1663
[1]1664    // get VMM pointer
1665    vmm_t * vmm = &process->vmm;
1666
[433]1667    // get vseg pointer from reference VSL
[407]1668    error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg );
[1]1669
[407]1670    if( error )
[1]1671    {
[407]1672        printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n",
1673        __FUNCTION__ , process->pid , vpn );
1674        return error;
1675    }
[406]1676
[438]1677#if( DEBUG_VMM_GET_PTE & 1 )
[433]1678cycle = (uint32_t)hal_get_cycles();
[438]1679if( DEBUG_VMM_GET_PTE < cycle )
[433]1680printk("\n[DBG] %s : thread %x found vseg %s / vpn_base = %x / vpn_size = %x\n",
1681__FUNCTION__, CURRENT_THREAD, vseg_type_str(vseg->type), vseg->vpn_base, vseg->vpn_size );
1682#endif
[1]1683
[438]1684    if( cow )  //////////////// copy_on_write request //////////////////////
1685               // get PTE from reference GPT
1686               // allocate a new physical page if there is pending forks,
1687               // initialize it from old physical page content,
1688               // update PTE in all GPT copies,
1689    {
1690        // access GPT to get current PTE attributes and PPN
1691        hal_gpt_get_pte( &vmm->gpt , vpn , &old_attr , &old_ppn );
[407]1692
[408]1693        assert( (old_attr & GPT_MAPPED) , __FUNCTION__ , 
1694        "PTE must be mapped for a copy-on-write exception\n" );
[407]1695
[438]1696#if( DEBUG_VMM_GET_PTE & 1 )
[433]1697cycle = (uint32_t)hal_get_cycles();
[438]1698if( DEBUG_VMM_GET_PTE < cycle )
[433]1699printk("\n[DBG] %s : thread %x handling COW for vpn %x in process %x\n",
1700__FUNCTION__, CURRENT_THREAD, vpn, process->pid );
1701#endif
[407]1702
[433]1703        // get extended pointer, cluster and local pointer on physical page descriptor
[408]1704        xptr_t   page_xp  = ppm_ppn2page( old_ppn );
1705        cxy_t    page_cxy = GET_CXY( page_xp );
[433]1706        page_t * page_ptr = GET_PTR( page_xp );
[407]1707
[408]1708        // get number of pending forks in page descriptor
[433]1709        uint32_t forks = hal_remote_lw( XPTR( page_cxy , &page_ptr->forks ) );
[408]1710
[433]1711        if( forks )        // pending fork => allocate a new page, copy old to new
[1]1712        {
[408]1713            // allocate a new physical page
1714            page_xp = vmm_page_allocate( vseg , vpn );
1715            if( page_xp == XPTR_NULL ) 
1716            {
1717                printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
1718                __FUNCTION__ , process->pid , vpn );
1719                return -1;
1720            }
[1]1721
[408]1722            // compute allocated page PPN
1723            new_ppn = ppm_page2ppn( page_xp );
[406]1724
[408]1725            // copy old page content to new page
1726            xptr_t  old_base_xp = ppm_ppn2base( old_ppn );
1727            xptr_t  new_base_xp = ppm_ppn2base( new_ppn );
1728            memcpy( GET_PTR( new_base_xp ),
1729                    GET_PTR( old_base_xp ),
1730                    CONFIG_PPM_PAGE_SIZE );
1731        }             
1732        else               // no pending fork => keep the existing page, reset COW
1733        {
1734            new_ppn = old_ppn;
1735        }
[1]1736
[408]1737        // build new_attr : reset COW and set WRITABLE,
1738        new_attr = (old_attr | GPT_WRITABLE) & (~GPT_COW);
[407]1739
[408]1740        // update GPT[vpn] for all GPT copies
[433]1741        vmm_global_update_pte( process, vpn, new_attr, new_ppn );
[407]1742
[433]1743        // decrement pending forks counter in page descriptor
1744        hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , -1 );
[407]1745    }
[438]1746    else        //////////// page_fault request ///////////////////////////
1747                // get PTE from reference GPT
1748                // allocate a physical page if it is a true page fault,
1749                // register in reference GPT, but don't update GPT copies
[407]1750    { 
[438]1751        // access GPT to get current PTE
1752        hal_gpt_get_pte( &vmm->gpt , vpn , &old_attr , &old_ppn );
1753
[408]1754        if( (old_attr & GPT_MAPPED) == 0 )   // true page_fault => map it
[407]1755        {
[1]1756
[438]1757#if( DEBUG_VMM_GET_PTE & 1 )
[433]1758cycle = (uint32_t)hal_get_cycles();
[438]1759if( DEBUG_VMM_GET_PTE < cycle )
[433]1760printk("\n[DBG] %s : thread %x handling page fault for vpn %x in process %x\n",
1761__FUNCTION__, CURRENT_THREAD, vpn, process->pid );
1762#endif
[1]1763
[408]1764            // allocate new_ppn, depending on vseg type
[407]1765            error = vmm_get_one_ppn( vseg , vpn , &new_ppn );
1766            if( error )
1767            {
1768                printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n",
1769                __FUNCTION__ , process->pid , vpn );
[408]1770                return -1;
[407]1771            }
1772
[408]1773            // define new_attr from vseg flags
[407]1774            new_attr = GPT_MAPPED | GPT_SMALL;
1775            if( vseg->flags & VSEG_USER  ) new_attr |= GPT_USER;
1776            if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE;
1777            if( vseg->flags & VSEG_EXEC  ) new_attr |= GPT_EXECUTABLE;
1778            if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE;
1779
[408]1780            // register new PTE in reference GPT
1781            // on demand policy => no update of GPT copies
1782            error = hal_gpt_set_pte( &vmm->gpt,
1783                                     vpn,
1784                                     new_attr,
1785                                     new_ppn );
[407]1786            if( error )
1787            {
1788                printk("\n[ERROR] in %s : cannot update GPT / process = %x / vpn = %x\n",
1789                __FUNCTION__ , process->pid , vpn );
[408]1790                return -1;
[407]1791            }
1792        }
[408]1793        else                                  // mapped in reference GPT => get it
[1]1794        {
[408]1795            new_ppn  = old_ppn;
[407]1796            new_attr = old_attr;
[1]1797        }
[407]1798    }
[1]1799
[438]1800#if DEBUG_VMM_GET_PTE
[433]1801cycle = (uint32_t)hal_get_cycles();
[438]1802if( DEBUG_VMM_GET_PTE < cycle )
1803printk("\n[DBG] %s : thread,%x exit / vpn %x in process %x / ppn %x / attr %x / cycle %d\n",
[433]1804__FUNCTION__, CURRENT_THREAD, vpn, process->pid, new_ppn, new_attr, cycle );
1805#endif
[406]1806
[438]1807    // return PPN and flags
[407]1808    *ppn  = new_ppn;
1809    *attr = new_attr;
[1]1810    return 0;
1811
[313]1812}  // end vmm_get_pte()
1813
[1]1814///////////////////////////////////////////////////
1815error_t vmm_handle_page_fault( process_t * process,
1816                               vpn_t       vpn )
1817{
1818    uint32_t         attr;          // missing page attributes
[21]1819    ppn_t            ppn;           // missing page PPN
[407]1820    error_t          error;
[1]1821
[438]1822#if DEBUG_VMM_GET_PTE
[435]1823uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1824if( DEBUG_VMM_GET_PTE < cycle )
[435]1825printk("\n[DBG] %s : thread %x enter for vpn %x / process %x / cycle %d\n",
1826__FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle );
1827#endif
1828
[1]1829    // get reference process cluster and local pointer
1830    cxy_t       ref_cxy = GET_CXY( process->ref_xp );
[433]1831    process_t * ref_ptr = GET_PTR( process->ref_xp );
[1]1832
[313]1833    // get missing PTE attributes and PPN from reference cluster
[407]1834    if( local_cxy != ref_cxy ) 
[1]1835    {
[407]1836        rpc_vmm_get_pte_client( ref_cxy,
1837                                ref_ptr,
1838                                vpn,
1839                                false,    // page_fault
1840                                &attr,
1841                                &ppn,
1842                                &error );
1843
1844        // get local VMM pointer
1845        vmm_t * vmm = &process->vmm;
1846
1847        // update local GPT
[408]1848        error |= hal_gpt_set_pte( &vmm->gpt,
1849                                  vpn,
1850                                  attr,
1851                                  ppn );
[1]1852    }
[407]1853    else   // local cluster is the reference cluster
[1]1854    {
[407]1855        error = vmm_get_pte( process,
1856                             vpn,
1857                             false,      // page-fault
1858                             &attr,
1859                             &ppn );
[1]1860    }
1861
[438]1862#if DEBUG_VMM_GET_PTE
[435]1863cycle = (uint32_t)hal_get_cycles();
[438]1864if( DEBUG_VMM_GET_PTE < cycle )
[435]1865printk("\n[DBG] %s : thread %x exit for vpn %x / process %x / cycle %d\n",
1866__FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle );
1867#endif
1868
[388]1869    return error;
[1]1870
[313]1871}  // end vmm_handle_page_fault()
1872
[408]1873////////////////////////////////////////////
1874error_t vmm_handle_cow( process_t * process,
1875                        vpn_t       vpn )
[407]1876{
[433]1877    uint32_t         attr;          // page attributes
1878    ppn_t            ppn;           // page PPN
[407]1879    error_t          error;
[313]1880
[438]1881#if DEBUG_VMM_GET_PTE
[435]1882uint32_t cycle = (uint32_t)hal_get_cycles();
[438]1883if( DEBUG_VMM_GET_PTE < cycle )
[435]1884printk("\n[DBG] %s : thread %x enter for vpn %x / process %x / cycle %d\n",
1885__FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle );
1886#endif
[433]1887   
[407]1888    // get reference process cluster and local pointer
1889    cxy_t       ref_cxy = GET_CXY( process->ref_xp );
[433]1890    process_t * ref_ptr = GET_PTR( process->ref_xp );
[407]1891
1892    // get new PTE attributes and PPN from reference cluster
1893    if( local_cxy != ref_cxy )
1894    {
1895        rpc_vmm_get_pte_client( ref_cxy,
1896                                ref_ptr,
1897                                vpn,
1898                                true,     // copy-on-write
1899                                &attr,
1900                                &ppn,
1901                                &error );
1902
1903        // get local VMM pointer
1904        vmm_t * vmm = &process->vmm;
1905
1906        // update local GPT
[408]1907        error |= hal_gpt_set_pte( &vmm->gpt,
1908                                  vpn,
1909                                  attr,
1910                                  ppn );
[407]1911    }
1912    else   // local cluster is the reference cluster
1913    {
1914        error = vmm_get_pte( process,
1915                             vpn,
1916                             true,      // copy-on-write
1917                             &attr,
1918                             &ppn );
1919    }
1920
[438]1921#if DEBUG_VMM_GET_PTE
[435]1922cycle = (uint32_t)hal_get_cycles();
[438]1923if( DEBUG_VMM_GET_PTE < cycle )
[435]1924printk("\n[DBG] %s : thread %x exit for vpn %x / process %x / cycle %d\n",
1925__FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle );
1926#endif
1927
[407]1928    return error;
1929
[408]1930}  // end vmm_handle_cow()
[407]1931
[1]1932///////////////////////////////////////////
1933error_t vmm_v2p_translate( bool_t    ident,
1934                           void    * ptr,
1935                           paddr_t * paddr )
1936{
[23]1937    process_t * process = CURRENT_THREAD->process;
[1]1938
1939    if( ident )  // identity mapping
1940    {
[23]1941        *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr );
[1]1942        return 0;
1943    }
1944
[21]1945    // access page table
[1]1946    error_t  error;
1947    vpn_t    vpn;
1948    uint32_t attr;
1949    ppn_t    ppn;
1950    uint32_t offset;
1951
[23]1952    vpn    = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT );
1953    offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK );
[1]1954
[50]1955    if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process
[1]1956    {
[407]1957        error = vmm_get_pte( process, vpn , false , &attr , &ppn );
[1]1958    }
[50]1959    else                                         // calling process is not reference process
[1]1960    {
1961        cxy_t       ref_cxy = GET_CXY( process->ref_xp );
[433]1962        process_t * ref_ptr = GET_PTR( process->ref_xp );
[407]1963        rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , false , &attr , &ppn , &error );
[1]1964    }
1965
[23]1966    // set paddr
[1]1967    *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset;
[21]1968
[23]1969    return error;
[313]1970
[315]1971}  // end vmm_v2p_translate()
[1]1972
[315]1973
Note: See TracBrowser for help on using the repository browser.