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

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

Improve sys_exec.

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