| 1 | /* |
|---|
| 2 | * kmem.c - kernel memory allocator implementation. |
|---|
| 3 | * |
|---|
| 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
|---|
| 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 | |
|---|
| 24 | #include <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <hal_special.h> |
|---|
| 27 | #include <printk.h> |
|---|
| 28 | #include <cluster.h> |
|---|
| 29 | #include <memcpy.h> |
|---|
| 30 | #include <khm.h> |
|---|
| 31 | #include <ppm.h> |
|---|
| 32 | #include <kcm.h> |
|---|
| 33 | #include <page.h> |
|---|
| 34 | #include <kmem.h> |
|---|
| 35 | |
|---|
| 36 | ///////////////////////////////////// |
|---|
| 37 | void * kmem_alloc( kmem_req_t * req ) |
|---|
| 38 | { |
|---|
| 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 |
|---|
| 42 | |
|---|
| 43 | type = req->type; |
|---|
| 44 | order = req->order; |
|---|
| 45 | flags = req->flags; |
|---|
| 46 | |
|---|
| 47 | ////////////////////////////////// PPM |
|---|
| 48 | if( type == KMEM_PPM ) |
|---|
| 49 | { |
|---|
| 50 | // allocate the number of requested pages |
|---|
| 51 | page_t * page_ptr = (void *)ppm_alloc_pages( order ); |
|---|
| 52 | |
|---|
| 53 | if( page_ptr == NULL ) |
|---|
| 54 | { |
|---|
| 55 | printk("\n[ERROR] in %s : PPM failed / order %d / cluster %x\n", |
|---|
| 56 | __FUNCTION__ , order , local_cxy ); |
|---|
| 57 | return NULL; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | xptr_t page_xp = XPTR( local_cxy , page_ptr ); |
|---|
| 61 | |
|---|
| 62 | // reset page if requested |
|---|
| 63 | if( flags & AF_ZERO ) page_zero( page_ptr ); |
|---|
| 64 | |
|---|
| 65 | // get pointer on buffer from the page descriptor |
|---|
| 66 | void * ptr = GET_PTR( ppm_page2base( page_xp ) ); |
|---|
| 67 | |
|---|
| 68 | #if DEBUG_KMEM |
|---|
| 69 | thread_t * this = CURRENT_THREAD; |
|---|
| 70 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 71 | if( DEBUG_KMEM < cycle ) |
|---|
| 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 ); |
|---|
| 75 | #endif |
|---|
| 76 | return ptr; |
|---|
| 77 | } |
|---|
| 78 | ///////////////////////////////////// KCM |
|---|
| 79 | else if( type == KMEM_KCM ) |
|---|
| 80 | { |
|---|
| 81 | // allocate memory from KCM |
|---|
| 82 | void * ptr = kcm_alloc( order ); |
|---|
| 83 | |
|---|
| 84 | if( ptr == NULL ) |
|---|
| 85 | { |
|---|
| 86 | printk("\n[ERROR] in %s : KCM failed / order %d / cluster %x\n", |
|---|
| 87 | __FUNCTION__ , order , local_cxy ); |
|---|
| 88 | return NULL; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // reset memory if requested |
|---|
| 92 | if( flags & AF_ZERO ) memset( ptr , 0 , 1<<order ); |
|---|
| 93 | |
|---|
| 94 | #if DEBUG_KMEM |
|---|
| 95 | thread_t * this = CURRENT_THREAD; |
|---|
| 96 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 97 | if( DEBUG_KMEM < cycle ) |
|---|
| 98 | printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n", |
|---|
| 99 | __FUNCTION__, this->process->pid, this->trdid, |
|---|
| 100 | 1<<order, ptr, local_cxy, cycle ); |
|---|
| 101 | #endif |
|---|
| 102 | return ptr; |
|---|
| 103 | } |
|---|
| 104 | //////////////////////////////////// KHM |
|---|
| 105 | else if( type == KMEM_KHM ) |
|---|
| 106 | { |
|---|
| 107 | // allocate memory from KHM |
|---|
| 108 | void * ptr = khm_alloc( &LOCAL_CLUSTER->khm , order ); |
|---|
| 109 | |
|---|
| 110 | if( ptr == NULL ) |
|---|
| 111 | { |
|---|
| 112 | printk("\n[ERROR] in %s : KHM failed / order %d / cluster %x\n", |
|---|
| 113 | __FUNCTION__ , order , local_cxy ); |
|---|
| 114 | return NULL; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | // reset memory if requested |
|---|
| 118 | if( flags & AF_ZERO ) memset( ptr , 0 , order ); |
|---|
| 119 | |
|---|
| 120 | #if DEBUG_KMEM |
|---|
| 121 | thread_t * this = CURRENT_THREAD; |
|---|
| 122 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 123 | if( DEBUG_KMEM < cycle ) |
|---|
| 124 | printk("\n[%s] thread[%x,%x] from KHM / %d bytes / base %x / cxy %x / cycle %d\n", |
|---|
| 125 | __FUNCTION__, this->process->pid, this->trdid, |
|---|
| 126 | order, ptr, local_cxy, cycle ); |
|---|
| 127 | #endif |
|---|
| 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() |
|---|
| 136 | |
|---|
| 137 | ////////////////////////////////// |
|---|
| 138 | void kmem_free( kmem_req_t * req ) |
|---|
| 139 | { |
|---|
| 140 | uint32_t type = req->type; |
|---|
| 141 | |
|---|
| 142 | if( type == KMEM_PPM ) |
|---|
| 143 | { |
|---|
| 144 | page_t * page = GET_PTR( ppm_base2page( XPTR( local_cxy , req->ptr ) ) ); |
|---|
| 145 | |
|---|
| 146 | ppm_free_pages( page ); |
|---|
| 147 | } |
|---|
| 148 | else if( type == KMEM_KCM ) |
|---|
| 149 | { |
|---|
| 150 | kcm_free( req->ptr ); |
|---|
| 151 | } |
|---|
| 152 | else if( type == KMEM_KHM ) |
|---|
| 153 | { |
|---|
| 154 | khm_free( req->ptr ); |
|---|
| 155 | } |
|---|
| 156 | else |
|---|
| 157 | { |
|---|
| 158 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
|---|
| 159 | } |
|---|
| 160 | } // end kmem_free() |
|---|
| 161 | |
|---|
| 162 | /////////////////////////////////////////// |
|---|
| 163 | void * kmem_remote_alloc( cxy_t cxy, |
|---|
| 164 | kmem_req_t * req ) |
|---|
| 165 | { |
|---|
| 166 | uint32_t type; // KMEM_PPM / KMEM_KCM / KMEM_KHM |
|---|
| 167 | uint32_t flags; // AF_ZERO / AF_KERNEL / AF_NONE |
|---|
| 168 | uint32_t order; // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes |
|---|
| 169 | |
|---|
| 170 | type = req->type; |
|---|
| 171 | order = req->order; |
|---|
| 172 | flags = req->flags; |
|---|
| 173 | |
|---|
| 174 | ///////////////////////////////// PPM |
|---|
| 175 | if( type == KMEM_PPM ) |
|---|
| 176 | { |
|---|
| 177 | // allocate the number of requested pages |
|---|
| 178 | page_t * page_ptr = ppm_remote_alloc_pages( cxy , order ); |
|---|
| 179 | |
|---|
| 180 | if( page_ptr == NULL ) |
|---|
| 181 | { |
|---|
| 182 | printk("\n[ERROR] in %s : failed for PPM / order %d in cluster %x\n", |
|---|
| 183 | __FUNCTION__ , order , cxy ); |
|---|
| 184 | return NULL; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | xptr_t page_xp = XPTR( cxy , page_ptr ); |
|---|
| 188 | |
|---|
| 189 | // get pointer on buffer from the page descriptor |
|---|
| 190 | xptr_t base_xp = ppm_page2base( page_xp ); |
|---|
| 191 | |
|---|
| 192 | // reset page if requested |
|---|
| 193 | if( flags & AF_ZERO ) hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
|---|
| 194 | |
|---|
| 195 | void * ptr = GET_PTR( base_xp ); |
|---|
| 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, |
|---|
| 203 | 1<<order, ppm_page2ppn(XPTR(local_cxy,ptr)), cxy, cycle ); |
|---|
| 204 | #endif |
|---|
| 205 | return ptr; |
|---|
| 206 | } |
|---|
| 207 | /////////////////////////////////// KCM |
|---|
| 208 | else if( type == KMEM_KCM ) |
|---|
| 209 | { |
|---|
| 210 | // allocate memory from KCM |
|---|
| 211 | void * ptr = kcm_remote_alloc( cxy , order ); |
|---|
| 212 | |
|---|
| 213 | if( ptr == NULL ) |
|---|
| 214 | { |
|---|
| 215 | printk("\n[ERROR] in %s : failed for KCM / order %d in cluster %x\n", |
|---|
| 216 | __FUNCTION__ , order , cxy ); |
|---|
| 217 | return NULL; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | // reset memory if requested |
|---|
| 221 | if( flags & AF_ZERO ) hal_remote_memset( XPTR( cxy , ptr ) , 0 , 1<<order ); |
|---|
| 222 | |
|---|
| 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 ); |
|---|
| 230 | #endif |
|---|
| 231 | return ptr; |
|---|
| 232 | } |
|---|
| 233 | /////////////////////////////////// KHM |
|---|
| 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() |
|---|
| 245 | |
|---|
| 246 | //////////////////////////////////////// |
|---|
| 247 | void kmem_remote_free( cxy_t cxy, |
|---|
| 248 | kmem_req_t * req ) |
|---|
| 249 | { |
|---|
| 250 | uint32_t type = req->type; |
|---|
| 251 | |
|---|
| 252 | if( type == KMEM_PPM ) |
|---|
| 253 | { |
|---|
| 254 | page_t * page = GET_PTR( ppm_base2page( XPTR( cxy , req->ptr ) ) ); |
|---|
| 255 | |
|---|
| 256 | ppm_remote_free_pages( cxy , page ); |
|---|
| 257 | } |
|---|
| 258 | else if( type == KMEM_KCM ) |
|---|
| 259 | { |
|---|
| 260 | kcm_remote_free( cxy , req->ptr ); |
|---|
| 261 | } |
|---|
| 262 | else if( type == KMEM_KHM ) |
|---|
| 263 | { |
|---|
| 264 | printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ ); |
|---|
| 265 | } |
|---|
| 266 | else |
|---|
| 267 | { |
|---|
| 268 | printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__); |
|---|
| 269 | } |
|---|
| 270 | } // end kmem_remote_free() |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|