Changeset 683 for trunk/kernel/mm/kmem.c
- Timestamp:
- Jan 13, 2021, 12:36:17 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/mm/kmem.c
r672 r683 2 2 * kmem.c - kernel memory allocator implementation. 3 3 * 4 * Authors Alain Greiner (2016,2017,2018,2019,2020)4 * Authors Alain Greiner (2016,2017,2018,2019,2020) 5 5 * 6 6 * Copyright (c) UPMC Sorbonne Universites … … 29 29 #include <thread.h> 30 30 #include <memcpy.h> 31 #include <khm.h>32 31 #include <ppm.h> 33 32 #include <kcm.h> … … 35 34 #include <kmem.h> 36 35 37 ///////////////////////////////////// 38 void * kmem_alloc( kmem_req_t * req ) 39 { 40 uint32_t type; // KMEM_PPM / KMEM_KCM / KMEM_KHM 41 uint32_t flags; // AF_NONE / AF_ZERO / AF_KERNEL 42 uint32_t order; // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes 43 44 type = req->type; 45 order = req->order; 46 flags = req->flags; 47 48 ////////////////////// 49 if( type == KMEM_PPM ) 50 { 51 // allocate the number of requested pages 52 page_t * page_ptr = (void *)ppm_alloc_pages( order ); 53 54 if( page_ptr == NULL ) 55 { 56 printk("\n[ERROR] in %s : PPM failed / order %d / cluster %x\n", 57 __FUNCTION__ , order , local_cxy ); 58 return NULL; 59 } 60 61 xptr_t page_xp = XPTR( local_cxy , page_ptr ); 62 63 // reset page if requested 64 if( flags & AF_ZERO ) page_zero( page_ptr ); 65 66 // get pointer on buffer from the page descriptor 67 void * ptr = GET_PTR( ppm_page2base( page_xp ) ); 68 69 #if DEBUG_KMEM 36 /////////////////////////////////// 37 void * kmem_alloc( uint32_t order, 38 uint32_t flags ) 39 { 40 41 #if DEBUG_KMEM || DEBUG_KMEM_ERROR 70 42 thread_t * this = CURRENT_THREAD; 71 43 uint32_t cycle = (uint32_t)hal_get_cycles(); 72 if( DEBUG_KMEM < cycle ) 73 printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n", 74 __FUNCTION__, this->process->pid, this->trdid, 75 1<<order, ppm_page2ppn(XPTR(local_cxy,ptr)), local_cxy, cycle ); 44 #endif 45 46 if( order >= CONFIG_PPM_PAGE_ORDER ) // use PPM 47 { 48 // allocate memory from PPM 49 page_t * page = (void *)ppm_alloc_pages( order - CONFIG_PPM_PAGE_ORDER ); 50 51 if( page == NULL ) 52 { 53 54 #if DEBUG_KMEM_ERROR 55 if (DEBUG_KMEM_ERROR < cycle) 56 printk("\n[ERROR] in %s : thread[%x,%x] failed for PPM / order %d / cluster %x / cycle %d\n", 57 __FUNCTION__ , this->process->pid , this->trdid , order , local_cxy , cycle ); 58 #endif 59 return NULL; 60 } 61 62 // reset page if requested 63 if( flags & AF_ZERO ) page_zero( page ); 64 65 // get pointer on buffer from the page descriptor 66 xptr_t page_xp = XPTR( local_cxy , page ); 67 void * ptr = GET_PTR( ppm_page2base( page_xp ) ); 68 69 #if DEBUG_KMEM 70 if( (DEBUG_KMEM < cycle) && (DEBUG_KMEM_CXY == local_cxy) && (DEBUG_KMEM_ORDER == order) ) 71 printk("\n[%s] thread[%x,%x] from PPM / order %d / ppn %x / cxy %x / cycle %d\n", 72 __FUNCTION__, this->process->pid, this->trdid, 73 order, ppm_page2ppn(XPTR(local_cxy,ptr)), local_cxy, cycle ); 76 74 #endif 77 75 return ptr; 78 76 } 79 /////////////////////////// 80 else if( type == KMEM_KCM ) 77 else // use KCM 81 78 { 82 79 // allocate memory from KCM … … 85 82 if( ptr == NULL ) 86 83 { 87 printk("\n[ERROR] in %s : KCM failed / order %d / cluster %x\n", 88 __FUNCTION__ , order , local_cxy ); 84 85 #if DEBUG_KMEM_ERROR 86 if (DEBUG_KMEM_ERROR < cycle) 87 printk("\n[ERROR] in %s : thread[%x,%x] failed for KCM / order %d / cluster %x / cycle %d\n", 88 __FUNCTION__ , this->process->pid , this->trdid , order , local_cxy , cycle ); 89 #endif 89 90 return NULL; 90 91 } … … 94 95 95 96 #if DEBUG_KMEM 96 thread_t * this = CURRENT_THREAD; 97 uint32_t cycle = (uint32_t)hal_get_cycles(); 98 if( DEBUG_KMEM < cycle ) 99 printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n", 100 __FUNCTION__, this->process->pid, this->trdid, 101 1<<order, ptr, local_cxy, cycle ); 97 if( (DEBUG_KMEM < cycle) && (DEBUG_KMEM_CXY == local_cxy) && (DEBUG_KMEM_ORDER == order) ) 98 printk("\n[%s] thread [%x,%x] from KCM / order %d / base %x / cxy %x / cycle %d\n", 99 __FUNCTION__, this->process->pid, this->trdid, 100 order, ptr, local_cxy, cycle ); 102 101 #endif 103 102 return ptr; 104 103 } 105 ///////////////////////////106 else if( type == KMEM_KHM )107 {108 // allocate memory from KHM109 void * ptr = khm_alloc( &LOCAL_CLUSTER->khm , order );110 111 if( ptr == NULL )112 {113 printk("\n[ERROR] in %s : KHM failed / order %d / cluster %x\n",114 __FUNCTION__ , order , local_cxy );115 return NULL;116 }117 118 // reset memory if requested119 if( flags & AF_ZERO ) memset( ptr , 0 , order );120 121 #if DEBUG_KMEM122 thread_t * this = CURRENT_THREAD;123 uint32_t cycle = (uint32_t)hal_get_cycles();124 if( DEBUG_KMEM < cycle )125 printk("\n[%s] thread[%x,%x] from KHM / %d bytes / base %x / cxy %x / cycle %d\n",126 __FUNCTION__, this->process->pid, this->trdid,127 order, ptr, local_cxy, cycle );128 #endif129 return ptr;130 }131 else132 {133 printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);134 return NULL;135 }136 104 } // end kmem_alloc() 137 105 138 ////////////////////////////////// 139 void kmem_free( kmem_req_t * req ) 140 { 141 uint32_t type = req->type; 142 143 ////////////////////// 144 if( type == KMEM_PPM ) 145 { 146 page_t * page = GET_PTR( ppm_base2page( XPTR( local_cxy , req->ptr ) ) ); 106 ////////////////////////////// 107 void kmem_free( void * ptr, 108 uint32_t order ) 109 { 110 if( order >= CONFIG_PPM_PAGE_ORDER ) // use PPM 111 { 112 page_t * page = GET_PTR( ppm_base2page( XPTR( local_cxy , ptr ) ) ); 147 113 148 114 ppm_free_pages( page ); 149 115 } 150 /////////////////////////// 151 else if( type == KMEM_KCM ) 116 else // use KCM 152 117 { 153 kcm_free( req->ptr ); 154 } 155 /////////////////////////// 156 else if( type == KMEM_KHM ) 157 { 158 khm_free( req->ptr ); 159 } 160 else 161 { 162 printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); 163 } 118 kcm_free( ptr , order ); 119 } 164 120 } // end kmem_free() 165 121 166 /////////////////////////////////////////// 167 void * kmem_remote_alloc( cxy_t cxy, 168 kmem_req_t * req ) 169 { 170 uint32_t type; // KMEM_PPM / KMEM_KCM / KMEM_KHM 171 uint32_t flags; // AF_ZERO / AF_KERNEL / AF_NONE 172 uint32_t order; // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes 173 174 type = req->type; 175 order = req->order;176 flags = req->flags;177 178 ////////////////////// 179 if( type == KMEM_PPM )180 { 181 // allocate the number of requested pages from remote cluster182 xptr_t page_xp = ppm_remote_alloc_pages( cxy , order );122 123 124 //////////////////////////////////////// 125 void * kmem_remote_alloc( cxy_t cxy, 126 uint32_t order, 127 uint32_t flags ) 128 { 129 130 #if DEBUG_KMEM || DEBUG_KMEM_ERROR 131 thread_t * this = CURRENT_THREAD; 132 uint32_t cycle = (uint32_t)hal_get_cycles(); 133 #endif 134 135 if( order >= CONFIG_PPM_PAGE_ORDER ) // use PPM 136 { 137 // allocate memory from PPM 138 xptr_t page_xp = ppm_remote_alloc_pages( cxy , order - CONFIG_PPM_PAGE_ORDER ); 183 139 184 140 if( page_xp == XPTR_NULL ) 185 141 { 186 printk("\n[ERROR] in %s : failed for PPM / order %d in cluster %x\n", 187 __FUNCTION__ , order , cxy ); 142 143 #if DEBUG_KMEM_ERROR 144 if( DEBUG_KMEM_ERROR < cycle ) 145 printk("\n[ERROR] in %s : thread[%x,%x] failed for PPM / order %d / cluster %x / cycle %d\n", 146 __FUNCTION__ , this->process->pid , this->trdid , order , cxy , cycle ); 147 #endif 188 148 return NULL; 189 149 } … … 192 152 xptr_t base_xp = ppm_page2base( page_xp ); 193 153 194 // reset page if requested 195 if( flags & AF_ZERO ) hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); 196 197 198 #if DEBUG_KMEM_REMOTE 199 thread_t * this = CURRENT_THREAD; 200 uint32_t cycle = (uint32_t)hal_get_cycles(); 201 if( DEBUG_KMEM_REMOTE < cycle ) 202 printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n", 203 __FUNCTION__, this->process->pid, this->trdid, 204 1<<order, ppm_page2ppn( page_xp ), cxy, cycle ); 154 // reset memory if requested 155 if( flags & AF_ZERO ) hal_remote_memset( base_xp , 0 , 1<<order ); 156 157 #if DEBUG_KMEM 158 if( (DEBUG_KMEM < cycle) && (DEBUG_KMEM_CXY == local_cxy) && (DEBUG_KMEM_ORDER == order) ) 159 printk("\n[%s] thread[%x,%x] from PPM / order %d / ppn %x / cxy %x / cycle %d\n", 160 __FUNCTION__, this->process->pid, this->trdid, 161 order, ppm_page2ppn( page_xp ), cxy, cycle ); 205 162 #endif 206 163 return GET_PTR( base_xp ); 207 164 } 208 /////////////////////////// 209 else if( type == KMEM_KCM ) 165 else // use KCM 210 166 { 211 167 // allocate memory from KCM … … 214 170 if( ptr == NULL ) 215 171 { 216 printk("\n[ERROR] in %s : failed for KCM / order %d in cluster %x\n", 217 __FUNCTION__ , order , cxy ); 172 173 #if DEBUG_KMEM_ERROR 174 if( DEBUG_KMEM_ERROR < cycle ) 175 printk("\n[ERROR] in %s : thread[%x,%x] failed for KCM / order %d / cluster %x / cycle %d\n", 176 __FUNCTION__ , this->process->pid , this->trdid , order , cxy , cycle ); 177 #endif 218 178 return NULL; 219 179 } … … 222 182 if( flags & AF_ZERO ) hal_remote_memset( XPTR( cxy , ptr ) , 0 , 1<<order ); 223 183 224 #if DEBUG_KMEM_REMOTE 225 thread_t * this = CURRENT_THREAD; 226 uint32_t cycle = (uint32_t)hal_get_cycles(); 227 if( DEBUG_KMEM_REMOTE < cycle ) 228 printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n", 229 __FUNCTION__, this->process->pid, this->trdid, 230 1<<order, ptr, cxy, cycle ); 184 #if DEBUG_KMEM 185 if( (DEBUG_KMEM < cycle) && (DEBUG_KMEM_CXY == local_cxy) && (DEBUG_KMEM_ORDER == order) ) 186 printk("\n[%s] thread [%x,%x] from KCM / order %d / base %x / cxy %x / cycle %d\n", 187 __FUNCTION__, this->process->pid, this->trdid, 188 order, ptr, cxy, cycle ); 231 189 #endif 232 190 return ptr; 233 191 } 234 ///////////////////////////235 else if( type == KMEM_KHM )236 {237 printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ );238 return NULL;239 }240 else241 {242 printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);243 return NULL;244 }245 192 } // kmem_remote_malloc() 246 193 247 //////////////////////////////////////// 248 void kmem_remote_free( cxy_t cxy, 249 kmem_req_t * req ) 250 { 251 uint32_t type = req->type; 252 253 ////////////////////// 254 if( type == KMEM_PPM ) 255 { 256 page_t * page = GET_PTR( ppm_base2page( XPTR( cxy , req->ptr ) ) ); 194 ///////////////////////////////////// 195 void kmem_remote_free( cxy_t cxy, 196 void * ptr, 197 uint32_t order ) 198 { 199 if( order >= CONFIG_PPM_PAGE_ORDER ) // use PPM 200 { 201 page_t * page = GET_PTR( ppm_base2page( XPTR( cxy , ptr ) ) ); 257 202 258 203 ppm_remote_free_pages( cxy , page ); 259 204 } 260 /////////////////////////// 261 else if( type == KMEM_KCM ) 205 else // use KCM 262 206 { 263 kcm_remote_free( cxy , req->ptr ); 264 } 265 /////////////////////////// 266 else if( type == KMEM_KHM ) 267 { 268 printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ ); 269 } 270 else 271 { 272 printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); 273 } 207 kcm_remote_free( cxy , ptr , order ); 208 } 274 209 } // end kmem_remote_free() 275 210
Note: See TracChangeset
for help on using the changeset viewer.