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

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

1) introduce a dev_ioc_sync_write() function in IOC API,

to improve the DEVFS synchronous update.

2) fix a big bug in both the user_dir_create() and user_dir_destroy()

functions: add an extended pointer on the reference client process
in the function's arguments.

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