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

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

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

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