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

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

1) Introduce the libsemaphore library.
2) Introduce a small libmath library, required by the "fft" application..
3) Introduce the multithreaded "fft" application.
4) Fix a bad synchronisation bug in the Copy-On-Write mechanism.

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