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

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

blap

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