[1] | 1 | /* |
---|
| 2 | * kmem.c - kernel memory allocator implementation. |
---|
| 3 | * |
---|
| 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Mohamed Lamine Karaoui (2015) |
---|
| 6 | * Alain Greiner (2016) |
---|
| 7 | * |
---|
| 8 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 9 | * |
---|
| 10 | * This file is part of ALMOS-MKH. |
---|
| 11 | * |
---|
| 12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 13 | * under the terms of the GNU General Public License as published by |
---|
| 14 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 15 | * |
---|
| 16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 19 | * General Public License for more details. |
---|
| 20 | * |
---|
| 21 | * You should have received a copy of the GNU General Public License |
---|
| 22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
[14] | 26 | #include <kernel_config.h> |
---|
[1] | 27 | #include <hal_types.h> |
---|
| 28 | #include <hal_special.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <spinlock.h> |
---|
| 31 | #include <readlock.h> |
---|
| 32 | #include <memcpy.h> |
---|
| 33 | #include <khm.h> |
---|
| 34 | #include <ppm.h> |
---|
| 35 | #include <page.h> |
---|
| 36 | #include <cluster.h> |
---|
| 37 | #include <thread.h> |
---|
| 38 | #include <process.h> |
---|
[7] | 39 | #include <chdev.h> |
---|
[1] | 40 | #include <mapper.h> |
---|
| 41 | #include <vfs.h> |
---|
| 42 | #include <fatfs.h> |
---|
| 43 | #include <ramfs.h> |
---|
| 44 | #include <remote_sem.h> |
---|
| 45 | #include <remote_barrier.h> |
---|
[23] | 46 | #include <remote_mutex.h> |
---|
| 47 | #include <remote_condvar.h> |
---|
[1] | 48 | #include <mapper.h> |
---|
| 49 | #include <grdxt.h> |
---|
| 50 | #include <vseg.h> |
---|
| 51 | #include <kmem.h> |
---|
| 52 | |
---|
[7] | 53 | /////////////////////////// |
---|
| 54 | void kmem_print_kcm_table() |
---|
| 55 | { |
---|
| 56 | uint32_t index; |
---|
| 57 | kcm_t * kcm; |
---|
| 58 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
[1] | 59 | |
---|
[7] | 60 | printk("\n *** KCM Pointers Table ***\n"); |
---|
| 61 | |
---|
| 62 | for( index = 0 ; index < KMEM_TYPES_NR ; index++ ) |
---|
| 63 | { |
---|
| 64 | kcm = cluster->kcm_tbl[index]; |
---|
| 65 | if( kcm != NULL ) |
---|
| 66 | { |
---|
[18] | 67 | if( index == kcm->type ) |
---|
[7] | 68 | { |
---|
| 69 | printk(" - KCM[%s] (at address %x) is OK\n", |
---|
| 70 | kmem_type_str( index ) , (intptr_t)kcm ); |
---|
| 71 | } |
---|
[18] | 72 | else |
---|
[7] | 73 | { |
---|
| 74 | printk(" - KCM[%s] (at address %x) is KO : has type %s\n", |
---|
| 75 | kmem_type_str( index ) , (intptr_t)kcm , kmem_type_str( kcm->type ) ); |
---|
[18] | 76 | } |
---|
[7] | 77 | } |
---|
| 78 | } |
---|
| 79 | } // end kmem_print_kcm_table() |
---|
| 80 | |
---|
| 81 | ///////////////////////////////////////// |
---|
| 82 | uint32_t kmem_type_size( uint32_t type ) |
---|
[1] | 83 | { |
---|
[7] | 84 | if ( type == KMEM_PAGE ) return CONFIG_PPM_PAGE_SIZE; |
---|
| 85 | else if( type == KMEM_GENERIC ) return 0; |
---|
| 86 | else if( type == KMEM_KCM ) return sizeof( kcm_t ); |
---|
| 87 | else if( type == KMEM_VSEG ) return sizeof( vseg_t ); |
---|
| 88 | else if( type == KMEM_DEVICE ) return sizeof( chdev_t ); |
---|
| 89 | else if( type == KMEM_MAPPER ) return sizeof( mapper_t ); |
---|
| 90 | else if( type == KMEM_PROCESS ) return sizeof( process_t ); |
---|
| 91 | else if( type == KMEM_CPU_CTX ) return sizeof( hal_cpu_context_t ); |
---|
| 92 | else if( type == KMEM_FPU_CTX ) return sizeof( hal_fpu_context_t ); |
---|
[23] | 93 | else if( type == KMEM_BARRIER ) return sizeof( remote_barrier_t ); |
---|
[1] | 94 | |
---|
[7] | 95 | else if( type == KMEM_FATFS_INODE ) return sizeof( fatfs_inode_t ); |
---|
| 96 | else if( type == KMEM_FATFS_CTX ) return sizeof( fatfs_ctx_t ); |
---|
[23] | 97 | else if( type == KMEM_DEVFS_INODE ) return sizeof( devfs_inode_t ); |
---|
| 98 | else if( type == KMEM_MUTEX ) return sizeof( remote_mutex_t ); |
---|
[7] | 99 | else if( type == KMEM_VFS_CTX ) return sizeof( vfs_ctx_t ); |
---|
| 100 | else if( type == KMEM_VFS_INODE ) return sizeof( vfs_inode_t ); |
---|
| 101 | else if( type == KMEM_VFS_DENTRY ) return sizeof( vfs_dentry_t ); |
---|
| 102 | else if( type == KMEM_VFS_FILE ) return sizeof( vfs_file_t ); |
---|
| 103 | else if( type == KMEM_SEM ) return sizeof( remote_sem_t ); |
---|
[23] | 104 | else if( type == KMEM_CONDVAR ) return sizeof( remote_condvar_t ); |
---|
[7] | 105 | else return 0; |
---|
[18] | 106 | } |
---|
[1] | 107 | |
---|
[7] | 108 | ///////////////////////////////////// |
---|
| 109 | char * kmem_type_str( uint32_t type ) |
---|
| 110 | { |
---|
| 111 | if ( type == KMEM_PAGE ) return "KMEM_PAGE"; |
---|
| 112 | else if( type == KMEM_GENERIC ) return "KMEM_GENERIC"; |
---|
| 113 | else if( type == KMEM_KCM ) return "KMEM_KCM"; |
---|
| 114 | else if( type == KMEM_VSEG ) return "KMEM_VSEG"; |
---|
| 115 | else if( type == KMEM_DEVICE ) return "KMEM_DEVICE"; |
---|
| 116 | else if( type == KMEM_MAPPER ) return "KMEM_MAPPER"; |
---|
| 117 | else if( type == KMEM_PROCESS ) return "KMEM_PROCESS"; |
---|
| 118 | else if( type == KMEM_CPU_CTX ) return "KMEM_CPU_CTX"; |
---|
| 119 | else if( type == KMEM_FPU_CTX ) return "KMEM_FPU_CTX"; |
---|
[23] | 120 | else if( type == KMEM_BARRIER ) return "KMEM_BARRIER"; |
---|
[1] | 121 | |
---|
[7] | 122 | else if( type == KMEM_FATFS_INODE ) return "KMEM_FATFS_INODE"; |
---|
| 123 | else if( type == KMEM_FATFS_CTX ) return "KMEM_FATFS_CTX"; |
---|
[23] | 124 | else if( type == KMEM_DEVFS_INODE ) return "KMEM_DEVFS_INODE"; |
---|
| 125 | else if( type == KMEM_MUTEX ) return "KMEM_MUTEX"; |
---|
[7] | 126 | else if( type == KMEM_VFS_CTX ) return "KMEM_VFS_CTX"; |
---|
| 127 | else if( type == KMEM_VFS_INODE ) return "KMEM_VFS_INODE"; |
---|
[18] | 128 | else if( type == KMEM_VFS_DENTRY ) return "KMEM_VFS_DENTRY"; |
---|
[7] | 129 | else if( type == KMEM_VFS_FILE ) return "KMEM_VFS_FILE"; |
---|
| 130 | else if( type == KMEM_SEM ) return "KMEM_SEM"; |
---|
[23] | 131 | else if( type == KMEM_SEM ) return "KMEM_CONDVAR"; |
---|
[7] | 132 | else return "undefined"; |
---|
| 133 | } |
---|
| 134 | |
---|
[1] | 135 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 136 | // This static function dynamically allocates and initializes a specific KCM allocator. |
---|
| 137 | // It uses the KCM allocator embedded in cluster manager, initialized by cluster_init(). |
---|
| 138 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
[7] | 139 | static error_t kmem_create_kcm( uint32_t type ) |
---|
[1] | 140 | { |
---|
| 141 | kcm_t * kcm; |
---|
| 142 | |
---|
[7] | 143 | assert( ((type > 1) && (type < KMEM_TYPES_NR) ) , __FUNCTION__ , "illegal KCM type" ); |
---|
[1] | 144 | |
---|
[7] | 145 | kmem_dmsg("\n[INFO] %s : enters / KCM type %s missing in cluster %x\n", |
---|
| 146 | __FUNCTION__ , kmem_type_str( type ) , local_cxy ); |
---|
| 147 | |
---|
[1] | 148 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 149 | |
---|
| 150 | // allocates memory for the requested KCM allocator |
---|
[7] | 151 | // from the KCM allocator embedded in cluster descriptor |
---|
[1] | 152 | kcm = kcm_alloc( &cluster->kcm ); |
---|
[7] | 153 | |
---|
[1] | 154 | if( kcm == NULL ) |
---|
| 155 | { |
---|
| 156 | printk("\n[ERROR] in %s : failed to create KCM type %d in cluster %x\n", |
---|
| 157 | __FUNCTION__ , type , local_cxy ); |
---|
| 158 | return ENOMEM; |
---|
| 159 | } |
---|
| 160 | |
---|
[18] | 161 | // initializes the new KCM allocator |
---|
[7] | 162 | kcm_init( kcm , type ); |
---|
[1] | 163 | |
---|
[7] | 164 | // register it if the KCM pointers Table |
---|
| 165 | cluster->kcm_tbl[type] = kcm; |
---|
[1] | 166 | |
---|
| 167 | hal_wbflush(); |
---|
| 168 | |
---|
[7] | 169 | kmem_dmsg("\n[INFO] %s : exit / KCM type %s created in cluster %x\n", |
---|
| 170 | __FUNCTION__ , kmem_type_str( type ) , local_cxy ); |
---|
| 171 | |
---|
[1] | 172 | return 0; |
---|
[7] | 173 | |
---|
| 174 | } // end kmem_create_kcm() |
---|
| 175 | |
---|
[1] | 176 | |
---|
[18] | 177 | |
---|
[1] | 178 | ///////////////////////////////////// |
---|
| 179 | void * kmem_alloc( kmem_req_t * req ) |
---|
| 180 | { |
---|
| 181 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 182 | |
---|
| 183 | uint32_t type; |
---|
| 184 | uint32_t flags; |
---|
[7] | 185 | uint32_t size; // ln( pages ) if PPM / bytes if KHM / unused if KCM |
---|
| 186 | void * ptr; // memory buffer if KHM or KCM / page descriptor if PPM |
---|
[1] | 187 | |
---|
[7] | 188 | |
---|
[1] | 189 | type = req->type; |
---|
| 190 | size = req->size; |
---|
| 191 | flags = req->flags; |
---|
[18] | 192 | |
---|
[7] | 193 | assert( (type < KMEM_TYPES_NR) , __FUNCTION__ , "illegal KMEM request type" ); |
---|
[18] | 194 | |
---|
[7] | 195 | kmem_dmsg("\n[INFO] %s : enters in cluster %x for type %s / size %d\n", |
---|
| 196 | __FUNCTION__ , local_cxy , kmem_type_str( type ) , size ); |
---|
[1] | 197 | |
---|
| 198 | // analyse request type |
---|
| 199 | if( type == KMEM_PAGE ) // PPM allocator |
---|
[18] | 200 | { |
---|
[1] | 201 | // allocate the number of requested pages |
---|
[7] | 202 | ptr = (void *)ppm_alloc_pages( size ); |
---|
[1] | 203 | |
---|
| 204 | // reset page if required |
---|
[7] | 205 | if( flags & AF_ZERO ) page_zero( (page_t *)ptr ); |
---|
[18] | 206 | |
---|
[7] | 207 | kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / page = %x / base = %x\n", |
---|
[18] | 208 | __FUNCTION__, local_cxy , kmem_type_str( type ) , |
---|
[7] | 209 | (intptr_t)ptr , (intptr_t)ppm_page2base( ptr ) ); |
---|
[1] | 210 | } |
---|
| 211 | else if( type == KMEM_GENERIC ) // KHM allocator |
---|
| 212 | { |
---|
| 213 | // allocate memory from KHM |
---|
| 214 | ptr = khm_alloc( &cluster->khm , size ); |
---|
| 215 | |
---|
| 216 | // reset memory if requested |
---|
| 217 | if( flags & AF_ZERO ) memset( ptr , 0 , size ); |
---|
[7] | 218 | |
---|
| 219 | kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / base = %x\n", |
---|
| 220 | __FUNCTION__, local_cxy , kmem_type_str( type ) , (intptr_t)ptr ); |
---|
[1] | 221 | } |
---|
| 222 | else // KCM allocator |
---|
| 223 | { |
---|
| 224 | // initialize the KCM allocator if not already done |
---|
| 225 | if( cluster->kcm_tbl[type] == NULL ) |
---|
[18] | 226 | { |
---|
[1] | 227 | spinlock_lock( &cluster->kcm_lock ); |
---|
[7] | 228 | error_t error = kmem_create_kcm( type ); |
---|
[1] | 229 | spinlock_unlock( &cluster->kcm_lock ); |
---|
[7] | 230 | if ( error ) return NULL; |
---|
[1] | 231 | } |
---|
| 232 | |
---|
[18] | 233 | // allocate memory from KCM |
---|
[7] | 234 | ptr = kcm_alloc( cluster->kcm_tbl[type] ); |
---|
| 235 | |
---|
| 236 | // reset memory if requested |
---|
| 237 | if( flags & AF_ZERO ) memset( ptr , 0 , kmem_type_size( type ) ); |
---|
| 238 | |
---|
| 239 | kmem_dmsg("\n[INFO] %s : exit in cluster %x for type %s / base = %x\n", |
---|
| 240 | __FUNCTION__, local_cxy , kmem_type_str( type ) , (intptr_t)ptr ); |
---|
[1] | 241 | } |
---|
| 242 | |
---|
| 243 | if( ptr == NULL ) |
---|
| 244 | { |
---|
[18] | 245 | printk("\n[ERROR] in %s : failed for type %d / size %d in cluster %x\n", |
---|
[7] | 246 | __FUNCTION__ , type , size , local_cxy ); |
---|
[18] | 247 | |
---|
[1] | 248 | return NULL; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | return ptr; |
---|
| 252 | |
---|
| 253 | } // end kmem_alloc() |
---|
| 254 | |
---|
| 255 | ////////////////////////////////// |
---|
| 256 | void kmem_free( kmem_req_t * req ) |
---|
| 257 | { |
---|
| 258 | if( req->type >= KMEM_TYPES_NR ) |
---|
| 259 | { |
---|
| 260 | printk("\n[PANIC] in %s : illegal request type\n", __FUNCTION__ ); |
---|
| 261 | hal_core_sleep(); |
---|
| 262 | } |
---|
[18] | 263 | |
---|
[1] | 264 | switch(req->type) |
---|
| 265 | { |
---|
| 266 | case KMEM_PAGE: |
---|
| 267 | ppm_free_pages( (page_t*)req->ptr ); |
---|
| 268 | return; |
---|
| 269 | |
---|
| 270 | case KMEM_GENERIC: |
---|
| 271 | khm_free( req->ptr ); |
---|
| 272 | return; |
---|
| 273 | |
---|
| 274 | default: |
---|
| 275 | kcm_free( req->ptr ); |
---|
| 276 | return; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|