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

Last change on this file since 568 was 567, checked in by alain, 5 years ago

Complete restructuration of kernel locks.

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