[1] | 1 | /* |
---|
[635] | 2 | * kcm.c - Kernel Cache Manager implementation. |
---|
[18] | 3 | * |
---|
[672] | 4 | * Author 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> |
---|
[567] | 27 | #include <busylock.h> |
---|
[1] | 28 | #include <list.h> |
---|
| 29 | #include <printk.h> |
---|
| 30 | #include <bits.h> |
---|
| 31 | #include <ppm.h> |
---|
| 32 | #include <thread.h> |
---|
| 33 | #include <page.h> |
---|
| 34 | #include <cluster.h> |
---|
[7] | 35 | #include <kmem.h> |
---|
[1] | 36 | #include <kcm.h> |
---|
| 37 | |
---|
[567] | 38 | |
---|
[635] | 39 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 40 | // Local access functions |
---|
| 41 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 42 | |
---|
[1] | 43 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 44 | // This static function must be called by a local thread. |
---|
[657] | 45 | // It returns a pointer on a block allocated from an active kcm_page. |
---|
| 46 | // It makes a panic if no block is available in the selected page. |
---|
[635] | 47 | // It changes the page status as required. |
---|
[1] | 48 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 49 | // @ kcm : pointer on KCM allocator. |
---|
[657] | 50 | // @ kcm_page : pointer on an active kcm_page. |
---|
[635] | 51 | // @ return pointer on allocated block. |
---|
[7] | 52 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 53 | static void * __attribute__((noinline)) kcm_get_block( kcm_t * kcm, |
---|
| 54 | kcm_page_t * kcm_page ) |
---|
[1] | 55 | { |
---|
[635] | 56 | // initialise variables |
---|
| 57 | uint32_t size = 1 << kcm->order; |
---|
| 58 | uint32_t max = kcm->max_blocks; |
---|
| 59 | uint32_t count = kcm_page->count; |
---|
| 60 | uint64_t status = kcm_page->status; |
---|
[1] | 61 | |
---|
[672] | 62 | assert( __FUNCTION__, (count < max) , "kcm_page should not be full" ); |
---|
[433] | 63 | |
---|
[635] | 64 | uint32_t index = 1; |
---|
| 65 | uint64_t mask = (uint64_t)0x2; |
---|
[50] | 66 | |
---|
[635] | 67 | // allocate first free block in kcm_page, update status, |
---|
| 68 | // and count , compute index of allocated block in kcm_page |
---|
| 69 | while( index <= max ) |
---|
| 70 | { |
---|
[657] | 71 | if( (status & mask) == 0 ) // block found |
---|
[635] | 72 | { |
---|
[657] | 73 | // update page count and status |
---|
[635] | 74 | kcm_page->status = status | mask; |
---|
| 75 | kcm_page->count = count + 1; |
---|
| 76 | break; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | index++; |
---|
| 80 | mask <<= 1; |
---|
| 81 | } |
---|
[18] | 82 | |
---|
[657] | 83 | // change the page list if found block is the last |
---|
[635] | 84 | if( count == max-1 ) |
---|
| 85 | { |
---|
[50] | 86 | list_unlink( &kcm_page->list); |
---|
[635] | 87 | kcm->active_pages_nr--; |
---|
[1] | 88 | |
---|
[635] | 89 | list_add_first( &kcm->full_root , &kcm_page->list ); |
---|
| 90 | kcm->full_pages_nr ++; |
---|
| 91 | } |
---|
[1] | 92 | |
---|
[161] | 93 | // compute return pointer |
---|
[635] | 94 | void * ptr = (void *)((intptr_t)kcm_page + (index * size) ); |
---|
[1] | 95 | |
---|
[672] | 96 | #if DEBUG_KCM |
---|
[635] | 97 | thread_t * this = CURRENT_THREAD; |
---|
| 98 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 99 | if( DEBUG_KCM < cycle ) |
---|
[635] | 100 | printk("\n[%s] thread[%x,%x] allocated block %x in page %x / size %d / count %d / cycle %d\n", |
---|
| 101 | __FUNCTION__, this->process->pid, this->trdid, ptr, kcm_page, size, count + 1, cycle ); |
---|
[433] | 102 | #endif |
---|
[50] | 103 | |
---|
| 104 | return ptr; |
---|
| 105 | |
---|
[635] | 106 | } // end kcm_get_block() |
---|
| 107 | |
---|
[1] | 108 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 109 | // This private static function must be called by a local thread. |
---|
| 110 | // It releases a previously allocated block to the relevant kcm_page. |
---|
| 111 | // It makes a panic if the released block is not allocated in this page. |
---|
| 112 | // It changes the kcm_page status as required. |
---|
[1] | 113 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 114 | // @ kcm : pointer on kcm allocator. |
---|
| 115 | // @ kcm_page : pointer on kcm_page. |
---|
| 116 | // @ block_ptr : pointer on block to be released. |
---|
[7] | 117 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 118 | static void __attribute__((noinline)) kcm_put_block ( kcm_t * kcm, |
---|
| 119 | kcm_page_t * kcm_page, |
---|
| 120 | void * block_ptr ) |
---|
[1] | 121 | { |
---|
[635] | 122 | // initialise variables |
---|
| 123 | uint32_t max = kcm->max_blocks; |
---|
| 124 | uint32_t size = 1 << kcm->order; |
---|
| 125 | uint32_t count = kcm_page->count; |
---|
| 126 | uint64_t status = kcm_page->status; |
---|
| 127 | |
---|
[161] | 128 | // compute block index from block pointer |
---|
[635] | 129 | uint32_t index = ((intptr_t)block_ptr - (intptr_t)kcm_page) / size; |
---|
[18] | 130 | |
---|
[635] | 131 | // compute mask in bit vector |
---|
| 132 | uint64_t mask = ((uint64_t)0x1) << index; |
---|
[176] | 133 | |
---|
[672] | 134 | if( (status & mask) == 0 ) |
---|
| 135 | { |
---|
| 136 | printk("\n[WARNING] in %s : block[%x,%x] not allocated / kcm %x / kcm_page %x\n", |
---|
| 137 | __FUNCTION__, local_cxy, block_ptr, kcm, kcm_page ); |
---|
| 138 | printk(" status %L / mask %L / sts & msk %L\n", status, mask, (status & mask) ); |
---|
| 139 | kcm_remote_display( local_cxy , kcm ); |
---|
| 140 | return; |
---|
| 141 | } |
---|
[619] | 142 | |
---|
[635] | 143 | // update status & count in kcm_page |
---|
| 144 | kcm_page->status = status & ~mask; |
---|
| 145 | kcm_page->count = count - 1; |
---|
[50] | 146 | |
---|
[635] | 147 | // change the page mode if page was full |
---|
| 148 | if( count == max ) |
---|
[1] | 149 | { |
---|
[50] | 150 | list_unlink( &kcm_page->list ); |
---|
[635] | 151 | kcm->full_pages_nr --; |
---|
[1] | 152 | |
---|
[50] | 153 | list_add_last( &kcm->active_root, &kcm_page->list ); |
---|
[1] | 154 | kcm->active_pages_nr ++; |
---|
| 155 | } |
---|
| 156 | |
---|
[672] | 157 | #if DEBUG_KCM |
---|
[635] | 158 | thread_t * this = CURRENT_THREAD; |
---|
| 159 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 160 | if( DEBUG_KCM < cycle ) |
---|
[672] | 161 | printk("\n[%s] thread[%x,%x] block %x / page %x / size %d / count %d / cycle %d\n", |
---|
[635] | 162 | __FUNCTION__, this->process->pid, this->trdid, block_ptr, kcm_page, size, count - 1, cycle ); |
---|
| 163 | #endif |
---|
[1] | 164 | |
---|
[635] | 165 | } // kcm_put_block() |
---|
[1] | 166 | |
---|
| 167 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[657] | 168 | // This static function must be called by a local thread. |
---|
| 169 | // It returns one non-full kcm_page with the following policy : |
---|
[635] | 170 | // - if the "active_list" is non empty, it returns the first "active" page, |
---|
| 171 | // without modifying the KCM state. |
---|
[657] | 172 | // - if the "active_list" is empty, it allocates a new page from PPM, inserts |
---|
[635] | 173 | // this page in the active_list, and returns it. |
---|
[1] | 174 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[635] | 175 | // @ kcm : local pointer on local KCM allocator. |
---|
| 176 | // @ return pointer on a non-full kcm page if success / returns NULL if no memory. |
---|
| 177 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 178 | static kcm_page_t * __attribute__((noinline)) kcm_get_page( kcm_t * kcm ) |
---|
[1] | 179 | { |
---|
[635] | 180 | kcm_page_t * kcm_page; |
---|
[1] | 181 | |
---|
[635] | 182 | uint32_t active_pages_nr = kcm->active_pages_nr; |
---|
[18] | 183 | |
---|
[635] | 184 | if( active_pages_nr > 0 ) // return first active page |
---|
| 185 | { |
---|
| 186 | kcm_page = LIST_FIRST( &kcm->active_root , kcm_page_t , list ); |
---|
| 187 | } |
---|
| 188 | else // allocate a new page from PPM |
---|
[7] | 189 | { |
---|
[635] | 190 | // get one 4 Kbytes page from local PPM |
---|
| 191 | page_t * page = ppm_alloc_pages( 0 ); |
---|
[7] | 192 | |
---|
[635] | 193 | if( page == NULL ) |
---|
| 194 | { |
---|
| 195 | printk("\n[ERROR] in %s : failed to allocate page in cluster %x\n", |
---|
| 196 | __FUNCTION__ , local_cxy ); |
---|
[1] | 197 | |
---|
[635] | 198 | return NULL; |
---|
| 199 | } |
---|
[1] | 200 | |
---|
[635] | 201 | // get page base address |
---|
| 202 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) ); |
---|
[1] | 203 | |
---|
[635] | 204 | // get local pointer on kcm_page |
---|
| 205 | kcm_page = GET_PTR( base_xp ); |
---|
[18] | 206 | |
---|
[635] | 207 | // initialize kcm_page descriptor |
---|
| 208 | kcm_page->status = 0; |
---|
| 209 | kcm_page->count = 0; |
---|
| 210 | kcm_page->kcm = kcm; |
---|
| 211 | kcm_page->page = page; |
---|
[1] | 212 | |
---|
[635] | 213 | // introduce new page in KCM active_list |
---|
| 214 | list_add_first( &kcm->active_root , &kcm_page->list ); |
---|
| 215 | kcm->active_pages_nr ++; |
---|
[1] | 216 | } |
---|
| 217 | |
---|
[50] | 218 | return kcm_page; |
---|
[1] | 219 | |
---|
[635] | 220 | } // end kcm_get_page() |
---|
| 221 | |
---|
[7] | 222 | ////////////////////////////// |
---|
| 223 | void kcm_init( kcm_t * kcm, |
---|
[635] | 224 | uint32_t order) |
---|
[1] | 225 | { |
---|
| 226 | |
---|
[672] | 227 | assert( __FUNCTION__, ((order > 5) && (order < 12)) , "order must be in [6,11]" ); |
---|
[619] | 228 | |
---|
[672] | 229 | assert( __FUNCTION__, (CONFIG_PPM_PAGE_SHIFT == 12) , "check status bit_vector width" ); |
---|
| 230 | |
---|
[20] | 231 | // initialize lock |
---|
[635] | 232 | remote_busylock_init( XPTR( local_cxy , &kcm->lock ) , LOCK_KCM_STATE ); |
---|
[1] | 233 | |
---|
[20] | 234 | // initialize KCM page lists |
---|
[635] | 235 | kcm->full_pages_nr = 0; |
---|
[1] | 236 | kcm->active_pages_nr = 0; |
---|
[635] | 237 | list_root_init( &kcm->full_root ); |
---|
[1] | 238 | list_root_init( &kcm->active_root ); |
---|
| 239 | |
---|
[635] | 240 | // initialize order and max_blocks |
---|
| 241 | kcm->order = order; |
---|
| 242 | kcm->max_blocks = ( CONFIG_PPM_PAGE_SIZE >> order ) - 1; |
---|
| 243 | |
---|
[619] | 244 | #if DEBUG_KCM |
---|
| 245 | thread_t * this = CURRENT_THREAD; |
---|
| 246 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 247 | if( DEBUG_KCM < cycle ) |
---|
[635] | 248 | printk("\n[%s] thread[%x,%x] initialised KCM / order %d / max_blocks %d\n", |
---|
| 249 | __FUNCTION__, this->process->pid, this->trdid, order, kcm->max_blocks ); |
---|
[619] | 250 | #endif |
---|
| 251 | |
---|
[635] | 252 | } // end kcm_init() |
---|
[1] | 253 | |
---|
| 254 | /////////////////////////////// |
---|
| 255 | void kcm_destroy( kcm_t * kcm ) |
---|
| 256 | { |
---|
[50] | 257 | kcm_page_t * kcm_page; |
---|
[18] | 258 | |
---|
[635] | 259 | // build extended pointer on KCM lock |
---|
| 260 | xptr_t lock_xp = XPTR( local_cxy , &kcm->lock ); |
---|
| 261 | |
---|
[20] | 262 | // get KCM lock |
---|
[635] | 263 | remote_busylock_acquire( lock_xp ); |
---|
[1] | 264 | |
---|
[635] | 265 | // release all full pages |
---|
| 266 | while( list_is_empty( &kcm->full_root ) == false ) |
---|
[1] | 267 | { |
---|
[635] | 268 | kcm_page = LIST_FIRST( &kcm->full_root , kcm_page_t , list ); |
---|
| 269 | list_unlink( &kcm_page->list ); |
---|
[50] | 270 | ppm_free_pages( kcm_page->page ); |
---|
[1] | 271 | } |
---|
| 272 | |
---|
[635] | 273 | // release all empty pages |
---|
| 274 | while( list_is_empty( &kcm->active_root ) == false ) |
---|
[1] | 275 | { |
---|
[635] | 276 | kcm_page = LIST_FIRST( &kcm->active_root , kcm_page_t , list ); |
---|
| 277 | list_unlink( &kcm_page->list ); |
---|
[50] | 278 | ppm_free_pages( kcm_page->page ); |
---|
[1] | 279 | } |
---|
| 280 | |
---|
[635] | 281 | // release KCM lock |
---|
| 282 | remote_busylock_release( lock_xp ); |
---|
| 283 | |
---|
[657] | 284 | } // end kcm_destroy() |
---|
| 285 | |
---|
[635] | 286 | ////////////////////////////////// |
---|
| 287 | void * kcm_alloc( uint32_t order ) |
---|
| 288 | { |
---|
| 289 | kcm_t * kcm_ptr; |
---|
| 290 | kcm_page_t * kcm_page; |
---|
| 291 | void * block_ptr; |
---|
| 292 | |
---|
[657] | 293 | // min block size is 64 bytes |
---|
[635] | 294 | if( order < 6 ) order = 6; |
---|
| 295 | |
---|
[672] | 296 | assert( __FUNCTION__, (order < 12) , "order = %d / must be less than 12" , order ); |
---|
[635] | 297 | |
---|
| 298 | // get local pointer on relevant KCM allocator |
---|
| 299 | kcm_ptr = &LOCAL_CLUSTER->kcm[order - 6]; |
---|
| 300 | |
---|
| 301 | // build extended pointer on local KCM lock |
---|
| 302 | xptr_t lock_xp = XPTR( local_cxy , &kcm_ptr->lock ); |
---|
| 303 | |
---|
| 304 | // get KCM lock |
---|
| 305 | remote_busylock_acquire( lock_xp ); |
---|
| 306 | |
---|
| 307 | // get a non-full kcm_page |
---|
| 308 | kcm_page = kcm_get_page( kcm_ptr ); |
---|
| 309 | |
---|
[657] | 310 | #if DEBUG_KCM |
---|
| 311 | thread_t * this = CURRENT_THREAD; |
---|
| 312 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[672] | 313 | if( DEBUG_KCM < cycle ) |
---|
[657] | 314 | { |
---|
| 315 | printk("\n[%s] thread[%x,%x] enters / order %d / page %x / kcm %x / page_status (%x|%x)\n", |
---|
| 316 | __FUNCTION__, this->process->pid, this->trdid, order, kcm_page, kcm_ptr, |
---|
| 317 | GET_CXY( kcm_page->status ), GET_PTR( kcm_page->status ) ); |
---|
| 318 | kcm_remote_display( local_cxy , kcm_ptr ); |
---|
| 319 | } |
---|
| 320 | #endif |
---|
| 321 | |
---|
[635] | 322 | if( kcm_page == NULL ) |
---|
[1] | 323 | { |
---|
[635] | 324 | remote_busylock_release( lock_xp ); |
---|
| 325 | return NULL; |
---|
[1] | 326 | } |
---|
| 327 | |
---|
[635] | 328 | // get a block from selected active page |
---|
| 329 | block_ptr = kcm_get_block( kcm_ptr , kcm_page ); |
---|
[1] | 330 | |
---|
[635] | 331 | // release lock |
---|
| 332 | remote_busylock_release( lock_xp ); |
---|
| 333 | |
---|
| 334 | #if DEBUG_KCM |
---|
[672] | 335 | if( DEBUG_KCM < cycle ) |
---|
[657] | 336 | printk("\n[%s] thread[%x,%x] exit / order %d / block %x / kcm %x / page_status (%x|%x)\n", |
---|
| 337 | __FUNCTION__, this->process->pid, this->trdid, order, block_ptr, kcm_ptr, |
---|
| 338 | GET_CXY( kcm_page->status ), GET_PTR( kcm_page->status ) ); |
---|
[635] | 339 | #endif |
---|
| 340 | |
---|
| 341 | return block_ptr; |
---|
| 342 | |
---|
| 343 | } // end kcm_alloc() |
---|
| 344 | |
---|
| 345 | ///////////////////////////////// |
---|
| 346 | void kcm_free( void * block_ptr ) |
---|
[1] | 347 | { |
---|
[635] | 348 | kcm_t * kcm_ptr; |
---|
[50] | 349 | kcm_page_t * kcm_page; |
---|
[1] | 350 | |
---|
[635] | 351 | // check argument |
---|
[672] | 352 | assert( __FUNCTION__, (block_ptr != NULL) , "block pointer cannot be NULL" ); |
---|
[635] | 353 | |
---|
| 354 | // get local pointer on KCM page |
---|
| 355 | kcm_page = (kcm_page_t *)((intptr_t)block_ptr & ~CONFIG_PPM_PAGE_MASK); |
---|
| 356 | |
---|
| 357 | // get local pointer on KCM descriptor |
---|
| 358 | kcm_ptr = kcm_page->kcm; |
---|
| 359 | |
---|
| 360 | #if DEBUG_KCM |
---|
| 361 | thread_t * this = CURRENT_THREAD; |
---|
| 362 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[657] | 363 | if( (DEBUG_KCM < cycle) && (local_cxy == 1) ) |
---|
| 364 | { |
---|
| 365 | printk("\n[%s] thread[%x,%x] enters / order %d / block %x / page %x / kcm %x / status [%x,%x]\n", |
---|
| 366 | __FUNCTION__, this->process->pid, this->trdid, kcm_ptr->order, block_ptr, kcm_page, kcm_ptr, |
---|
| 367 | GET_CXY(kcm_page->status), GET_PTR(kcm_page->status) ); |
---|
| 368 | kcm_remote_display( local_cxy , kcm_ptr ); |
---|
| 369 | } |
---|
[635] | 370 | #endif |
---|
| 371 | |
---|
| 372 | // build extended pointer on local KCM lock |
---|
| 373 | xptr_t lock_xp = XPTR( local_cxy , &kcm_ptr->lock ); |
---|
| 374 | |
---|
[20] | 375 | // get lock |
---|
[635] | 376 | remote_busylock_acquire( lock_xp ); |
---|
[18] | 377 | |
---|
[635] | 378 | // release block |
---|
| 379 | kcm_put_block( kcm_ptr , kcm_page , block_ptr ); |
---|
| 380 | |
---|
| 381 | // release lock |
---|
| 382 | remote_busylock_release( lock_xp ); |
---|
[657] | 383 | |
---|
| 384 | #if DEBUG_KCM |
---|
| 385 | if( (DEBUG_KCM < cycle) && (local_cxy == 1) ) |
---|
| 386 | { |
---|
| 387 | printk("\n[%s] thread[%x,%x] exit / order %d / page %x / status [%x,%x]\n", |
---|
| 388 | __FUNCTION__, this->process->pid, this->trdid, kcm_ptr->order, kcm_ptr, |
---|
| 389 | GET_CXY(kcm_page->status), GET_PTR(kcm_page->status) ); |
---|
| 390 | kcm_remote_display( local_cxy , kcm_ptr ); |
---|
[635] | 391 | } |
---|
[657] | 392 | #endif |
---|
[635] | 393 | |
---|
[657] | 394 | } // end kcm_free() |
---|
| 395 | |
---|
[672] | 396 | |
---|
[635] | 397 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 398 | // Remote access functions |
---|
| 399 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 400 | |
---|
| 401 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 402 | // This static function can be called by any thread running in any cluster. |
---|
[657] | 403 | // It returns a local pointer on a block allocated from an active kcm_page. |
---|
| 404 | // It makes a panic if no block available in the selected kcm_page. |
---|
[635] | 405 | // It changes the page status as required. |
---|
| 406 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[657] | 407 | // @ kcm_cxy : remote KCM cluster identifier. |
---|
[635] | 408 | // @ kcm_ptr : local pointer on remote KCM allocator. |
---|
[657] | 409 | // @ kcm_page : local pointer on remote active kcm_page to use. |
---|
[635] | 410 | // @ return a local pointer on the allocated block. |
---|
| 411 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 412 | static void * __attribute__((noinline)) kcm_remote_get_block( cxy_t kcm_cxy, |
---|
| 413 | kcm_t * kcm_ptr, |
---|
| 414 | kcm_page_t * kcm_page ) |
---|
| 415 | { |
---|
| 416 | uint32_t order = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->order ) ); |
---|
| 417 | uint32_t max = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->max_blocks ) ); |
---|
| 418 | uint32_t count = hal_remote_l32( XPTR( kcm_cxy , &kcm_page->count ) ); |
---|
| 419 | uint64_t status = hal_remote_l64( XPTR( kcm_cxy , &kcm_page->status ) ); |
---|
| 420 | uint32_t size = 1 << order; |
---|
| 421 | |
---|
[672] | 422 | assert( __FUNCTION__, (count < max) , "kcm_page should not be full" ); |
---|
[635] | 423 | |
---|
| 424 | uint32_t index = 1; |
---|
| 425 | uint64_t mask = (uint64_t)0x2; |
---|
| 426 | |
---|
| 427 | // allocate first free block in kcm_page, update status, |
---|
| 428 | // and count , compute index of allocated block in kcm_page |
---|
| 429 | while( index <= max ) |
---|
| 430 | { |
---|
[657] | 431 | if( (status & mask) == 0 ) // block found |
---|
[635] | 432 | { |
---|
| 433 | hal_remote_s64( XPTR( kcm_cxy , &kcm_page->status ) , status | mask ); |
---|
[657] | 434 | hal_remote_s32( XPTR( kcm_cxy , &kcm_page->count ) , count + 1 ); |
---|
[635] | 435 | break; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | index++; |
---|
| 439 | mask <<= 1; |
---|
| 440 | } |
---|
| 441 | |
---|
[657] | 442 | // change the page list if found block is the last |
---|
[635] | 443 | if( count == max-1 ) |
---|
[20] | 444 | { |
---|
[635] | 445 | list_remote_unlink( kcm_cxy , &kcm_page->list ); |
---|
| 446 | hal_remote_atomic_add( XPTR( kcm_cxy , &kcm_ptr->active_pages_nr ) , -1 ); |
---|
[7] | 447 | |
---|
[635] | 448 | list_remote_add_first( kcm_cxy , &kcm_ptr->full_root , &kcm_page->list ); |
---|
| 449 | hal_remote_atomic_add( XPTR( kcm_cxy , &kcm_ptr->full_pages_nr ) , 1 ); |
---|
| 450 | } |
---|
[50] | 451 | |
---|
[635] | 452 | // compute return pointer |
---|
| 453 | void * ptr = (void *)((intptr_t)kcm_page + (index * size) ); |
---|
| 454 | |
---|
| 455 | #if DEBUG_KCM_REMOTE |
---|
| 456 | thread_t * this = CURRENT_THREAD; |
---|
| 457 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 458 | if( DEBUG_KCM_REMOTE < cycle ) |
---|
| 459 | printk("\n[%s] thread[%x,%x] get block %x in page %x / cluster %x / size %x / count %d\n", |
---|
| 460 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 461 | ptr, kcm_page, kcm_cxy, size, count + 1 ); |
---|
| 462 | #endif |
---|
| 463 | |
---|
| 464 | return ptr; |
---|
| 465 | |
---|
| 466 | } // end kcm_remote_get_block() |
---|
| 467 | |
---|
| 468 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 469 | // This private static function can be called by any thread running in any cluster. |
---|
| 470 | // It releases a previously allocated block to the relevant kcm_page. |
---|
| 471 | // It changes the kcm_page status as required. |
---|
| 472 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 473 | // @ kcm_cxy : remote KCM cluster identifier |
---|
| 474 | // @ kcm_ptr : local pointer on remote KCM. |
---|
| 475 | // @ kcm_page : local pointer on kcm_page. |
---|
| 476 | // @ block_ptr : pointer on block to be released. |
---|
| 477 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 478 | static void __attribute__((noinline)) kcm_remote_put_block ( cxy_t kcm_cxy, |
---|
| 479 | kcm_t * kcm_ptr, |
---|
| 480 | kcm_page_t * kcm_page, |
---|
| 481 | void * block_ptr ) |
---|
| 482 | { |
---|
| 483 | uint32_t max = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->max_blocks ) ); |
---|
| 484 | uint32_t order = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->order ) ); |
---|
| 485 | uint32_t count = hal_remote_l32( XPTR( kcm_cxy , &kcm_page->count ) ); |
---|
| 486 | uint64_t status = hal_remote_l64( XPTR( kcm_cxy , &kcm_page->status ) ); |
---|
| 487 | uint32_t size = 1 << order; |
---|
| 488 | |
---|
| 489 | // compute block index from block pointer |
---|
| 490 | uint32_t index = ((intptr_t)block_ptr - (intptr_t)kcm_page) / size; |
---|
| 491 | |
---|
| 492 | // compute mask in bit vector |
---|
[672] | 493 | uint64_t mask = ((uint64_t)0x1) << index; |
---|
[635] | 494 | |
---|
[672] | 495 | if( (status & mask) == 0 ) |
---|
| 496 | { |
---|
| 497 | printk("\n[WARNING] in %s : block[%x,%x] not allocated / kcm %x / kcm_page %x\n", |
---|
| 498 | __FUNCTION__, kcm_cxy, block_ptr, kcm_ptr, kcm_page ); |
---|
| 499 | printk(" status %L / mask %L / sts & msk %L\n", status, mask, (status & mask) ); |
---|
| 500 | kcm_remote_display( kcm_cxy , kcm_ptr ); |
---|
| 501 | return; |
---|
| 502 | } |
---|
[635] | 503 | |
---|
| 504 | // update status & count in kcm_page |
---|
| 505 | hal_remote_s64( XPTR( kcm_cxy , &kcm_page->status ) , status & ~mask ); |
---|
| 506 | hal_remote_s32( XPTR( kcm_cxy , &kcm_page->count ) , count - 1 ); |
---|
| 507 | |
---|
| 508 | // change the page list if page was full |
---|
| 509 | if( count == max ) |
---|
| 510 | { |
---|
| 511 | list_remote_unlink( kcm_cxy , &kcm_page->list ); |
---|
| 512 | hal_remote_atomic_add( XPTR( kcm_cxy , &kcm_ptr->full_pages_nr ) , -1 ); |
---|
| 513 | |
---|
| 514 | list_remote_add_last( kcm_cxy , &kcm_ptr->active_root, &kcm_page->list ); |
---|
| 515 | hal_remote_atomic_add( XPTR( kcm_cxy , &kcm_ptr->active_pages_nr ) , 1 ); |
---|
[20] | 516 | } |
---|
[635] | 517 | |
---|
| 518 | #if (DEBUG_KCM_REMOTE & 1) |
---|
| 519 | thread_t * this = CURRENT_THREAD; |
---|
| 520 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 521 | if( DEBUG_KCM_REMOTE < cycle ) |
---|
[672] | 522 | printk("\n[%s] thread[%x,%x] block %x / page %x / cluster %x / size %x / count %d\n", |
---|
[635] | 523 | __FUNCTION__, this->process->pid, this->trdid, block_ptr, kcm_page, size, count - 1 ) |
---|
| 524 | #endif |
---|
| 525 | |
---|
| 526 | } // end kcm_remote_put_block() |
---|
| 527 | |
---|
| 528 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 529 | // This private static function can be called by any thread running in any cluster. |
---|
| 530 | // It gets one non-full KCM page from the remote KCM. |
---|
| 531 | // It allocates a page from remote PPM to populate the freelist, and initialises |
---|
| 532 | // the kcm_page descriptor when required. |
---|
| 533 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 534 | static kcm_page_t * __attribute__((noinline)) kcm_remote_get_page( cxy_t kcm_cxy, |
---|
| 535 | kcm_t * kcm_ptr ) |
---|
| 536 | { |
---|
| 537 | kcm_page_t * kcm_page; // local pointer on remote KCM page |
---|
| 538 | |
---|
| 539 | uint32_t active_pages_nr = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->active_pages_nr ) ); |
---|
| 540 | |
---|
| 541 | if( active_pages_nr > 0 ) // return first active page |
---|
| 542 | { |
---|
| 543 | kcm_page = LIST_REMOTE_FIRST( kcm_cxy , &kcm_ptr->active_root , kcm_page_t , list ); |
---|
| 544 | } |
---|
| 545 | else // allocate a new page from PPM |
---|
[20] | 546 | { |
---|
[635] | 547 | // get one 4 Kbytes page from remote PPM |
---|
[656] | 548 | xptr_t page_xp = ppm_remote_alloc_pages( kcm_cxy , 0 ); |
---|
[635] | 549 | |
---|
[656] | 550 | if( page_xp == XPTR_NULL ) |
---|
[635] | 551 | { |
---|
| 552 | printk("\n[ERROR] in %s : failed to allocate page in cluster %x\n", |
---|
| 553 | __FUNCTION__ , kcm_cxy ); |
---|
| 554 | |
---|
| 555 | return NULL; |
---|
| 556 | } |
---|
| 557 | |
---|
[656] | 558 | // get extended pointer on allocated buffer |
---|
| 559 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[635] | 560 | |
---|
| 561 | // get local pointer on kcm_page |
---|
| 562 | kcm_page = GET_PTR( base_xp ); |
---|
| 563 | |
---|
| 564 | // initialize kcm_page descriptor |
---|
| 565 | hal_remote_s32( XPTR( kcm_cxy , &kcm_page->count ) , 0 ); |
---|
| 566 | hal_remote_s64( XPTR( kcm_cxy , &kcm_page->status ) , 0 ); |
---|
| 567 | hal_remote_spt( XPTR( kcm_cxy , &kcm_page->kcm ) , kcm_ptr ); |
---|
[656] | 568 | hal_remote_spt( XPTR( kcm_cxy , &kcm_page->page ) , GET_PTR( page_xp ) ); |
---|
[635] | 569 | |
---|
| 570 | // introduce new page in remote KCM active_list |
---|
| 571 | list_remote_add_first( kcm_cxy , &kcm_ptr->active_root , &kcm_page->list ); |
---|
| 572 | hal_remote_atomic_add( XPTR( kcm_cxy , &kcm_ptr->active_pages_nr ) , 1 ); |
---|
[20] | 573 | } |
---|
[1] | 574 | |
---|
[635] | 575 | return kcm_page; |
---|
| 576 | |
---|
| 577 | } // end kcm_remote_get_page() |
---|
| 578 | |
---|
[672] | 579 | ////////////////////////////////////////// |
---|
[635] | 580 | void * kcm_remote_alloc( cxy_t kcm_cxy, |
---|
| 581 | uint32_t order ) |
---|
| 582 | { |
---|
| 583 | kcm_t * kcm_ptr; |
---|
| 584 | kcm_page_t * kcm_page; |
---|
| 585 | void * block_ptr; |
---|
| 586 | |
---|
| 587 | if( order < 6 ) order = 6; |
---|
| 588 | |
---|
[672] | 589 | assert( __FUNCTION__, (order < 12) , "order = %d / must be less than 12" , order ); |
---|
[635] | 590 | |
---|
| 591 | // get local pointer on relevant KCM allocator |
---|
| 592 | kcm_ptr = &LOCAL_CLUSTER->kcm[order - 6]; |
---|
| 593 | |
---|
| 594 | // build extended pointer on remote KCM lock |
---|
| 595 | xptr_t lock_xp = XPTR( kcm_cxy , &kcm_ptr->lock ); |
---|
| 596 | |
---|
| 597 | // get lock |
---|
| 598 | remote_busylock_acquire( lock_xp ); |
---|
| 599 | |
---|
| 600 | // get a non-full kcm_page |
---|
| 601 | kcm_page = kcm_remote_get_page( kcm_cxy , kcm_ptr ); |
---|
| 602 | |
---|
| 603 | if( kcm_page == NULL ) |
---|
| 604 | { |
---|
| 605 | remote_busylock_release( lock_xp ); |
---|
| 606 | return NULL; |
---|
| 607 | } |
---|
| 608 | |
---|
[20] | 609 | // get a block from selected active page |
---|
[635] | 610 | block_ptr = kcm_remote_get_block( kcm_cxy , kcm_ptr , kcm_page ); |
---|
[7] | 611 | |
---|
[20] | 612 | // release lock |
---|
[635] | 613 | remote_busylock_release( lock_xp ); |
---|
[1] | 614 | |
---|
[635] | 615 | #if DEBUG_KCM_REMOTE |
---|
| 616 | thread_t * this = CURRENT_THREAD; |
---|
| 617 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 618 | if( DEBUG_KCM_REMOTE < cycle ) |
---|
| 619 | printk("\n[%s] thread[%x,%x] allocated block %x / order %d / kcm[%x,%x]\n", |
---|
| 620 | __FUNCTION__, this->process->pid, this->trdid, block_ptr, order, kcm_cxy, kcm_ptr ); |
---|
| 621 | #endif |
---|
[1] | 622 | |
---|
[635] | 623 | return block_ptr; |
---|
| 624 | |
---|
| 625 | } // end kcm_remote_alloc() |
---|
| 626 | |
---|
| 627 | ///////////////////////////////////// |
---|
| 628 | void kcm_remote_free( cxy_t kcm_cxy, |
---|
| 629 | void * block_ptr ) |
---|
[1] | 630 | { |
---|
[635] | 631 | kcm_t * kcm_ptr; |
---|
[50] | 632 | kcm_page_t * kcm_page; |
---|
[18] | 633 | |
---|
[619] | 634 | // check argument |
---|
[672] | 635 | assert( __FUNCTION__, (block_ptr != NULL) , "block pointer cannot be NULL" ); |
---|
[18] | 636 | |
---|
[635] | 637 | // get local pointer on remote KCM page |
---|
| 638 | kcm_page = (kcm_page_t *)((intptr_t)block_ptr & ~CONFIG_PPM_PAGE_MASK); |
---|
[1] | 639 | |
---|
[635] | 640 | // get local pointer on remote KCM |
---|
| 641 | kcm_ptr = hal_remote_lpt( XPTR( kcm_cxy , &kcm_page->kcm ) ); |
---|
| 642 | |
---|
| 643 | // build extended pointer on remote KCM lock |
---|
| 644 | xptr_t lock_xp = XPTR( kcm_cxy , &kcm_ptr->lock ); |
---|
| 645 | |
---|
[20] | 646 | // get lock |
---|
[635] | 647 | remote_busylock_acquire( lock_xp ); |
---|
[1] | 648 | |
---|
[20] | 649 | // release block |
---|
[635] | 650 | kcm_remote_put_block( kcm_cxy , kcm_ptr , kcm_page , block_ptr ); |
---|
[1] | 651 | |
---|
[20] | 652 | // release lock |
---|
[635] | 653 | remote_busylock_release( lock_xp ); |
---|
[1] | 654 | |
---|
[635] | 655 | #if DEBUG_KCM_REMOTE |
---|
| 656 | thread_t * this = CURRENT_THREAD; |
---|
| 657 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 658 | uint32_t order = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->order ) ); |
---|
| 659 | if( DEBUG_KCM_REMOTE < cycle ) |
---|
| 660 | printk("\n[%s] thread[%x,%x] released block %x / order %d / kcm[%x,%x]\n", |
---|
| 661 | __FUNCTION__, this->process->pid, this->trdid, block_ptr, order, kcm_cxy, kcm_ptr ); |
---|
| 662 | #endif |
---|
| 663 | |
---|
| 664 | } // end kcm_remote_free |
---|
| 665 | |
---|
| 666 | ///////////////////////////////////////// |
---|
| 667 | void kcm_remote_display( cxy_t kcm_cxy, |
---|
| 668 | kcm_t * kcm_ptr ) |
---|
[1] | 669 | { |
---|
[657] | 670 | list_entry_t * iter; |
---|
| 671 | kcm_page_t * kcm_page; |
---|
| 672 | uint64_t status; |
---|
| 673 | uint32_t count; |
---|
| 674 | |
---|
[635] | 675 | uint32_t order = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->order) ); |
---|
| 676 | uint32_t full_pages_nr = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->full_pages_nr ) ); |
---|
| 677 | uint32_t active_pages_nr = hal_remote_l32( XPTR( kcm_cxy , &kcm_ptr->active_pages_nr ) ); |
---|
| 678 | |
---|
[657] | 679 | printk("*** KCM : cxy %x / order %d / full_pages_nr %d / active_pages_nr %d\n", |
---|
[635] | 680 | kcm_cxy, order, full_pages_nr, active_pages_nr ); |
---|
[657] | 681 | |
---|
| 682 | if( active_pages_nr ) |
---|
| 683 | { |
---|
| 684 | LIST_REMOTE_FOREACH( kcm_cxy , &kcm_ptr->active_root , iter ) |
---|
| 685 | { |
---|
| 686 | kcm_page = LIST_ELEMENT( iter , kcm_page_t , list ); |
---|
| 687 | status = hal_remote_l64( XPTR( kcm_cxy , &kcm_page->status ) ); |
---|
| 688 | count = hal_remote_l32( XPTR( kcm_cxy , &kcm_page->count ) ); |
---|
| 689 | |
---|
| 690 | printk("- active page %x / status (%x,%x) / count %d\n", |
---|
| 691 | kcm_page, GET_CXY( status ), GET_PTR( status ), count ); |
---|
| 692 | } |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | if( full_pages_nr ) |
---|
| 696 | { |
---|
| 697 | LIST_REMOTE_FOREACH( kcm_cxy , &kcm_ptr->full_root , iter ) |
---|
| 698 | { |
---|
| 699 | kcm_page = LIST_ELEMENT( iter , kcm_page_t , list ); |
---|
| 700 | status = hal_remote_l64( XPTR( kcm_cxy , &kcm_page->status ) ); |
---|
| 701 | count = hal_remote_l32( XPTR( kcm_cxy , &kcm_page->count ) ); |
---|
| 702 | |
---|
| 703 | printk("- full page %x / status (%x,%x) / count %d\n", |
---|
| 704 | kcm_page, GET_CXY( status ), GET_PTR( status ), count ); |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | } // end kcm remote_display() |
---|