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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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