Changeset 409 for trunk/kernel/mm


Ignore:
Timestamp:
Dec 20, 2017, 4:51:09 PM (6 years ago)
Author:
alain
Message:

Fix bugs in exec

Location:
trunk/kernel/mm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/mm/ppm.h

    r407 r409  
    3434
    3535/*****************************************************************************************
    36  * This structure defines the Physical Memory Manager in a cluster.
    37  * In all clusters, the physical memory bank starts at local physical address 0.
    38  * The size of this local physical memory is defined by the <pages_nr> field in the
     36 * This structure defines the Physical Pages Manager in a cluster.
     37 * In each cluster, the physical memory bank starts at local physical address 0 and
     38 * contains an integer number of pages, is defined by the <pages_nr> field in the
    3939 * boot_info structure. It is split in three parts:
    4040 * - the "kernel_code" section contains the kernel code, loaded by the boot-loader.
  • trunk/kernel/mm/vmm.c

    r408 r409  
    2828#include <hal_special.h>
    2929#include <hal_gpt.h>
     30#include <hal_vmm.h>
    3031#include <printk.h>
    3132#include <memcpy.h>
     
    8384             "STACK zone too small\n");
    8485
    85     // register kentry vseg in VMM
     86    // register kentry vseg in VSL
    8687    base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT;
    8788    size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT;
     
    100101    vmm->kent_vpn_base = base;
    101102
    102     // register args vseg in VMM
     103    // register args vseg in VSL
    103104    base = (CONFIG_VMM_KENTRY_BASE +
    104105            CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT;
     
    118119    vmm->args_vpn_base = base;
    119120
    120     // register the envs vseg in VMM
     121    // register the envs vseg in VSL
    121122    base = (CONFIG_VMM_KENTRY_BASE +
    122123            CONFIG_VMM_KENTRY_SIZE +
     
    137138    vmm->envs_vpn_base = base;
    138139
    139     // initialize generic page table
     140    // create GPT (empty)
    140141    error = hal_gpt_create( &vmm->gpt );
    141142
    142     assert( (error == 0) , __FUNCTION__ , "cannot initialize page table\n");
     143    assert( (error == 0) , __FUNCTION__ , "cannot create GPT\n");
     144
     145    // architecture specific GPT initialization
     146    // (For TSAR, identity map the kentry_vseg)
     147    error = hal_vmm_init( vmm );
     148
     149    assert( (error == 0) , __FUNCTION__ , "cannot initialize GPT\n");
    143150
    144151    // initialize STACK allocator
     
    154161
    155162    // initialize instrumentation counters
    156         vmm->pgfault_nr          = 0;
     163        vmm->pgfault_nr = 0;
    157164
    158165    hal_fence();
     
    534541    vmm_t  * vmm = &process->vmm;
    535542
     543// @@@
     544vmm_display( process , true );
     545// @@@
     546
    536547    // get extended pointer on VSL root and VSL lock
    537548    xptr_t   root_xp = XPTR( local_cxy , &vmm->vsegs_root );
     
    541552        remote_rwlock_wr_lock( lock_xp );
    542553
    543     // remove all vsegs registered in VSL
     554    // remove all user vsegs registered in VSL
    544555        while( !xlist_is_empty( root_xp ) )
    545556        {
     557        // get pointer on first vseg in VSL
    546558                vseg_xp = XLIST_FIRST_ELEMENT( root_xp , vseg_t , xlist );
    547559        vseg = (vseg_t *)GET_PTR( vseg_xp );
     560
     561printk("\n@@@ %s : vseg %s\n", __FUNCTION__ , vseg_type_str( vseg->type ) );
     562
     563        // unmap and release all pages
     564        vmm_unmap_vseg( process , vseg );
     565
     566        // remove vseg from VSL
    548567                vseg_detach( vmm , vseg );
     568
     569        // release memory allocated to vseg descriptor
    549570        vseg_free( vseg );
    550571        }
     
    565586    }
    566587
    567     // release memory allocated to the local page table
     588    // release memory allocated to the GPT itself
    568589    hal_gpt_destroy( &vmm->gpt );
    569590
     
    928949    vpn_t       vpn_min;    // VPN of first PTE
    929950    vpn_t       vpn_max;    // VPN of last PTE (excluded)
    930 
    931     // get pointer on process page table
     951    ppn_t       ppn;        // current PTE ppn value
     952    uint32_t    attr;       // current PTE attributes
     953    kmem_req_t  req;        // request to release memory
     954    xptr_t      page_xp;    // extended pointer on page descriptor
     955    cxy_t       page_cxy;   // page descriptor cluster
     956    page_t    * page_ptr;   // page descriptor pointer
     957
     958vmm_dmsg("\n[DBG] %s : core[%x, %d] enter / process %x / vseg %s / base %x / cycle %d\n",
     959__FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, process->pid ,
     960vseg_type_str( vseg->type ), vseg->vpn_base, (uint32_t)hal_get_cycles() );
     961
     962    // get pointer on process GPT
    932963    gpt_t     * gpt = &process->vmm.gpt;
    933964
     
    937968        for( vpn = vpn_min ; vpn < vpn_max ; vpn++ )
    938969    {
    939         hal_gpt_reset_pte( gpt , vpn );
    940     }
    941 }
     970        // get GPT entry
     971        hal_gpt_get_pte( gpt , vpn , &attr , &ppn );
     972
     973        if( attr & GPT_MAPPED )  // entry is mapped
     974        {
     975            // check small page
     976            assert( (attr & GPT_SMALL) , __FUNCTION__ ,
     977            "an user vseg must use small pages" );
     978
     979            // unmap GPT entry
     980            hal_gpt_reset_pte( gpt , vpn );
     981
     982            // release memory if not identity mapped
     983            if( (vseg->flags & VSEG_IDENT)  == 0 )
     984            {
     985                // get extended pointer on page descriptor
     986                page_xp  = ppm_ppn2page( ppn );
     987                page_cxy = GET_CXY( page_xp );
     988                page_ptr = (page_t *)GET_PTR( page_xp );
     989
     990                // release physical page to relevant cluster
     991                if( page_cxy == local_cxy )                   // local cluster
     992                {
     993                    req.type = KMEM_PAGE;
     994                    req.ptr  = page_ptr;
     995                    kmem_free( &req );
     996                }
     997                else                                          // remote cluster
     998                {
     999                    rpc_pmem_release_pages_client( page_cxy , page_ptr );
     1000                }
     1001            }
     1002        }
     1003    }
     1004}  // end vmm_unmap_vseg()
    9421005
    9431006//////////////////////////////////////////////////////////////////////////////////////////
  • trunk/kernel/mm/vmm.h

    r408 r409  
    131131 * - It initializes the STACK and MMAP allocators.
    132132 * - It registers the "kentry", "args", "envs" vsegs in the VSL.
     133 * - It initializes the generic page table, calling the HAL specific hal_gpt_init() function.
     134 * - For TSAR it map all pages for the "kentry" vseg, that must be identity mapping.
     135 * Note:
    133136 * - The "code" and "data" vsegs are registered by the elf_load_process() function.
    134137 * - The "stack" vsegs are dynamically created by the thread_user_create() function.
    135  * - The "file", "anon", "remote" vsegs are dynamically created by the mmap() syscalls.
    136  * - It initializes the generic page table, calling the HAL specific hal_gpt_init() function.
    137  * - For TSAR it map all pages for the "kentry" vseg, that must be identity mapping.
     138 * - The "file", "anon", "remote" vsegs are dynamically created by the mmap() syscall.
    138139 * TODO : Any error in this function gives a kernel panic => improve error handling.
    139140 *********************************************************************************************
     
    206207
    207208/*********************************************************************************************
    208  * This function removes all vsegs registered in in the virtual memory manager of the
    209  * process identified by the <process> argument.
    210  * It releases the memory allocated to the local generic page table.
     209 * This function scan the list of vsegs registered in the VSL of the process
     210 * identified by the <process> argument, and for each vseg:
     211 * - it unmap from the GPT and releases all mapped pages in vseg.
     212 * - it removes the vseg from the process VSL.
     213 * - It releases the memory allocated to the vseg descriptor.
     214 * Finally, it releases the memory allocated to the GPT itself.
    211215 *********************************************************************************************
    212216 * @ process   : pointer on process descriptor.
     
    286290
    287291/*********************************************************************************************
    288  * This function unmaps all PTEs of a given vseg, in the generic page table associated
    289  * to a given process descriptor, and releases the corresponding physical memory.
    290  * It can be used for any type of vseg.
     292 * This function unmaps all mapped PTEs of a given vseg, from the generic page table
     293 * associated to a given process descriptor, and releases the physical memory allocated
     294 * to all mapped GPT entries.  It can be used for any type of vseg.
    291295 *********************************************************************************************
    292296 * @ process  : pointer on process descriptor.
  • trunk/kernel/mm/vseg.h

    r408 r409  
    3535struct vmm_s;
    3636
    37 /**********************************************************************************************
     37/*******************************************************************************************
    3838 * This enum defines the vseg types for an user process.
    39  *********************************************************************************************/
     39 ***********************************************************************************VSEG*******/
    4040
    4141typedef enum
    4242{
    43     VSEG_TYPE_CODE   = 0,          /*! executable user code   / private / localized          */
    44     VSEG_TYPE_DATA   = 1,          /*! initialized user data  / public  / distributed        */
    45     VSEG_TYPE_STACK  = 2,          /*! execution user stack   / private / localized          */
    46     VSEG_TYPE_ANON   = 3,          /*! anonymous mmap         / public  / localized          */
    47     VSEG_TYPE_FILE   = 4,          /*! file mmap              / public  / localized          */
    48     VSEG_TYPE_REMOTE = 5,          /*! remote mmap            / public  / localized          */
     43    VSEG_TYPE_CODE   = 0,          /*! executable user code   / private / localized       */
     44    VSEG_TYPE_DATA   = 1,          /*! initialized user data  / public  / distributed     */
     45    VSEG_TYPE_STACK  = 2,          /*! execution user stack   / private / localized       */
     46    VSEG_TYPE_ANON   = 3,          /*! anonymous mmap         / public  / localized       */
     47    VSEG_TYPE_FILE   = 4,          /*! file mmap              / public  / localized       */
     48    VSEG_TYPE_REMOTE = 5,          /*! remote mmap            / public  / localized       */
    4949
    5050    VSEG_TYPE_KDATA  = 10,
     
    5555
    5656
    57 /**********************************************************************************************
     57/*******************************************************************************************
    5858 * These masks define the vseg generic (hardware independent) flags.
    59  *********************************************************************************************/
     59 ******************************************************************************************/
    6060
    61 #define VSEG_USER     0x0001       /*! user accessible                                       */
    62 #define VSEG_WRITE    0x0002       /*! writeable                                             */
    63 #define VSEG_EXEC     0x0004       /*! executable                                            */
    64 #define VSEG_CACHE    0x0008       /*! cachable                                              */
    65 #define VSEG_PRIVATE  0x0010       /*! should not be accessed from another cluster           */
    66 #define VSEG_DISTRIB  0x0020       /*! physically distributed on all clusters                */
     61#define VSEG_USER     0x0001       /*! user accessible                                    */
     62#define VSEG_WRITE    0x0002       /*! writeable                                          */
     63#define VSEG_EXEC     0x0004       /*! executable                                         */
     64#define VSEG_CACHE    0x0008       /*! cachable                                           */
     65#define VSEG_PRIVATE  0x0010       /*! should not be accessed from another cluster        */
     66#define VSEG_DISTRIB  0x0020       /*! physically distributed on all clusters             */
     67#define VSEG_IDENT    0x0040       /*! identity mapping                                   */
    6768
    68 /**********************************************************************************************
     69/*******************************************************************************************
    6970 * This structure defines a virtual segment descriptor.
    7071 * - The VSL contains only local vsegs, but is implemented as an xlist, because it can be
    7172 *   accessed by thread running in a remote cluster.
    7273 * - The zombi list is used by the local MMAP allocator. It is implemented as a local list.
    73  *********************************************************************************************/
     74 ******************************************************************************************/
    7475
    7576typedef struct vseg_s
    7677{
    77         xlist_entry_t     xlist;        /*! all vsegs in same VSL (or same zombi list)           */
    78         list_entry_t      zlist;        /*! all vsegs in same zombi list                         */
    79         struct vmm_s    * vmm;          /*! pointer on associated VM manager                     */
    80     uint32_t          type;         /*! vseg type                                            */
    81         intptr_t          min;          /*! segment min virtual address                          */
    82         intptr_t          max;          /*! segment max virtual address (excluded)               */
    83         vpn_t             vpn_base;     /*! first page of vseg                                   */
    84         vpn_t             vpn_size;     /*! number of pages occupied                             */
    85         uint32_t          flags;        /*! vseg attributes                                      */
    86         xptr_t            mapper_xp;    /*! xptr on remote mapper (for types CODE/DATA/FILE)     */
    87         intptr_t          file_offset;  /*! vseg offset in file (for types CODE/DATA/FILE        */
    88     intptr_t          file_size;    /*! max segment size in mapper (for type CODE/DATA)      */
    89     cxy_t             cxy;          /*! physical mapping (for non distributed vseg)          */
     78        xlist_entry_t     xlist;        /*! all vsegs in same VSL (or same zombi list)        */
     79        list_entry_t      zlist;        /*! all vsegs in same zombi list                      */
     80        struct vmm_s    * vmm;          /*! pointer on associated VM manager                  */
     81    uint32_t          type;         /*! vseg type                                         */
     82        intptr_t          min;          /*! segment min virtual address                       */
     83        intptr_t          max;          /*! segment max virtual address (excluded)            */
     84        vpn_t             vpn_base;     /*! first page of vseg                                */
     85        vpn_t             vpn_size;     /*! number of pages occupied                          */
     86        uint32_t          flags;        /*! vseg attributes                                   */
     87        xptr_t            mapper_xp;    /*! xptr on remote mapper (for types CODE/DATA/FILE)  */
     88        intptr_t          file_offset;  /*! vseg offset in file (for types CODE/DATA/FILE     */
     89    intptr_t          file_size;    /*! max segment size in mapper (for type CODE/DATA)   */
     90    cxy_t             cxy;          /*! physical mapping (for non distributed vseg)       */
    9091}
    9192vseg_t;
    9293
    93 /**********************************************************************************************
     94/*******************************************************************************************
    9495 * This function returns a printable string for the vseg type.
    95  **********************************************************************************************
     96 *******************************************************************************************
    9697 * @ vseg_type  : type of vseg
    9798 * @ return pointer on string
    98  *********************************************************************************************/
     99 ******************************************************************************************/
    99100char * vseg_type_str( uint32_t vseg_type );
    100101
    101 /**********************************************************************************************
     102/*******************************************************************************************
    102103 * This function allocates physical memory for a new vseg descriptor from the local cluster
    103104 * physical memory allocator.
    104  **********************************************************************************************
     105 *******************************************************************************************
    105106 * @ return pointer on allocated vseg descriptor if success / return NULL if failure.
    106  *********************************************************************************************/
     107 ******************************************************************************************/
    107108vseg_t * vseg_alloc();
    108109
    109 /**********************************************************************************************
    110  * This function releases physical memory allocated for a vseg descriptor to the local cluster
    111  * physical memory allocator.
    112  **********************************************************************************************
     110/*******************************************************************************************
     111 * This function releases the physical memory allocated for a vseg descriptor
     112 * to the local cluster physical memory allocator.
     113 *******************************************************************************************
    113114 * @ vseg   : local pointer on released vseg descriptor.
    114  *********************************************************************************************/
     115 ******************************************************************************************/
    115116void vseg_free( vseg_t * vseg );
    116117
    117 /**********************************************************************************************
     118/*******************************************************************************************
    118119 * This function initializes a local vseg descriptor, from the arguments values.
    119120 * It does NOT register the vseg in the local VMM.
    120  **********************************************************************************************
     121 *******************************************************************************************
    121122 * @ vseg      : pointer on the vseg descriptor.
    122123 * @ base      : vseg base address.
     
    126127 * @ type      : vseg type.
    127128 * @ cxy       : target cluster for physical mapping.
    128  *********************************************************************************************/
     129 ******************************************************************************************/
    129130void vseg_init( vseg_t      * vseg,
    130131                    vseg_type_t   type,
     
    138139                cxy_t         cxy );
    139140
    140 /**********************************************************************************************
     141/*******************************************************************************************
    141142 * This function initializes a local vseg descriptor from values contained in a reference
    142143 * remote vseg descriptor. It does NOT register the vseg in the local VMM.
    143  **********************************************************************************************
     144 *******************************************************************************************
    144145 * @ vseg      : pointer on the vseg descriptor.
    145146 * @ ref_xp    : extended pointer on the reference vseg descriptor.
    146  *********************************************************************************************/
     147 ******************************************************************************************/
    147148void vseg_init_from_ref( vseg_t * vseg,
    148149                         xptr_t   ref_xp );
    149150
    150 /**********************************************************************************************
     151/*******************************************************************************************
    151152 * This function adds a vseg descriptor in the set of vsegs controlled by a given VMM,
    152153 * and updates the vmm field in the vseg descriptor.
    153154 * The lock protecting the vsegs list in VMM must be taken by the caller.
    154  **********************************************************************************************
     155 *******************************************************************************************
    155156 * @ vmm       : pointer on the VMM
    156157 * @ vseg      : pointer on the vseg descriptor
    157  *********************************************************************************************/
     158 ******************************************************************************************/
    158159void vseg_attach( struct vmm_s  * vmm,
    159160                  vseg_t        * vseg );
    160161
    161 /**********************************************************************************************
     162/*******************************************************************************************
    162163 * This function removes a vseg descriptor from the set of vsegs controlled by a given VMM,
    163164 * and updates the vmm field in the vseg descriptor. No memory is released.
    164165 * The lock protecting the vsegs list in VMM must be taken by the caller.
    165  **********************************************************************************************
     166 *******************************************************************************************
    166167 * @ vmm       : pointer on the VMM
    167168 * @ vseg      : pointer on the vseg descriptor
    168  *********************************************************************************************/
     169 ******************************************************************************************/
    169170void vseg_detach( struct vmm_s  * vmm,
    170171                  vseg_t        * vseg );
Note: See TracChangeset for help on using the changeset viewer.