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

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

Fix several GCC warning related to the -Wextra compilation option.

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