source: trunk/kernel/mm/kcm.c @ 520

Last change on this file since 520 was 492, checked in by viala@…, 6 years ago

Refactoring assert calling to conform with new assert macro.

Made with this command for the general case.
find ./kernel/ hal/ -name "*.c" | xargs sed -i -e '/assert(/ s/,[ ]*FUNCTION[ ]*,/,/'

And some done by hand.

File size: 9.8 KB
RevLine 
[1]1/*
2 * kcm.c - Per cluster & per type Kernel Cache Manager access functions
[18]3 *
[1]4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
[437]5 *         Alain Greiner    (2016,2017,2018)
[1]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 <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
38//////////////////////////////////////////////////////////////////////////////////////
[7]39// This static function returns pointer on an allocated block from an active page.
[1]40// It returns NULL if no block available in selected page.
41// It changes the page status if required.
42//////////////////////////////////////////////////////////////////////////////////////
[50]43// @ kcm      : pointer on kcm allocator.
44// @ kcm_page : pointer on active kcm page to use.
[7]45/////////////////////////////////////////////////////////////////////////////////////
46static void * kcm_get_block( kcm_t      * kcm,
[50]47                             kcm_page_t * kcm_page )
[1]48{
49
[438]50#if DEBUG_KCM
[433]51uint32_t cycle = (uint32_t)hal_get_cycles();
[438]52if( DEBUG_KCM < cycle )
[433]53printk("\n[DBG] %s : thread %x enters for %s / page %x / count %d / active %d\n",
54__FUNCTION__ , CURRENT_THREAD , kmem_type_str( kcm->type ) ,
55(intptr_t)kcm_page , kcm_page->count , kcm_page->active );
56#endif
57
[492]58        assert( kcm_page->active , "kcm_page should be active" );
[50]59
[20]60        // get first block available
[50]61        int32_t index = bitmap_ffs( kcm_page->bitmap , kcm->blocks_nr );
[1]62
[492]63        assert( (index != -1) , "kcm_page should not be full" );
[18]64
[20]65        // allocate block
[50]66        bitmap_clear( kcm_page->bitmap , index );
[7]67
[50]68        // increase kcm_page count
69        kcm_page->count ++;
[1]70
[50]71        // change the kcm_page to busy if no more free block in page
72        if( kcm_page->count >= kcm->blocks_nr )
[20]73        {
[161]74                kcm_page->active = 0;
[50]75                list_unlink( &kcm_page->list);
[1]76                kcm->active_pages_nr --;
77
[50]78                list_add_first( &kcm->busy_root , &kcm_page->list);
[1]79                kcm->busy_pages_nr ++;
[50]80                kcm_page->busy = 1;
[20]81        }
[1]82
[161]83        // compute return pointer
84        void * ptr = (void *)((intptr_t)kcm_page + CONFIG_KCM_SLOT_SIZE
85                     + (index * kcm->block_size) );
[1]86
[438]87#if DEBUG_KCM
[433]88cycle = (uint32_t)hal_get_cycles();
[438]89if( DEBUG_KCM < cycle )
[464]90printk("\n[DBG] %s : thread %x exit / type  %s / ptr %x / page %x / count %d\n",
91       __FUNCTION__ , CURRENT_THREAD , kmem_type_str( kcm->type ) , (intptr_t)ptr ,
[433]92(intptr_t)kcm_page , kcm_page->count );
93#endif
[50]94
95        return ptr;
[161]96}
[50]97
[1]98/////////////////////////////////////////////////////////////////////////////////////
99// This static function releases a previously allocated block.
[50]100// It changes the kcm_page status if required.
[1]101/////////////////////////////////////////////////////////////////////////////////////
[352]102// @ kcm      : pointer on kcm allocator.
103// @ kcm_page : pointer on kcm_page.
104// @ ptr      : pointer on block to be released.
[7]105/////////////////////////////////////////////////////////////////////////////////////
[352]106static void kcm_put_block ( kcm_t      * kcm,
107                            kcm_page_t * kcm_page,
108                            void       * ptr )
[1]109{
[20]110        uint32_t     index;
[18]111
[161]112        // compute block index from block pointer
[50]113        index = ((uint8_t *)ptr - (uint8_t *)kcm_page - CONFIG_KCM_SLOT_SIZE) / kcm->block_size;
[18]114
[492]115        assert( !bitmap_state( kcm_page->bitmap , index ) , "page already freed" );
116        assert( (kcm_page->count > 0) , "count already zero" );
[176]117
[50]118        bitmap_set( kcm_page->bitmap , index );
119        kcm_page->count --;
120
[20]121        // change the page to active if it was busy
[50]122        if( kcm_page->busy )
[1]123        {
[50]124                kcm_page->busy = 0;
125                list_unlink( &kcm_page->list );
[1]126                kcm->busy_pages_nr --;
127
[50]128                list_add_last( &kcm->active_root, &kcm_page->list );
[1]129                kcm->active_pages_nr ++;
[50]130                kcm_page->active = 1;
[1]131        }
132
[50]133        // change the kcm_page to free if last block in active page
134        if( (kcm_page->active) && (kcm_page->count == 0) )
[1]135        {
[50]136                kcm_page->active = 0;
137                list_unlink( &kcm_page->list);
[1]138                kcm->active_pages_nr --;
139
[50]140                list_add_first( &kcm->free_root , &kcm_page->list);
[1]141                kcm->free_pages_nr ++;
142        }
[161]143}
[1]144
145/////////////////////////////////////////////////////////////////////////////////////
[7]146// This static function allocates one page from PPM. It initializes
[50]147// the kcm_page descriptor, and introduces the new kcm_page into freelist.
[1]148/////////////////////////////////////////////////////////////////////////////////////
149static error_t freelist_populate( kcm_t * kcm )
150{
151        page_t     * page;
[50]152        kcm_page_t * kcm_page;
[20]153        kmem_req_t   req;
[1]154
[20]155        // get one page from local PPM
156        req.type  = KMEM_PAGE;
157        req.size  = 0;
158        req.flags = AF_KERNEL;
159        page = kmem_alloc( &req );
[18]160
[7]161        if( page == NULL )
162        {
[18]163                printk("\n[ERROR] in %s : failed to allocate page in cluster %d\n",
[20]164                       __FUNCTION__ , local_cxy );
165                return ENOMEM;
[7]166        }
167
[20]168        // get page base address
[315]169        xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
170        kcm_page = (kcm_page_t *)GET_PTR( base_xp );
[1]171
[20]172        // initialize KCM-page descriptor
[50]173        bitmap_set_range( kcm_page->bitmap , 0 , kcm->blocks_nr );
[1]174
[50]175        kcm_page->busy          = 0;
176        kcm_page->active        = 0;
177        kcm_page->count      = 0;
178        kcm_page->kcm           = kcm;
179        kcm_page->page          = page;
[1]180
[20]181        // introduce new page in free-list
[50]182        list_add_first( &kcm->free_root , &kcm_page->list );
[1]183        kcm->free_pages_nr ++;
[18]184
[1]185        return 0;
[161]186}
[1]187
188/////////////////////////////////////////////////////////////////////////////////////
[20]189// This private function gets one KCM page from the KCM freelist.
[1]190// It populates the freelist if required.
191/////////////////////////////////////////////////////////////////////////////////////
192static kcm_page_t * freelist_get( kcm_t * kcm )
193{
[7]194        error_t      error;
[50]195        kcm_page_t * kcm_page;
[1]196
[20]197        // get a new page from PPM if freelist empty
[1]198        if( kcm->free_pages_nr == 0 )
199        {
[20]200                error = freelist_populate( kcm );
201                if( error ) return NULL;
[1]202        }
203
[50]204        // get first KCM page from freelist and unlink it
205        kcm_page = LIST_FIRST( &kcm->free_root, kcm_page_t , list );
206        list_unlink( &kcm_page->list );
[1]207        kcm->free_pages_nr --;
208
[50]209        return kcm_page;
[161]210}
[1]211
[7]212//////////////////////////////
213void kcm_init( kcm_t    * kcm,
214                   uint32_t   type )
[1]215{
[161]216        // the kcm_page descriptor mut fit in the KCM slot
217        assert( (sizeof(kcm_page_t) <= CONFIG_KCM_SLOT_SIZE) ,
[492]218                "KCM slot too small\n" );
[1]219
[20]220        // initialize lock
[1]221        spinlock_init( &kcm->lock );
222
[20]223        // initialize KCM type
[1]224        kcm->type = type;
225
[20]226        // initialize KCM page lists
[1]227        kcm->free_pages_nr   = 0;
228        kcm->busy_pages_nr   = 0;
229        kcm->active_pages_nr = 0;
230        list_root_init( &kcm->free_root );
231        list_root_init( &kcm->busy_root );
232        list_root_init( &kcm->active_root );
233
[161]234        // initialize block size
[50]235        uint32_t block_size = ARROUND_UP( kmem_type_size( type ) , CONFIG_KCM_SLOT_SIZE );
[1]236        kcm->block_size = block_size;
[18]237
[50]238        // initialize number of blocks per page
239        uint32_t  blocks_nr = (CONFIG_PPM_PAGE_SIZE - CONFIG_KCM_SLOT_SIZE) / block_size;
[161]240        kcm->blocks_nr = blocks_nr;
241}
[1]242
243///////////////////////////////
244void kcm_destroy( kcm_t * kcm )
245{
[50]246        kcm_page_t   * kcm_page;
[1]247        list_entry_t * iter;
[18]248
[20]249        // get KCM lock
[1]250        spinlock_lock( &kcm->lock );
251
[20]252        // release all free pages
[1]253        LIST_FOREACH( &kcm->free_root , iter )
254        {
[50]255                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
[1]256                list_unlink( iter );
257                kcm->free_pages_nr --;
[50]258                ppm_free_pages( kcm_page->page );
[1]259        }
260
[20]261        // release all active pages
[1]262        LIST_FOREACH( &kcm->active_root , iter )
263        {
[50]264                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
[1]265                list_unlink( iter );
266                kcm->free_pages_nr --;
[50]267                ppm_free_pages( kcm_page->page );
[1]268        }
269
[20]270        // release all busy pages
[1]271        LIST_FOREACH( &kcm->busy_root , iter )
272        {
[50]273                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
[1]274                list_unlink( iter );
275                kcm->free_pages_nr --;
[50]276                ppm_free_pages( kcm_page->page );
[1]277        }
278
[20]279        // release KCM lock
280        spinlock_unlock( &kcm->lock );
[161]281}
[1]282
283///////////////////////////////
284void * kcm_alloc( kcm_t * kcm )
285{
[50]286        kcm_page_t * kcm_page;
[1]287        void       * ptr = NULL;   // pointer on block
288
[20]289        // get lock
[1]290        spinlock_lock( &kcm->lock );
[18]291
[20]292        // get an active page
293        if( list_is_empty( &kcm->active_root ) )  // no active page => get one
294        {
295                // get a page from free list
[50]296                kcm_page = freelist_get( kcm );
[7]297
[182]298                if( kcm_page == NULL )
299                {
300                        spinlock_unlock( &kcm->lock );
301                        return NULL;
302                }
[50]303
[20]304                // insert page in active list
[50]305                list_add_first( &kcm->active_root , &kcm_page->list );
[20]306                kcm->active_pages_nr ++;
[161]307                kcm_page->active = 1;
[20]308        }
[50]309        else                                    // get first page from active list
[20]310        {
[50]311                // get page pointer from active list
312                kcm_page = (kcm_page_t *)LIST_FIRST( &kcm->active_root , kcm_page_t , list );
[20]313        }
[1]314
[20]315        // get a block from selected active page
316        // cannot fail, as an active page cannot be full...
[50]317        ptr  = kcm_get_block( kcm , kcm_page );
[7]318
[20]319        // release lock
[50]320        spinlock_unlock( &kcm->lock );
[1]321
322        return ptr;
[161]323}
[1]324
325///////////////////////////
326void kcm_free( void * ptr )
327{
[50]328        kcm_page_t * kcm_page;
[1]329        kcm_t      * kcm;
[18]330
[492]331        assert( (ptr != NULL) , "pointer cannot be NULL" );
[18]332
[50]333        kcm_page = (kcm_page_t *)((intptr_t)ptr & ~CONFIG_PPM_PAGE_MASK);
334        kcm      = kcm_page->kcm;
[1]335
[20]336        // get lock
[1]337        spinlock_lock( &kcm->lock );
338
[20]339        // release block
[352]340        kcm_put_block( kcm , kcm_page , ptr );
[1]341
[20]342        // release lock
[1]343        spinlock_unlock( &kcm->lock );
[161]344}
[1]345
346////////////////////////////
347void kcm_print (kcm_t * kcm)
348{
[7]349        printk("*** KCM type = %s / free_pages = %d / busy_pages = %d / active_pages = %d\n",
[20]350               kmem_type_str( kcm->type ) ,
351               kcm->free_pages_nr ,
352               kcm->busy_pages_nr ,
353               kcm->active_pages_nr );
[1]354}
Note: See TracBrowser for help on using the repository browser.