Changeset 7 for trunk/kernel/mm/kmem.c


Ignore:
Timestamp:
Apr 26, 2017, 2:15:50 PM (7 years ago)
Author:
alain
Message:

Various bugs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/mm/kmem.c

    r1 r7  
    3737#include <thread.h>
    3838#include <process.h>
    39 #include <device.h>
     39#include <chdev.h>
    4040#include <mapper.h>
    4141#include <vfs.h>
     
    4949#include <kmem.h>
    5050
    51 /////////////////////////////////////////////////////////////////////////////////////////////
    52 // This global array is indexed by the Kernel Memory Object Type (defined in kmem.h)
    53 // It contains the size of fixed size objects type dynamically allocated by the KCMs.
    54 // This array should be consistent with the enum defined kmem.h.
    55 /////////////////////////////////////////////////////////////////////////////////////////////
    56 
    57 uint32_t  kmem_size_tbl[KMEM_TYPES_NR] =
    58 {
    59     0,                        // 0  KMEM_PAGE is not a fixed size object
    60     0,                        // 1  KMEM_GENERIC   
    61     sizeof( kcm_t ),          // 2  KMEM_KCM
    62     sizeof( vseg_t ),         // 3  KMEM_VSEG
    63     sizeof( device_t ),       // 4  KMEM_DEVICE
    64     sizeof( mapper_t ),       // 5  KMEM_MAPPER
    65     sizeof( process_t ),      // 6  KMEM_PROCESS
    66     0,                        // 7
    67     0,                        // 8 
    68     0,                        // 9 
    69 
    70     sizeof( fatfs_inode_t ),  // 10 KMEM_FATFS_INODE
    71     sizeof( fatfs_ctx_t ),    // 11 KMEM_FATFS_CTX
    72     sizeof( ramfs_inode_t ),  // 12 KMEM_RAMFS_INODE
    73     sizeof( ramfs_ctx_t ),    // 13 KMEM_RAMFS_CTX
    74     sizeof( vfs_ctx_t ),      // 14 KMEM_VFS_CTX
    75     sizeof( vfs_inode_t ),    // 15 KMEM_VFS_INODE
    76     sizeof( vfs_dentry_t ),   // 16 KMEM_VFS_DENTRY
    77     sizeof( vfs_file_t ),     // 17 KMEM_VFS_FILE
    78     sizeof( remote_sem_t ),   // 18 KMEM_SEM
    79     0,                        // 19
    80 
    81     64,                       // 20 KMEM_64_BYTES
    82     128,                      // 21 KMEM_128_BYTES
    83     256,                      // 22 KMEM_256_BYTES
    84     512,                      // 23 KMEM_512_BYTES
    85     1024,                     // 24 KMEM_1024_BYTES
    86     2048,                     // 25 KMEM_2048_BYTES
    87 };
     51///////////////////////////
     52void kmem_print_kcm_table()
     53{
     54    uint32_t    index;
     55    kcm_t     * kcm;
     56    cluster_t * cluster = LOCAL_CLUSTER;
     57
     58    printk("\n    *** KCM Pointers Table ***\n");
     59
     60    for( index = 0 ; index < KMEM_TYPES_NR ; index++ )
     61    {
     62        kcm = cluster->kcm_tbl[index];
     63        if( kcm != NULL )
     64        {
     65            if( index == kcm->type )
     66            {
     67                printk("     - KCM[%s] (at address %x) is OK\n",
     68                       kmem_type_str( index ) , (intptr_t)kcm );
     69            }
     70            else
     71            {
     72                printk("     - KCM[%s] (at address %x) is KO : has type %s\n",
     73                       kmem_type_str( index ) , (intptr_t)kcm , kmem_type_str( kcm->type ) );
     74            }           
     75        }
     76    }
     77}  // end kmem_print_kcm_table()
     78
     79/////////////////////////////////////////
     80uint32_t  kmem_type_size( uint32_t type )
     81{
     82    if     ( type == KMEM_PAGE )          return CONFIG_PPM_PAGE_SIZE;
     83    else if( type == KMEM_GENERIC )       return 0;
     84    else if( type == KMEM_KCM )           return sizeof( kcm_t );
     85    else if( type == KMEM_VSEG )          return sizeof( vseg_t );
     86    else if( type == KMEM_DEVICE )        return sizeof( chdev_t );
     87    else if( type == KMEM_MAPPER )        return sizeof( mapper_t );
     88    else if( type == KMEM_PROCESS )       return sizeof( process_t );
     89    else if( type == KMEM_CPU_CTX )       return sizeof( hal_cpu_context_t );
     90    else if( type == KMEM_FPU_CTX )       return sizeof( hal_fpu_context_t );
     91
     92    else if( type == KMEM_FATFS_INODE )   return sizeof( fatfs_inode_t );
     93    else if( type == KMEM_FATFS_CTX )     return sizeof( fatfs_ctx_t );
     94    else if( type == KMEM_RAMFS_INODE )   return sizeof( ramfs_inode_t );
     95    else if( type == KMEM_RAMFS_CTX )     return sizeof( ramfs_ctx_t );
     96    else if( type == KMEM_VFS_CTX )       return sizeof( vfs_ctx_t );
     97    else if( type == KMEM_VFS_INODE )     return sizeof( vfs_inode_t );
     98    else if( type == KMEM_VFS_DENTRY )    return sizeof( vfs_dentry_t );
     99    else if( type == KMEM_VFS_FILE )      return sizeof( vfs_file_t );
     100    else if( type == KMEM_SEM )           return sizeof( remote_sem_t );
     101    else                                  return 0;
     102}
     103
     104/////////////////////////////////////
     105char * kmem_type_str( uint32_t type )
     106{
     107    if     ( type == KMEM_PAGE )          return "KMEM_PAGE";
     108    else if( type == KMEM_GENERIC )       return "KMEM_GENERIC";
     109    else if( type == KMEM_KCM )           return "KMEM_KCM";
     110    else if( type == KMEM_VSEG )          return "KMEM_VSEG";
     111    else if( type == KMEM_DEVICE )        return "KMEM_DEVICE";
     112    else if( type == KMEM_MAPPER )        return "KMEM_MAPPER";
     113    else if( type == KMEM_PROCESS )       return "KMEM_PROCESS";
     114    else if( type == KMEM_CPU_CTX )       return "KMEM_CPU_CTX";
     115    else if( type == KMEM_FPU_CTX )       return "KMEM_FPU_CTX";
     116
     117    else if( type == KMEM_FATFS_INODE )   return "KMEM_FATFS_INODE";
     118    else if( type == KMEM_FATFS_CTX )     return "KMEM_FATFS_CTX";
     119    else if( type == KMEM_RAMFS_INODE )   return "KMEM_RAMFS_INODE";
     120    else if( type == KMEM_RAMFS_CTX )     return "KMEM_RAMFS_CTX";
     121    else if( type == KMEM_VFS_CTX )       return "KMEM_VFS_CTX";
     122    else if( type == KMEM_VFS_INODE )     return "KMEM_VFS_INODE";
     123    else if( type == KMEM_VFS_DENTRY )    return "KMEM_VFS_DENTRY";
     124    else if( type == KMEM_VFS_FILE )      return "KMEM_VFS_FILE";
     125    else if( type == KMEM_SEM )           return "KMEM_SEM";
     126    else                                  return "undefined";
     127}
    88128
    89129/////////////////////////////////////////////////////////////////////////////////////////////
     
    91131// It uses the KCM allocator embedded in cluster manager, initialized by cluster_init().
    92132/////////////////////////////////////////////////////////////////////////////////////////////
    93 static error_t kmem_get_kcm( uint32_t type )
     133static error_t kmem_create_kcm( uint32_t type )
    94134{
    95135        kcm_t    * kcm;
    96         error_t    error;
    97 
    98     // check kmem object type
    99     if( (type < 2) || (type >= KMEM_TYPES_NR) )
    100     {
    101         printk("\n[PANIC] in %s illegal request type\n", __FUNCTION__ );
    102         hal_core_sleep();
    103     }
     136
     137    assert( ((type > 1) && (type < KMEM_TYPES_NR) ) , __FUNCTION__ , "illegal KCM type" );
     138
     139        kmem_dmsg("\n[INFO] %s : enters / KCM type %s missing in cluster %x\n",
     140                      __FUNCTION__ , kmem_type_str( type ) , local_cxy );
    104141
    105142    cluster_t * cluster = LOCAL_CLUSTER;
    106143
    107144    // allocates memory for the requested KCM allocator
     145    // from the KCM allocator embedded in cluster descriptor
    108146        kcm = kcm_alloc( &cluster->kcm );
     147
    109148        if( kcm == NULL )
    110149    {
     
    115154
    116155    // initializes the new KCM allocator
    117         error = kcm_init( kcm , type );
    118 
    119         if( error )
    120         {
    121                 printk("\n[ERROR] in %s : failed to init KCM type %d\n",
    122                __FUNCTION__ , type , local_cxy );
    123                 return error;
    124         }
    125 
     156        kcm_init( kcm , type );
     157
     158    // register it if the KCM pointers Table
    126159        cluster->kcm_tbl[type] = kcm;
     160
    127161        hal_wbflush();
    128162
     163        kmem_dmsg("\n[INFO] %s : exit / KCM type %s created in cluster %x\n",
     164                      __FUNCTION__ , kmem_type_str( type ) , local_cxy );
     165
    129166        return 0;
    130 }
     167
     168}  // end kmem_create_kcm()
     169
    131170 
    132171
     
    137176
    138177        uint32_t    type;
    139         uint32_t    size;    // ln( pages ) if PPM / bytes if KHM or BKM / unused if KCM
    140178        uint32_t    flags;
    141         void      * ptr;     // local pointer on allocated memory buffer
     179        uint32_t    size;    // ln( pages ) if PPM / bytes if KHM / unused if KCM
     180        void      * ptr;     // memory buffer if KHM or KCM / page descriptor if PPM
     181
    142182
    143183        type  = req->type;
     
    145185        flags = req->flags;
    146186 
    147         kmem_dmsg("\n[INFO] %s in cluster %x : type %d, size %d, flags %x at cycle %d \n",
    148                       __FUNCTION__, local_cxy , type, size, flags , hal_time_stamp() );
    149 
    150         if( type >= KMEM_TYPES_NR )
    151     {
    152         printk("\n[PANIC] in %s illegal request type\n", __FUNCTION__ );
    153         hal_core_sleep();
    154     }
     187        assert( (type < KMEM_TYPES_NR) , __FUNCTION__ , "illegal KMEM request type" );
    155188 
     189        kmem_dmsg("\n[INFO] %s : enters in cluster %x for type %s / size %d\n",
     190                      __FUNCTION__ , local_cxy , kmem_type_str( type ) , size );
     191
    156192    // analyse request type
    157193        if( type ==  KMEM_PAGE )                       // PPM allocator
    158194    {       
    159195        // allocate the number of requested pages
    160                 ptr = (void*)ppm_alloc_pages( size );
     196                ptr = (void *)ppm_alloc_pages( size );
    161197
    162198        // reset page if required
    163                 if( flags & AF_ZERO ) page_zero( (page_t*)ptr );
     199                if( flags & AF_ZERO ) page_zero( (page_t *)ptr );
     200   
     201        kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / page = %x / base = %x\n",
     202                  __FUNCTION__, local_cxy , kmem_type_str( type ) ,
     203                  (intptr_t)ptr , (intptr_t)ppm_page2base( ptr ) );
    164204        }
    165205    else if( type == KMEM_GENERIC )                // KHM allocator
     
    170210        // reset memory if requested
    171211                if( flags & AF_ZERO ) memset( ptr , 0 , size );
     212
     213        kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / base = %x\n",
     214                  __FUNCTION__, local_cxy , kmem_type_str( type ) , (intptr_t)ptr );
    172215        }
    173216    else                                           // KCM allocator
    174217    {
    175         uint32_t error = 0;
    176 
    177218        // initialize the KCM allocator if not already done
    178219            if( cluster->kcm_tbl[type] == NULL )
    179220            {
    180221            spinlock_lock( &cluster->kcm_lock );
    181                         error = kmem_get_kcm( type );
     222                        error_t error = kmem_create_kcm( type );
    182223            spinlock_unlock( &cluster->kcm_lock );
     224            if ( error ) return NULL;
    183225            }
    184226
    185         // allocate memory from KCM if success
    186         if( error ) ptr = NULL;
    187         else        ptr = kcm_alloc( cluster->kcm_tbl[type] );
     227        // allocate memory from KCM
     228        ptr = kcm_alloc( cluster->kcm_tbl[type] );
     229
     230        // reset memory if requested
     231                if( flags & AF_ZERO ) memset( ptr , 0 , kmem_type_size( type ) );
     232
     233        kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / base = %x\n",
     234                  __FUNCTION__, local_cxy , kmem_type_str( type ) , (intptr_t)ptr );
    188235        }
    189236
    190237    if( ptr == NULL )
    191238    {
    192             printk("\n[ERROR] in %s : failed for type %d, size %d, flags %x in cluster %x\n",
    193                __FUNCTION__ , type , size , flags , local_cxy );
     239            printk("\n[ERROR] in %s : failed for type %d / size %d in cluster %x\n",
     240               __FUNCTION__ , type , size , local_cxy );
    194241 
    195242            return NULL;
    196243    }
    197244
    198         kmem_dmsg("\n[INFO] %s got ptr = %x in cluster %x at cycle %d\n",
    199                   __FUNCTION__, (intptr_t)ptr , local_cxy , hal_time_stamp() );
    200            
    201245        return ptr;
    202246
Note: See TracChangeset for help on using the changeset viewer.