[1] | 1 | /* |
---|
| 2 | * khm.c - kernel heap manager implementation. |
---|
[18] | 3 | * |
---|
[1] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Alain Greiner (2016) |
---|
| 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
[14] | 25 | #include <kernel_config.h> |
---|
[457] | 26 | #include <hal_kernel_types.h> |
---|
[1] | 27 | #include <hal_special.h> |
---|
| 28 | #include <spinlock.h> |
---|
| 29 | #include <bits.h> |
---|
| 30 | #include <printk.h> |
---|
| 31 | #include <thread.h> |
---|
| 32 | #include <cluster.h> |
---|
| 33 | #include <page.h> |
---|
| 34 | #include <ppm.h> |
---|
| 35 | #include <khm.h> |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | //////////////////////////// |
---|
| 39 | void khm_init( khm_t * khm ) |
---|
| 40 | { |
---|
[20] | 41 | // check config parameters |
---|
[492] | 42 | assert( ((CONFIG_PPM_PAGE_SHIFT + CONFIG_PPM_HEAP_ORDER) < 32 ) , |
---|
[20] | 43 | "CONFIG_PPM_HEAP_ORDER too large" ); |
---|
[1] | 44 | |
---|
[20] | 45 | // initialize lock |
---|
[18] | 46 | spinlock_init( &khm->lock ); |
---|
| 47 | |
---|
[20] | 48 | // compute kernel heap size |
---|
| 49 | intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 50 | |
---|
[20] | 51 | // get kernel heap base from PPM |
---|
| 52 | page_t * page = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER ); |
---|
[315] | 53 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy, page ) ); |
---|
| 54 | void * heap_base = GET_PTR( base_xp ); |
---|
[1] | 55 | |
---|
[20] | 56 | // initialize first block (complete heap) |
---|
[1] | 57 | khm_block_t * block = (khm_block_t *)heap_base; |
---|
| 58 | block->size = heap_size; |
---|
| 59 | block->busy = 0; |
---|
| 60 | |
---|
[20] | 61 | // initialize KHM fields |
---|
[1] | 62 | khm->base = (intptr_t)heap_base; |
---|
| 63 | khm->size = heap_size; |
---|
| 64 | khm->next = (intptr_t)heap_base; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | ///////////////////////////////// |
---|
[18] | 68 | void * khm_alloc( khm_t * khm, |
---|
[1] | 69 | uint32_t size ) |
---|
| 70 | { |
---|
[18] | 71 | khm_block_t * current; |
---|
[1] | 72 | khm_block_t * next; |
---|
| 73 | uint32_t effective_size; |
---|
| 74 | |
---|
[20] | 75 | // compute actual block size |
---|
[1] | 76 | effective_size = size + sizeof(khm_block_t); |
---|
| 77 | effective_size = ARROUND_UP( effective_size, CONFIG_CACHE_LINE_SIZE ); |
---|
| 78 | |
---|
[20] | 79 | // get lock protecting heap |
---|
[551] | 80 | uint32_t irq_state; |
---|
| 81 | spinlock_lock_busy( &khm->lock, &irq_state ); |
---|
[18] | 82 | |
---|
[20] | 83 | // define a starting block to scan existing blocks |
---|
| 84 | if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base; |
---|
| 85 | else current = (khm_block_t*)khm->next; |
---|
[1] | 86 | |
---|
[20] | 87 | // scan all existing blocks to find a free block large enough |
---|
[18] | 88 | while( current->busy || (current->size < effective_size)) |
---|
[1] | 89 | { |
---|
[20] | 90 | // get next block pointer |
---|
[1] | 91 | current = (khm_block_t*)((char*)current + current->size); |
---|
[18] | 92 | |
---|
[1] | 93 | if( (intptr_t)current >= (khm->base + khm->size) ) // heap full |
---|
| 94 | { |
---|
[551] | 95 | spinlock_unlock_busy(&khm->lock, irq_state ); |
---|
[1] | 96 | |
---|
[18] | 97 | printk("\n[ERROR] in %s : failed to allocate block of size %d\n", |
---|
[1] | 98 | __FUNCTION__ , effective_size ); |
---|
| 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[20] | 103 | // split the current block if it is too large |
---|
[1] | 104 | if( (current->size - effective_size) >= CONFIG_CACHE_LINE_SIZE ) |
---|
| 105 | { |
---|
[20] | 106 | // update new free block features |
---|
[1] | 107 | next = (khm_block_t *)((char*)current + effective_size); |
---|
| 108 | next->size = current->size - effective_size; |
---|
| 109 | next->busy = 0; |
---|
| 110 | |
---|
[20] | 111 | // register new free block |
---|
[1] | 112 | khm->next = (intptr_t)next; |
---|
| 113 | |
---|
[20] | 114 | // update allocated block features |
---|
[1] | 115 | current->size = effective_size; |
---|
| 116 | current->busy = 1; |
---|
| 117 | } |
---|
| 118 | else |
---|
[20] | 119 | { |
---|
| 120 | // change block state |
---|
[1] | 121 | current->busy = 1; |
---|
[20] | 122 | } |
---|
[1] | 123 | |
---|
[20] | 124 | // release lock protecting heap |
---|
[551] | 125 | spinlock_unlock_busy( &khm->lock, irq_state ); |
---|
[1] | 126 | |
---|
| 127 | return (char*)current + sizeof(khm_block_t); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /////////////////////////// |
---|
| 131 | void khm_free( void * ptr ) |
---|
| 132 | { |
---|
| 133 | khm_t * khm = &LOCAL_CLUSTER->khm; |
---|
| 134 | |
---|
| 135 | khm_block_t * current; |
---|
| 136 | khm_block_t * next; |
---|
[18] | 137 | |
---|
[1] | 138 | if(ptr == NULL) return; |
---|
[18] | 139 | |
---|
[1] | 140 | current = (khm_block_t *)((char*)ptr - sizeof(khm_block_t)); |
---|
[18] | 141 | |
---|
[20] | 142 | // get lock protecting heap |
---|
[1] | 143 | spinlock_lock(&khm->lock); |
---|
| 144 | |
---|
[492] | 145 | assert( (current->busy == 1) , "page already freed" ); |
---|
[175] | 146 | |
---|
[20] | 147 | // release block |
---|
[1] | 148 | current->busy = 0; |
---|
[18] | 149 | |
---|
[20] | 150 | // try to merge released block with the next |
---|
[1] | 151 | while ( 1 ) |
---|
[18] | 152 | { |
---|
[20] | 153 | next = (khm_block_t*)((char*)current + current->size); |
---|
[1] | 154 | if ( ((intptr_t)next >= (khm->base + khm->size)) || (next->busy == 1) ) break; |
---|
| 155 | current->size += next->size; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if( (intptr_t)current < khm->next ) khm->next = (intptr_t)current; |
---|
[18] | 159 | |
---|
[20] | 160 | // release lock protecting heap |
---|
[1] | 161 | spinlock_unlock( &khm->lock ); |
---|
| 162 | } |
---|
| 163 | |
---|