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