[1] | 1 | /* |
---|
| 2 | * kmem.c - kernel memory allocator implementation. |
---|
| 3 | * |
---|
[657] | 4 | * Authors Alain Greiner (2016,2017,2018,2019,2020) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_special.h> |
---|
| 27 | #include <printk.h> |
---|
[635] | 28 | #include <cluster.h> |
---|
[1] | 29 | #include <memcpy.h> |
---|
| 30 | #include <khm.h> |
---|
| 31 | #include <ppm.h> |
---|
[635] | 32 | #include <kcm.h> |
---|
[1] | 33 | #include <page.h> |
---|
| 34 | #include <kmem.h> |
---|
| 35 | |
---|
[635] | 36 | ///////////////////////////////////// |
---|
| 37 | void * kmem_alloc( kmem_req_t * req ) |
---|
[7] | 38 | { |
---|
[635] | 39 | uint32_t type; // KMEM_PPM / KMEM_KCM / KMEM_KHM |
---|
| 40 | uint32_t flags; // AF_NONE / AF_ZERO / AF_KERNEL |
---|
| 41 | uint32_t order; // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes |
---|
[1] | 42 | |
---|
[635] | 43 | type = req->type; |
---|
| 44 | order = req->order; |
---|
| 45 | flags = req->flags; |
---|
[7] | 46 | |
---|
[656] | 47 | ////////////////////// |
---|
[635] | 48 | if( type == KMEM_PPM ) |
---|
[159] | 49 | { |
---|
[635] | 50 | // allocate the number of requested pages |
---|
| 51 | page_t * page_ptr = (void *)ppm_alloc_pages( order ); |
---|
| 52 | |
---|
| 53 | if( page_ptr == NULL ) |
---|
[159] | 54 | { |
---|
[635] | 55 | printk("\n[ERROR] in %s : PPM failed / order %d / cluster %x\n", |
---|
| 56 | __FUNCTION__ , order , local_cxy ); |
---|
| 57 | return NULL; |
---|
[159] | 58 | } |
---|
[7] | 59 | |
---|
[635] | 60 | xptr_t page_xp = XPTR( local_cxy , page_ptr ); |
---|
[1] | 61 | |
---|
[635] | 62 | // reset page if requested |
---|
| 63 | if( flags & AF_ZERO ) page_zero( page_ptr ); |
---|
[619] | 64 | |
---|
[635] | 65 | // get pointer on buffer from the page descriptor |
---|
| 66 | void * ptr = GET_PTR( ppm_page2base( page_xp ) ); |
---|
[50] | 67 | |
---|
[438] | 68 | #if DEBUG_KMEM |
---|
[635] | 69 | thread_t * this = CURRENT_THREAD; |
---|
| 70 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 71 | if( DEBUG_KMEM < cycle ) |
---|
[635] | 72 | printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n", |
---|
| 73 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 74 | 1<<order, ppm_page2ppn(XPTR(local_cxy,ptr)), local_cxy, cycle ); |
---|
[435] | 75 | #endif |
---|
[635] | 76 | return ptr; |
---|
| 77 | } |
---|
[656] | 78 | /////////////////////////// |
---|
[635] | 79 | else if( type == KMEM_KCM ) |
---|
[159] | 80 | { |
---|
[635] | 81 | // allocate memory from KCM |
---|
| 82 | void * ptr = kcm_alloc( order ); |
---|
[1] | 83 | |
---|
[180] | 84 | if( ptr == NULL ) |
---|
| 85 | { |
---|
[635] | 86 | printk("\n[ERROR] in %s : KCM failed / order %d / cluster %x\n", |
---|
| 87 | __FUNCTION__ , order , local_cxy ); |
---|
[180] | 88 | return NULL; |
---|
| 89 | } |
---|
[1] | 90 | |
---|
[635] | 91 | // reset memory if requested |
---|
| 92 | if( flags & AF_ZERO ) memset( ptr , 0 , 1<<order ); |
---|
[18] | 93 | |
---|
[438] | 94 | #if DEBUG_KMEM |
---|
[635] | 95 | thread_t * this = CURRENT_THREAD; |
---|
| 96 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 97 | if( DEBUG_KMEM < cycle ) |
---|
[635] | 98 | printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n", |
---|
[611] | 99 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[635] | 100 | 1<<order, ptr, local_cxy, cycle ); |
---|
[433] | 101 | #endif |
---|
[635] | 102 | return ptr; |
---|
[1] | 103 | } |
---|
[656] | 104 | /////////////////////////// |
---|
[635] | 105 | else if( type == KMEM_KHM ) |
---|
[159] | 106 | { |
---|
| 107 | // allocate memory from KHM |
---|
[635] | 108 | void * ptr = khm_alloc( &LOCAL_CLUSTER->khm , order ); |
---|
| 109 | |
---|
[180] | 110 | if( ptr == NULL ) |
---|
| 111 | { |
---|
[635] | 112 | printk("\n[ERROR] in %s : KHM failed / order %d / cluster %x\n", |
---|
| 113 | __FUNCTION__ , order , local_cxy ); |
---|
[180] | 114 | return NULL; |
---|
| 115 | } |
---|
[1] | 116 | |
---|
[159] | 117 | // reset memory if requested |
---|
[635] | 118 | if( flags & AF_ZERO ) memset( ptr , 0 , order ); |
---|
[7] | 119 | |
---|
[438] | 120 | #if DEBUG_KMEM |
---|
[635] | 121 | thread_t * this = CURRENT_THREAD; |
---|
| 122 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 123 | if( DEBUG_KMEM < cycle ) |
---|
[635] | 124 | printk("\n[%s] thread[%x,%x] from KHM / %d bytes / base %x / cxy %x / cycle %d\n", |
---|
[611] | 125 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[635] | 126 | order, ptr, local_cxy, cycle ); |
---|
[435] | 127 | #endif |
---|
[635] | 128 | return ptr; |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
---|
| 133 | return NULL; |
---|
| 134 | } |
---|
| 135 | } // end kmem_alloc() |
---|
[435] | 136 | |
---|
[635] | 137 | ////////////////////////////////// |
---|
| 138 | void kmem_free( kmem_req_t * req ) |
---|
| 139 | { |
---|
| 140 | uint32_t type = req->type; |
---|
| 141 | |
---|
[656] | 142 | ////////////////////// |
---|
[635] | 143 | if( type == KMEM_PPM ) |
---|
| 144 | { |
---|
| 145 | page_t * page = GET_PTR( ppm_base2page( XPTR( local_cxy , req->ptr ) ) ); |
---|
| 146 | |
---|
| 147 | ppm_free_pages( page ); |
---|
| 148 | } |
---|
[656] | 149 | /////////////////////////// |
---|
[635] | 150 | else if( type == KMEM_KCM ) |
---|
| 151 | { |
---|
| 152 | kcm_free( req->ptr ); |
---|
[1] | 153 | } |
---|
[656] | 154 | /////////////////////////// |
---|
[635] | 155 | else if( type == KMEM_KHM ) |
---|
| 156 | { |
---|
| 157 | khm_free( req->ptr ); |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
---|
| 162 | } |
---|
| 163 | } // end kmem_free() |
---|
| 164 | |
---|
| 165 | /////////////////////////////////////////// |
---|
| 166 | void * kmem_remote_alloc( cxy_t cxy, |
---|
| 167 | kmem_req_t * req ) |
---|
| 168 | { |
---|
| 169 | uint32_t type; // KMEM_PPM / KMEM_KCM / KMEM_KHM |
---|
| 170 | uint32_t flags; // AF_ZERO / AF_KERNEL / AF_NONE |
---|
| 171 | uint32_t order; // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes |
---|
| 172 | |
---|
| 173 | type = req->type; |
---|
| 174 | order = req->order; |
---|
| 175 | flags = req->flags; |
---|
| 176 | |
---|
[656] | 177 | ////////////////////// |
---|
[635] | 178 | if( type == KMEM_PPM ) |
---|
[159] | 179 | { |
---|
[656] | 180 | // allocate the number of requested pages from remote cluster |
---|
| 181 | xptr_t page_xp = ppm_remote_alloc_pages( cxy , order ); |
---|
[635] | 182 | |
---|
[656] | 183 | if( page_xp == XPTR_NULL ) |
---|
[159] | 184 | { |
---|
[635] | 185 | printk("\n[ERROR] in %s : failed for PPM / order %d in cluster %x\n", |
---|
| 186 | __FUNCTION__ , order , cxy ); |
---|
| 187 | return NULL; |
---|
| 188 | } |
---|
[567] | 189 | |
---|
[656] | 190 | // get extended pointer on remote buffer |
---|
[635] | 191 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[567] | 192 | |
---|
[635] | 193 | // reset page if requested |
---|
| 194 | if( flags & AF_ZERO ) hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
[1] | 195 | |
---|
[635] | 196 | |
---|
| 197 | #if DEBUG_KMEM_REMOTE |
---|
| 198 | thread_t * this = CURRENT_THREAD; |
---|
| 199 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 200 | if( DEBUG_KMEM_REMOTE < cycle ) |
---|
| 201 | printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n", |
---|
| 202 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[656] | 203 | 1<<order, ppm_page2ppn( page_xp ), cxy, cycle ); |
---|
[635] | 204 | #endif |
---|
[656] | 205 | return GET_PTR( base_xp ); |
---|
[635] | 206 | } |
---|
[656] | 207 | /////////////////////////// |
---|
[635] | 208 | else if( type == KMEM_KCM ) |
---|
| 209 | { |
---|
[159] | 210 | // allocate memory from KCM |
---|
[635] | 211 | void * ptr = kcm_remote_alloc( cxy , order ); |
---|
| 212 | |
---|
[180] | 213 | if( ptr == NULL ) |
---|
| 214 | { |
---|
[635] | 215 | printk("\n[ERROR] in %s : failed for KCM / order %d in cluster %x\n", |
---|
| 216 | __FUNCTION__ , order , cxy ); |
---|
[180] | 217 | return NULL; |
---|
| 218 | } |
---|
[7] | 219 | |
---|
[159] | 220 | // reset memory if requested |
---|
[635] | 221 | if( flags & AF_ZERO ) hal_remote_memset( XPTR( cxy , ptr ) , 0 , 1<<order ); |
---|
[7] | 222 | |
---|
[635] | 223 | #if DEBUG_KMEM_REMOTE |
---|
| 224 | thread_t * this = CURRENT_THREAD; |
---|
| 225 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 226 | if( DEBUG_KMEM_REMOTE < cycle ) |
---|
| 227 | printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n", |
---|
| 228 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 229 | 1<<order, ptr, cxy, cycle ); |
---|
[435] | 230 | #endif |
---|
[635] | 231 | return ptr; |
---|
[1] | 232 | } |
---|
[656] | 233 | /////////////////////////// |
---|
[635] | 234 | else if( type == KMEM_KHM ) |
---|
| 235 | { |
---|
| 236 | printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ ); |
---|
| 237 | return NULL; |
---|
| 238 | } |
---|
| 239 | else |
---|
| 240 | { |
---|
| 241 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
---|
| 242 | return NULL; |
---|
| 243 | } |
---|
| 244 | } // kmem_remote_malloc() |
---|
[1] | 245 | |
---|
[635] | 246 | //////////////////////////////////////// |
---|
| 247 | void kmem_remote_free( cxy_t cxy, |
---|
| 248 | kmem_req_t * req ) |
---|
| 249 | { |
---|
| 250 | uint32_t type = req->type; |
---|
[1] | 251 | |
---|
[656] | 252 | ////////////////////// |
---|
[635] | 253 | if( type == KMEM_PPM ) |
---|
[159] | 254 | { |
---|
[635] | 255 | page_t * page = GET_PTR( ppm_base2page( XPTR( cxy , req->ptr ) ) ); |
---|
| 256 | |
---|
| 257 | ppm_remote_free_pages( cxy , page ); |
---|
| 258 | } |
---|
[656] | 259 | /////////////////////////// |
---|
[635] | 260 | else if( type == KMEM_KCM ) |
---|
| 261 | { |
---|
| 262 | kcm_remote_free( cxy , req->ptr ); |
---|
[159] | 263 | } |
---|
[656] | 264 | /////////////////////////// |
---|
[635] | 265 | else if( type == KMEM_KHM ) |
---|
| 266 | { |
---|
| 267 | printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ ); |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
---|
| 272 | } |
---|
| 273 | } // end kmem_remote_free() |
---|
[18] | 274 | |
---|
[1] | 275 | |
---|
| 276 | |
---|