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

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

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

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