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

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

Fix bugs in exec

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