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

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

Fix several bugs in the fork() syscall.

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